Optimized Bubble Sort - java

I would like to know how else I can optimize bubble sort so that it overlooks elements that have already been sorted, even after the first pass.
Eg. [4, 2, 3, 1, 5, 6] --> [2, 3, 1, **4, 5, 6**]
We observe that [4,5,6] are already in sorted order, how can I modify my code so that it overlooks this 3 elements in the next pass? Which means the sort would be more efficient? Do you suggest a recursive method?
public static void bubbleSort(int[] a) {
for (int i = 1; i < a.length; i++) {
boolean is_sorted = true;
for (int j = 0; j < a.length; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
is_sorted = false;
}
}
if (is_sorted) return;
}
}

First of all, you have an out-of-bounds access:
for (int j = 0; j < a.length; j++) {
if (a[j] > a[j + 1]) {
for j == a.length-1, so the loop condition should rather be j < a.length-1.
But, in Bubble sort, you know that after k passes, the largest k elements are sorted at the k last entries of the array, so the conventional Bubble sort uses
public static void bubbleSort(int[] a) {
for (int i = 1; i < a.length; i++) {
boolean is_sorted = true;
// skip the already sorted largest elements
for (int j = 0; j < a.length - i; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
is_sorted = false;
}
}
if (is_sorted) return;
}
}
Now, that would still do a lot of unnecessary iterations when the array has a long sorted tail of largest elements, say you have k,k-1,...,1 as the first k elements and k+1 to 100000000 in order after that. The standard Bubble sort will pass k times through (almost) the entire array.
But if you remember where you made your last swap, you know that after that index, there are the largest elements in order, so
public static void bubbleSort(int[] a) {
int lastSwap = a.length - 1;
for (int i = 1; i < a.length; i++) {
boolean is_sorted = true;
int currentSwap = -1;
for (int j = 0; j < lastSwap; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
is_sorted = false;
currentSwap = j;
}
}
if (is_sorted) return;
lastSwap = currentSwap;
}
}
would sort the above example with only one pass through the entire array, and the remaining passes only through a (short) prefix.
Of course, in general, that won't buy you much, but then optimising a Bubble sort is a rather futile exercise anyway.

public static void BubbleSorter(params int[] input) {
int newSize = input.Length - 1, size = 0;
bool swap;
do {
swap = false;
for (int j = 0; j < newSize; j++) {
if (input[j] > input[j + 1]) {
int temp = input[j + 1];
input[j + 1] = input[j];
input[j] = temp;
swap = true;
size = j;
}
}
newSize = size;
} while (swap);
DisplayArrayElements(input);
}

public static Integer[] optimizedBubbleSort(Integer[] input) {
long startTime = System.nanoTime();
boolean swapped = true;
for (int pass = input.length - 1; pass >= 0 && swapped; pass--) {
swapped = false;
for (int i = 0; i < pass; i++) {
if (input[i] > input[i + 1]) {
int temp = input[i];
input[i] = input[i + 1];
input[i + 1] = temp;
swapped = true;
}
}
}
System.out.println("Time taken for OPTIMIZED bubbleSort: "
+ (System.nanoTime() - startTime));
return input;
}

you should use a variable "size" for the inner loop and change it to the latest swapped element in each cycle.This way your inner loop goes up to the latest "swapped" element and passes the rest that are unswapped (aka in their correctplace). i.e
do {
int newsize = 0;
for (int i = 1; i < size; i++) {
if (a[i - 1] > a[i]) {
int temp;
temp = a[i - 1];
a[i - 1] = a[i];
a[i] = temp;
newsize = i;
}
}
size = newsize;
} while (size > 0);

In the above example, the array got sorted after 3rd pass, but we will still continue with the 4th, 5th pass. Suppose if the array is already sorted, then there will be no swapping (because adjacent elements are always in order), but still we will continue with the passes and there will still be (n-1) passes.
If we can identify, that the array is sorted, then we should stop execution of further passes. This is the optimization over the original bubble sort algorithm.
If there is no swapping in a particular pass, it means the array has become sorted, so we should not perform the further passes. For this we can have a flag variable which is set to true before each pass and is made false when a swapping is performed.
void bubbleSort(int *arr, int n) {
for (int i = 0; i < n; i++) {
bool flag = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
flag = true;
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
// No Swapping happened, array is sorted
if (!flag) {
return;
}
}
}

