Java Improved bubbling sort Improvedbubblesort

Source: Internet
Author: User



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.