Principle: The bubble sort needs to traverse the array several times, each traversal must compare the adjacent element, if a pair of descending is interchangeable their value, because the small value floats like "The bubble" to the top, the larger value sinks to the bottom, therefore is called the bubble sort.
Example Demo:
General bubble sort is characterized by: N number of total to compare (Traverse) n-1, the first trip to compare n-i times.
The general procedure is:
Public Static voidBubblesort (int[]List){intn=List. length; for(intI=1; i<n;i++) {//Total comparison n-1 for(intj=0; j<n-i;j++) {//I trip comparison n-i times if(List[j]>List[j+1]){intTemp temp=List[j];List[j]=List[j+1];List[j+1]=temp; } } } }
Attention!!! : You can see that if a traversal does not take place, the next traversal is not necessary because all elements are already in order. The improved algorithm efficiency can be as follows:
Public Static voidBubblesort (int[]List){intn=List. length; Boolean neednextpass=true; for(intI=1; i<n&&neednextpass;i++) {//Total comparison n-1, if there is no interchange in a traversal, then the next traversal is not required, because the elements are lined up.neednextpass=false; for(intj=0; j<n-i;j++) {//I trip comparison n-i times if(List[j]>List[j+1]){intTemp temp=List[j];List[j]=List[j+1];List[j+1]=temp; neednextpass=true; } } } }
The General code is:
Public classBubblesort {//Bubble sort Public Static voidBubblesort (int[]List){intn=List. length; for(intI=1; i<n;i++) {//Total comparison n-1 for(intj=0; j<n-i;j++) {//I trip comparison n-i times if(List[j]>List[j+1]){intTemp temp=List[j];List[j]=List[j+1];List[j+1]=temp; } } } } Public Static voidMain (String args[]) {int[]List={2,1, About, the,9,6, -, -,Panax Notoginseng, A}; System.out.println ("The list before the sort is:"); for(intI=0;i<List. length;i++) {System.out.print (List[i]+" "); } bubblesort (List);//To bubble sortSystem.out.println (); System.out.println ("The sorted list is:"); for(intI=0;i<List. length;i++) {System.out.print (List[i]+" "); } }}
The improved code is:
Public classBubbleimprovedsort {//Bubble sort Public Static voidBubblesort (int[]List){intn=List. length; Boolean neednextpass=true; for(intI=1; i<n&&neednextpass;i++) {//Total comparison n-1, if there is no interchange in a traversal, then the next traversal is not required, because the elements are lined up.neednextpass=false; for(intj=0; j<n-i;j++) {//I trip comparison n-i times if(List[j]>List[j+1]){intTemp temp=List[j];List[j]=List[j+1];List[j+1]=temp; neednextpass=true; } } } } Public Static voidMain (String args[]) {int[]List={2,1, About, the,9,6, -, -,Panax Notoginseng, A}; System.out.println ("The list before the sort is:"); for(intI=0;i<List. length;i++) {System.out.print (List[i]+" "); } bubblesort (List);//To bubble sortSystem.out.println (); System.out.println ("The sorted list is:"); for(intI=0;i<List. length;i++) {System.out.print (List[i]+" "); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Improved bubbling sort Improvedbubblesort