## Bubble Sort

public static int[] bubbleSort(int[] nums){ //takes an unsorted array, returns sorted one in increasing order

int count=0;

int temp=0;

for(int i=0;i<nums.length-1;i++){ //amount of times array is run through

for(int j=0;j<nums.length-1;j++){ //compares elements

if(nums[j]>nums[j+1]){ //if nums[j]>nums[j+1], swap

temp=nums[j+1];

nums[j+1]=nums[j];

nums[j]=temp;

count++;

}

}

if(count==0){ //if at any time the array is looked through without any changes made, it is sorted

return(nums);

}

}

return(nums); //returns sorted array

}

One of the most popular sorting algorithms. Used very commonly by teachers to demonstrate different methods of sorting, bubble sort has a average time of O(n^2), making it not fast. It goes through an array and looks at two adjacent elements. If they are at the wrong place, swap. If not, ignore. This is done over and over again until the array is fully sorted.

Step-by-step example:

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in

(

( 1

( 1 4

( 1 4 2

(

( 1

( 1 2

( 1 2 4

Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one

(

( 1

( 1 2

( 1 2 4

Finally, the array is sorted, and the algorithm can terminate.

int count=0;

int temp=0;

for(int i=0;i<nums.length-1;i++){ //amount of times array is run through

for(int j=0;j<nums.length-1;j++){ //compares elements

if(nums[j]>nums[j+1]){ //if nums[j]>nums[j+1], swap

temp=nums[j+1];

nums[j+1]=nums[j];

nums[j]=temp;

count++;

}

}

if(count==0){ //if at any time the array is looked through without any changes made, it is sorted

return(nums);

}

}

return(nums); //returns sorted array

}

One of the most popular sorting algorithms. Used very commonly by teachers to demonstrate different methods of sorting, bubble sort has a average time of O(n^2), making it not fast. It goes through an array and looks at two adjacent elements. If they are at the wrong place, swap. If not, ignore. This is done over and over again until the array is fully sorted.

Step-by-step example:

Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in

**bold**are being compared.**First Pass:**(

**5****1**4 2 8 ) (**1****5**4 2 8 ), Here, algorithm compares the first two elements, and swaps them.( 1

**5****4**2 8 ) ( 1**4****5**2 8 ), Swap since 5 > 4( 1 4

**5****2**8 ) ( 1 4**2****5**8 ), Swap since 5 > 2( 1 4 2

**5****8**) ( 1 4 2**5****8**), Now, since these elements are already in order (8 > 5), algorithm does not swap them.**Second Pass:**(

**1****4**2 5 8 ) (**1****4**2 5 8 )( 1

**4****2**5 8 ) ( 1**2****4**5 8 ), Swap since 4 > 2( 1 2

**4****5**8 ) ( 1 2**4****5**8 )( 1 2 4

**5****8**) ( 1 2 4**5****8**)Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one

**whole**pass without**any**swap to know it is sorted.**Third Pass:**(

**1****2**4 5 8 ) (**1****2**4 5 8 )( 1

**2****4**5 8 ) ( 1**2****4**5 8 )( 1 2

**4****5**8 ) ( 1 2**4****5**8 )( 1 2 4

**5****8**) ( 1 2 4**5****8**)Finally, the array is sorted, and the algorithm can terminate.

Provided by website-hit-counters.com site. |