Introduction to Algorithms
Bubble sort (Bubble sort) is a typical exchange sorting algorithm that continuously compares adjacent elements and moves large to the back, so the big one will move backwards, so call it bubbling.
Algorithm description
- Compares the adjacent elements. If the first one is larger (small) than the second one, swap them two;
- For each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair, so that the final element should be the largest (small) number;
- Repeat the above steps for all elements except the last one;
- Repeat steps until the sort is complete.
Code implementation
/*** Bubble Sort * * @param array */ Private Static void Bubblesort(int[] array) {if(Array = =NULL|| Array.length==0|| Array.length==1)return; for(inti =0; I < array.length-1; i++) { for(intj =0; J < Array.length-1I J + +) {//Note array bounds if(Array[j] > array[j +1]) {inttemp = Array[j]; ARRAY[J] = array[j +1]; Array[j +1] = temp; } } } }
Performance analysis
In the best case: orderly order, you only need to compare \ (n\) times. The reason is \ (O (n) \).
Worst case: Reverse order, you need to compare \ ((n-1) + (n-2) +......+1\), it is \ (O (n^2) \).
When the original sequence is disorganized, the average time complexity of the bubbling sort is $o (n^2) $.
Because a temporary variable is needed to swap the position of the element (and, naturally, a variable is used to index the sequence), the spatial complexity is \ (O (1) \).
Bubble sort during the sorting process, when element 22 is swapped, the order of the same elements is not changed, so the bubbling sort is a stable sorting algorithm.
Sorting Algorithms |
Average Time complexity |
Best Case |
Worst Case Scenario |
Complexity of Space |
Stability |
Bubble sort |
\ (O (n^2) \) |
\ (O (n) \) |
\ (O (n^2) \) |
\ (O (1) \) |
Stability |
Bubble sort optimization (optimized outer loop)
If no exchange of locations is found in a single trip, then all elements in the unordered area to be sorted are ordered, so the bubbling sort process can be terminated after this sequencing. For this reason, in the algorithm given below, a tag flag is introduced, which is set to before each sequencing start false
. If an interchange occurs during the sorting process, it is set to true
. Check flag at the end of each trip, stop the algorithm if it has not occurred, and stop the next sequence.
/*** Bubble Optimization (outer loop optimization) * * @param array */ Private Static void bubblesort_2(int[] array) {if(Array = =NULL|| Array.length==0|| Array.length==1)return;BooleanFlag =true;//The interchange is true, no occurrence is false, the first judgment must be a flag bit true for(inti =0; I < array.length-1; i++) {flag =false;flag is not sorted before each start of the sort for(intj =0; J < Array.length-1I J + +) {//Note array bounds if(Array[j] > array[j +1]) {inttemp = Array[j]; ARRAY[J] = array[j +1]; Array[j +1] = temp; Flag =true;//indicates exchange of data;} }//Determine if the flag bit is false, if False, indicating that the following elements are ordered, return directly if(Flag = =false)return; } }
Bubble sort optimization (optimize inner loop)
In each scan, remember the location POS where the last interchange occurred, (the adjacent records after that location are ordered). The next time the sort begins,\ (array[1,pos-1]\) is an unordered area,\ (array[pos,n]\) is an ordered area. Therefore, in the next sequencing, just scan to POS location.
/*** Bubble Optimization (inner loop optimization) * * @param array */ Private Static void Bubblesort_3(int[] array) {if(Array = =NULL|| Array.length==0|| Array.length==1)return;BooleanFlag =true;//The interchange is true, no occurrence is false, the first judgment must be a flag bit true intk = array.length-1;intpos =0;The//pos variable is used to mark the position of the last interchange in the Loop . for(inti =0; I < array.length-1; i++) {flag =false;flag is not sorted before each start of the sort for(intj =0; J < K; J + +) {//Note array bounds if(Array[j] > array[j +1]) {inttemp = Array[j]; ARRAY[J] = array[j +1]; Array[j +1] = temp; Flag =true;//indicates exchange of data;pos = j; }} k = pos;//Determine if the flag bit is false, if False, indicating that the following elements are ordered, return directly if(Flag = =false)return; } }
Sort algorithm-bubble sort