You can use a single do-while-loop instead of two nested for-loops and move the logic into the internal if-statement. Subsequent passes are shorter by the pass index.
public static void bubbleSort(int[] arr) {
boolean swapped = false;
int i = 0, pass = 0;
do {
if (i < arr.length - 1 - pass) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
i++;
} else {
i = 0;
pass++;
swapped = false;
}
} while (i < arr.length - 1 - pass || swapped);
}
public static void main(String[] args) {
int[] arr = {6, 1, 5, 8, 4, 3, 9, 2, 0, 7};
System.out.println(Arrays.toString(arr));
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
Output:
[6, 1, 5, 8, 4, 3, 9, 2, 0, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
See also: Bubble sort output is incorrect

public class Tester {
static boolean bubbleFlag = true;
public static void main(String[] args) {
int array[] = new int[]{1, 9, 2, 3, 4, 5, 6};
bubbleSort(array);
}
private static void bubbleSort(int... array) {
System.out.println("Before Sorting: " + Arrays.toString(array));
for (int i = 0; i < array.length - 1; i++) {
if (i > 0) if (bubbleFlag) break;
for (int j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) array = swap(j, j + 1, array);
System.out.println("Iteration "+j+" :"+Arrays.toString(array));
}
bubbleFlag = true;
}
}
private static int[] swap(int i1, int i2, int... is) {
bubbleFlag = false;
is[i1] = is[i1] + is[i2];
is[i2] = is[i1] - is[i2];
is[i1] = is[i1] - is[i2];
return is;
}
}
Output:
Before Sorting: [1, 9, 2, 3, 4, 5, 6]
Iteration 0 :[1, 9, 2, 3, 4, 5, 6]
Iteration 1 :[1, 2, 9, 3, 4, 5, 6]
Iteration 2 :[1, 2, 3, 9, 4, 5, 6]
Iteration 3 :[1, 2, 3, 4, 9, 5, 6]
Iteration 4 :[1, 2, 3, 4, 5, 9, 6]
Iteration 5 :[1, 2, 3, 4, 5, 6, 9]

I devised a method that reduces the number of iterations by excluding parts at the beginning and end of the array that have been ordered in previous loops.
static int[] bubbleSortOptimized(int arr[]) {
int start = 0, stop = arr.length - 1, control = 0;
boolean ordered, nsCaught;
while (true) {
ordered = true;
nsCaught = false;
for (int i = start; i < stop; i++) {
if (i > 1) {
if (!nsCaught && arr[i - 2] > arr[i - 1]) {
ordered = false;
start = i - 2;
nsCaught = true;
}
}
if (arr[i] > arr[i + 1]) {
int hold = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = hold;
control = i;
}
}
System.out.println(Arrays.toString(arr));
if (ordered) return arr;
stop = control;
}
}
But as #Daniel Fischer said in an earlier answer, it doesn't do a lot with larger arrays.

I think this is what you need. The key is to consider the
array only till the index where last swap occurred (newn).
public static void bubbleSort(int[] a) {
int i, n, newn;
n = a.length;
while (n > 0) {
newn = 0;
for (i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
temp = a[i];
a[i] = a[i - 1];
a[i - 1] = temp;
newn = i;
}
}
n = newn;
}
return a;
}

