Bubble Sort Description:
Compare two elements at a time and swap them out if they are in the wrong order.
Repeat until no more swapping is needed, that is, the sorting is complete.
The smaller elements will slowly "float" through the exchange to the top of the sequence.
The bubbling Sorting algorithm works as follows:
- Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
- Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
- Repeat the above steps for all elements, except for the last one.
- Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
A simple bubble sort code:
Importjava.util.Arrays;/*** Achieve bubble sort *@authorDell **/ Public classBubblesort { Public Static voidMain (string[] args) {int[] arr = {1,200,19,55,10,22,3,7}; //API ImplementationArrays.sort (arr); System.out.println (arrays.tostring (arr)); //Bubbling Implementation inttemp; for(intI =0;i<arr.length-1;i++){ for(intj = 0;j<arr.length-1-i;j++){ if(arr[j]>arr[j+1]) {temp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; }}} System.out.println (Arrays.tostring (arr)); }}
Performance analysis and algorithm optimization for bubble sequencing (Outer loop optimization) issues:
Some bubbles after the first round of exchange is already orderly, such as: 2 1 3 4. The more data you have, the slower it is, and it's very unsuitable for big data sorting.
Solutions
If a flag is used to determine if the current array is ordered, exiting the loop in an orderly manner can significantly improve the performance of the bubbling sort.
PackageBubblesort;Importjava.util.Arrays;Importorg.junit.Test;/*** Bubble sequencing performance analysis and algorithm optimization (Outer loop optimization) *@authorDell **/ Public classBubbeSort02 {@Test Public voidtest1 () {BooleanFlag =true; int[] arr = {2,1,3,4,5}; inttemp; for(inti = 0; i < arr.length-1; i++) { for(intj = 0; J < Arr.length-1-i; J + +) { if(arr[j]>arr[j+1]) {temp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; Flag=true; } } if(!flag) { //exits the loop without an exchange; Break; }} System.out.println (Arrays.tostring (arr)); }}
Bubble sort second optimization (Inner loop optimization)
/*** Bubble sequencing performance analysis and algorithm optimization (inner loop optimization)*/@Test Public voidtest2 () {int[] arr = {22,1,10,5}; //mark the location of the last interchange for(inti = 0; i < arr.length-1; i++) { intFlag = 0; inttemp; for(intj = 0; J < Arr.length-i-1; J + +) { if(arr[j]>arr[j+1]) {temp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; //when the position changes, the value of flag changesFlag=1; } } //determine if flag flags have changed without directly ending the inner loop if(flag==0){ return; }} System.out.println (Arrays.tostring (arr)); }}
Reference links
http://blog.csdn.net/pzhtpf/article/details/7560294
Http://www.jianshu.com/p/ffcf5f97bc36
Bubble sort, bubble performance optimization--java implementation