Here is the simplest, best and optimal Bubble Sort Algorithm using a while loop. It sorts the numbers in the given array form left to right in ascending order. It is very simple to understand and easy to implement.
private static int[] bubbleSort(int[] array) {
int length = array.length - 1;
int index = 0;
while (index < length) {
if (array[index] > array[index + 1]) {
swap(array, index, index + 1);
}
index++;
if (index == length) {
index = 0;
length--;
}
}
return array;
}
private static void swap(int[] array, int index1, int index2) {
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
public static void main(String[] args) {
int[] arr = {4, 2, 3, 1, 5, 6};
System.out.println(Arrays.toString(arr));
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
Output:
[4, 2, 3, 1, 5, 6]
[1, 2, 3, 4, 5, 6]

I don't have code, but you could use n bits to keep track of where swaps were performed in the last pass. Or, less effectively, use a single variable to track where the first swap was performed. We don't need to re-compare elements that were not swapped - they are the same elements in the same order, so we know the comparisons will be the same, and we can safely skip them.
Intuitively I feel, even with the above optimization, bubble sort would still not beat the comparisons of a binary insertion sort, and would introduce a lot more branch logic (on top of the auxiliary space) to keep track of the swaps. So this is probably not worth investigating, unless someone is curious.

Optimized bubble sort with just 1 for Loop
/*Advanced BUBBLE SORT with ONE PASS*/
/*Authored by :: Brooks Tare AAU*/
public class Bubble {
public int[] bubble(int b[]) {
int temp, temp1;
for (int i = 0; i < b.length - 1; i++) {
if (b[i] > b[i + 1]) {
///swap(b[i],b[i+1]);
temp = b[i];
b[i] = b[i + 1];
b[i + 1] = temp;
// Checking if there is any number(s) greater
// than the current number. If there is swap them.
while (i > 0) {
if (b[i] < b[i - 1]) {
///swap(b[i]<b[i-1])
temp1 = b[i];
b[i] = b[i - 1];
b[i - 1] = temp1;
i--;
} else if (b[i] > b[i - 1]) {
i--;
}
}
} else {
continue;
}
}
return b;
}
///the following is a function to display the Array
public void see(int[] a) {
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + ",");
}
}
public static void main(String[] args) {
///You can change the Array to your preference..
// u can even make it dynamic
int b[] = {5, 1, 4, 2, 0, 3};
int v[] = new int[100];
Bubble br = new Bubble();
v = br.bubble(b);
br.see(v);
}
}

Related

Delete local maximum

A local maximum is an element that is greater than any of its neighboring elements. You must remove elements that are local maxima in the original array.
Input array:
[18, 1, 3, 6, 7, -5]
output array:
[1, 3, 6, -5]
public static int[] removeLocalMaxima(int[] array){
int[] result = new int[array.length];
int j = 0;
for (int i = 0; i < array.length - 1; i++, j++) {
if(array[i] > array[i + 1]){
result[j] = array[++i];
}else {
result[j] = array[i];
}
}
return Arrays.copyOf(result, j);
}
if you set it like that, then something is not working:
array = new int[1000];
Arrays.fill(array, 15);
array[0] = -20;
array[999] = 25;
array[168] = 18;
array[421] = 0;
actual = LocalMaximaRemove.removeLocalMaxima(array);
assertEquals(998, actual.length);
assertEquals(-20, actual[0]);
assertEquals(15, actual[997]);
assertEquals(0, actual[420]);
public static int[] removeLocalMaxima(int[] array){
int[] result = new int[array.length];
int j = 0;
for (int i = 0; i < array.length; i++) {
if ((i > 0 && array[i] <= array[i - 1])
|| (i != array.length - 1 && array[i] <= array[i + 1])){
result[j++] = array[i];
}
}
return Arrays.copyOf(result, j);
}
My function saves in result array only elements that are less or equal of any of its neightbors.

Given an array, please determine whether it contains three numbers whose sum equals to 0 [duplicate]

This question already has answers here:
Finding three elements in an array whose sum is closest to a given number
(15 answers)
Closed 6 years ago.
Given an array, please determine whether it contains three numbers whose sum equals to 0.
Java
I could easily think of a brute force solution
public class HelloWorld {
public static void main(String[] args) {
int[]arr = {2, 3, 4, 5, 6, 7, -7, 0, -9};
boolean found = false;
for(int x = 0; x < arr.length; x++){
for(int y = 0; y < arr.length; y++){
for(int z = 0; z < arr.length; z++){
if(arr[x] + arr[y] + arr[z] == 0){
found = true;
break;
}
}
}
}
if(found){
System.out.println("Was found");
} else{
System.out.println("Was not found");
}
}
}
This is clearly O(N^3) but how can I do better?
A little better solution will be:
int[]arr = {2, 3, 4, 5, 6, 7, -7, 0, -9};
boolean found = false;
for(int x = 0; x < arr.length-3; x++){
for (int y = x+1; y < arr.length - 2; y++){
for (int z = y+1; z < arr.length; z++){
if(arr[x] + arr[y] + arr[z] == 0){
found = true;
break;
}
}
}
}
if(found){
System.out.println("Was found");
} else{
System.out.println("Was not found");
}
}
You don't need to add the same number to itself again.
Hence you can use y = x+1 and z = y+1.
Another better O(n^2) solution is something like this which uses sorting:
class FindTriplet
{
// returns true if there is triplet with sum equal
// to 'sum' present in A[]. Also, prints the triplet
boolean find3Numbers(int A[], int arr_size, int sum)
{
int l, r;
/* Sort the elements */
quickSort(A, 0, arr_size - 1);
/* Now fix the first element one by one and find the
other two elements */
for (int i = 0; i < arr_size - 2; i++)
{
// To find the other two elements, start two index variables
// from two corners of the array and move them toward each
// other
l = i + 1; // index of the first element in the remaining elements
r = arr_size - 1; // index of the last element
while (l < r)
{
if (A[i] + A[l] + A[r] == sum)
{
System.out.print("Triplet is " + A[i] + " ," + A[l]
+ " ," + A[r]);
return true;
}
else if (A[i] + A[l] + A[r] < sum)
l++;
else // A[i] + A[l] + A[r] > sum
r--;
}
}
// If we reach here, then no triplet was found
return false;
}
int partition(int A[], int si, int ei)
{
int x = A[ei];
int i = (si - 1);
int j;
for (j = si; j <= ei - 1; j++)
{
if (A[j] <= x)
{
i++;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
int temp = A[i + 1];
A[i + 1] = A[ei];
A[ei] = temp;
return (i + 1);
}
/* Implementation of Quick Sort
A[] --> Array to be sorted
si --> Starting index
ei --> Ending index
*/
void quickSort(int A[], int si, int ei)
{
int pi;
/* Partitioning index */
if (si < ei)
{
pi = partition(A, si, ei);
quickSort(A, si, pi - 1);
quickSort(A, pi + 1, ei);
}
}
// Driver program to test above functions
public static void main(String[] args)
{
FindTriplet triplet = new FindTriplet();
int A[] = {1, 4, 45, 6, 10, 8};
int sum = 0; // this is what you are looking for
int arr_size = A.length;
triplet.find3Numbers(A, arr_size, sum);
}
}
Fort more read here:
http://www.geeksforgeeks.org/find-a-triplet-that-sum-to-a-given-value/

How can I get this to check all of the elements on one side of the array instead of stopping at one local minimum?

When I run this code, it stops at 2 as the local minimum and doesn't check the rest of the values on the other half of the array. It should also print 0 as a local minimum.
How can I solve this? (it has to be in O(logn) time complexity)
Code:
public class LocalMinimum {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int[] array = {8, 5, 7, 2, 3, 0, 1, 9};
int leftIndex = 0;
int rightIndex = array.length -1;
while (leftIndex < rightIndex) {
int middleIndex = (leftIndex + rightIndex)/2;
if ((array[middleIndex] < array[middleIndex - 1]) &&
(array[middleIndex] < (array[middleIndex + 1]))) {
System.out.println("Local minimum: " + array[middleIndex]);
break;
}
else if (array[middleIndex - 1] < array[middleIndex + 1]) {
rightIndex = middleIndex - 1;
}
else {
leftIndex = middleIndex + 1;
}
}
}
}
Try this code:
int[] array = {8, 5, 7, 2, 3, 0, 1, 9};
for (int i=0; i < array.length; ++i) {
if ( (i == 0 || array[i] < array[i-1]) &&
(i == array.length-1 || array[i] < array[i+1])) {
System.out.println("Local minimum: " + array[i]);
}
}
Use a plain old for loop:
int[] array = {8, 5, 7, 2, 3, 0, 1, 9};
for (int index = 1; index < array.length - 1; index++)
{
if (array[index] < array[index - 1] &&
array[index] < array[index + 1])
{
System.out.println("Local minimum: " + array[index]);
}
}
for(int i=1;i<array.length;i++) {
if(array[i]<array[i-1] && array[i]<array[i+1]) {
System.out.println(array[i]);
}
}

I need to find the values that are local minimums in this array, but I'm getting an arrayindexoutofbounds exception. How can I fix this?

public class LocalMinimum {
public static void main(String[] args) {
int[] A = {8, 5, 7, 2, 3, 0, 1, 9};
for (int i = 0; i < A.length; ++i) {
int prevValue = A[i - 1];
int nextValue = A[i + 1];
if ((A[i] < prevValue) && (A[i] < nextValue)) {
System.out.println(A[i] + " is a minimum value.");
}
else {
System.out.println(A[i] + " is not a minimum value.");
}
}
}
}
The error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
You should use module when trying to access A's itens so it never is out of bounds.
like:
int prevValue = A[((i - 1) % A.length)];
int nextValue = A[((i + 1) % A.length)];
This way, the result for the first iteration for prevValue will be the last number instead and the result for the last iteration for nextValue will be the first number.
If you are writing
int prevValue = A[i - 1];
int nextValue = A[i + 1];
in the for loop, starting from i = 0 and ending with i = array.length - 1, be aware that at the first and last turn, you will be trying to get values from array[-1] and array[array.length] elements, which do not exist. Try starting the loop from 1 to array.length - 2:
for (int i = 1; i < array.length - 2; i++) {
// do your job
}

Using a For Loop to Manually Sort an Array - Java

I'm having trouble manually sorting the array with a for loop. It works except for the first and last number. Here's my code:
Scanner numInput = new Scanner(System.in);
int tempVar, num;
String numbersString;
int[] numbers = {4, 11, 13, 12, 17, 35, 15, 7, 19, 3, 45};
for (int i = 0; i < numbers.length - 1; i++)
{
for(int j = 0; j < numbers.length - 1; j++)
{
if(numbers[i] < numbers[j + 1])
{
tempVar = numbers [j + 1];
numbers [j + 1]= numbers [i];
numbers [i] = tempVar;
}
}
}
numbersString = Arrays.toString(numbers);
System.out.println(numbersString);
You have to initialize the value for j as i+1, this sorting algorithm is called bubble sort, that works by repeatedly swapping the adjacent elements if they are in wrong order. The below method is always runs O(n^2) time even if the array is sorted.
public static void main (String[] args)
{
int[] array = {4,2,1,3,5,9,6,8,7};
for(int i = 0 ; i < array.length;i++)
{
for(int j = i+1 ; j< array.length;j++)
{
if(array[i] > array[j])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
public int[] sort(int[] arr) {
int marker, i, temp;
marker =0;
i = 1;
while (marker < arr.length - 1) {
if (i == arr.length) {
marker++;
i = marker;
}
if (arr[marker] > arr[i]) {
temp = arr[marker];
arr[marker] = arr[i];
arr[i] = temp;
}
i++;
}
return arr;
}
Try this one:
int temp = 0;
for (int i = 0; i < numbers.length - 1; i++) {
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[i] > numbers[j]) {
temp = numbers[j];
numbers[j] = numbers[i];
numbers[i] = temp;
}
}
}
You have little wrong second for iteration and reverse condition.
you have some errors in your code. I make some modifications, please look:
int tempVar, num;
String numbersString;
int[] numbers = {4, 11, 13, 12, 17, 35, 15, 7, 19, 3, 45};
for (int i = 0; i < numbers.length; i++) {
for (int j = i; j < numbers.length; j++) {
if (numbers[i] < numbers[j]) {
tempVar = numbers[i];
numbers[i] = numbers[j];
numbers[j] = tempVar;
}
}
}
numbersString = Arrays.toString(numbers);
System.out.println(numbersString);
First, I recommend you to iterate in the second loop, from i (beacause the items previous i are now sorted).
Second, you have to switch the items on i and j positions.
Finally, beacause you use < strict comparator un your loops break, you have to go user numbers.length and not numbers.length - 1 .
For more information, please see the buble sort algorithm
I will provide a modified version of your code with the intention of explaining why your code does not work.
First, we need to decide specifically what we want to do. Lets say we want to sort the numbers in descending order.
What does this mean? Every time when we are comparing two values at lowIndex and highIndex, we want to make sure that the value at lowIndex is higher than the value at highIndex.
The problem with your code is that it does not keep track of which index, i or j+1, that is lower.
When i==1 and j+1==2, your code will swap the values so that the greatest value out of numbers[1] and numbers[2] will be put at index 1.
When i==2 and j+1==1, your code will swap the values so that the smallest value out of numbers[1] and numbers[2] will be put at index 1.
This is incosistent. The algorithm is competing with itself, trying to move values in different directions. If we modify your code to check that we are consistent in whether we want to swap large values towards the beginning of the array or towards the end, your algorithm will start to work:
for (int i = 0; i < numbers.length - 1; i++)
{
for(int j = 0; j < numbers.length - 1; j++)
{
if(numbers[i] < numbers[j + 1] && i < (j + 1)) //NOTE: additional condition for indices
{
tempVar = numbers [j + 1];
numbers [j + 1]= numbers [i];
numbers [i] = tempVar;
}
}
}
Note, however, that the example above is just for explaining what goes wrong in execution of your code. Rather than using this code, it would probably be more appropriate to use one of the other answers to this question, or study and compare sorting algorithms on wikipedia
Try this
class desc
{
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
System.out.print(" "+arr[i]);
}
}
void sort(int arr[],int n)
{
for (int i = 1; i < n; i++)
{
if(arr[i] < arr[i - 1] )
{
arr[i] = arr[i] + arr[i - 1];
arr[i - 1] = arr[i] - arr[i - 1];
arr[i] = arr[i] - arr[i - 1];
i=0;
}
}
}
public static void main(String []args)throws Exception
{
int[] arr = {-5, 0, -7, -2, -5, 1, -9, -1};
int n = arr.length;
desc d=new desc();
d.sort(arr,n);
d.printArray(arr, n);
}
}
I believe what you are willing to do is selection sort? https://en.wikipedia.org/wiki/Selection_sort An other option I see is bubble sort but I'll try to explain selection sort.
So the first iteration of the outer for loop, the one with i, you check the whole array for the smallest number with the inner for loop, the one with j, and you put the smallest number upfront in the array. During the second iteration of the outer for loop you only go over the numbers you haven't checked yet, which is the second number through the last number. During the third iteration you go over the third number through the last number and so on. Here's how I adjusted your code, I adjusted the inner for loop so with each iteration you check a smaller sub-list and adjusted your if clause so that the smallest number is found:
int tempVar;
String numbersString;
int[] numbers = {4, 11, 13, 12, 17, 35, 15, 7, 19, 3, 45};
for (int i = 0; i < numbers.length - 1; i++)
{
// each iteration i you would need to go over a smaller array, so you set j = i each time
for(int j = i; j < numbers.length - 1; j++){
// checking if numbers[i] is greater than numbers[j + 1] instead of smaller than
if(numbers[i] > numbers[j + 1]){
tempVar = numbers [j + 1];
numbers [j + 1]= numbers [i];
numbers [i] = tempVar;
}
}
}
numbersString = Arrays.toString(numbers);
System.out.println(numbersString);
for(int i = 0; i < nums.length - 1; ++i){
for(int j = i + 1; j < nums.length; ++j){
if(nums[i]>nums[j]){
int tempVar = nums[i];
nums[i] = nums[j];
nums[j] = tempVar;
}
}
}
return nums;
}

Categories

Resources