Bubble sort of Java base algorithm

Source: Internet
Author: User

Contact bubbling algorithm or a freshman, learning the C language bubble algorithm. Now the only thing to remember is bubbling and choosing. Come out just at work when the algorithm basically did not use, now found that these are very good foundation. So he is ready to pick up the knowledge again.

Bubble sort

Bubble sorting is a very simple sort of algorithm, according to the name can know the process, a little bit to move the large number to the top.

Complexity of Time

If the initial state of the file is positive, a single scan will complete the sequencing. The required number of keyword comparisons and record moves has reached the minimum value:. So, the best time to bubble sort is complexity .

If the initial file is reversed, a sequencing is required. The sub-keyword comparison (1≤i≤n-1) is performed for each order, and each comparison must move the record three times to reach the Exchange record location. In this case, the comparison and the number of moves have reached the maximum value:

The worst time complexity for bubbling sorting is .

Overall, the average time complexity for the bubble sort is .

Algorithm stability

The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.

Bubble Sort Code
 Packagecom.roc.bubbl;/*** Bubble sort of Java algorithm *@authorLIAOWP **/ Public classBubblingsort { Public Static voidMain (string[] args) {int[] data={99,45,32,56,12};        print (data);        System.err.println (); System.err.println ("Sort Process Start"); inti,j,temp;  for(i = 0; i < data.length; i++) {//number of items to sort = = size of array             for(j=i+1; j<data.length;j++) {//the number of orders is reduced in descending order, such as 99 requires the following 4 comparisons,                if(Data[j]<data[i]) {//control Ascending also descendingprint (data); System.err.print ("|"); Temp=Data[i]; Data[i]=Data[j]; DATA[J]=temp;                    print (data);                System.err.println (); }}} System.err.println ("Sort End");    print (data); }         Public Static voidPrintint[] Array) {         for(inti = 0; i < Array.Length; i++) {System.err.print (Array[i]+" "); }    }}

Results

99 45 32) 56 12
Sort process started
99 45 32 56 12 |45 99 32 56 12
45 99 32 56 12 |32 99 45 56 12
32 99 45 56 12 |12 99 45 56 32
12 99 45 56 32 |12 45 99 56 32
12 45 99 56 32 |12 32 99 56 45
12 32 99 56 45 |12 32 56 99 45
12 32 56 99 45 |12 32 45 99 56
12 32 45 99 56 |12 32 45 56 99
Sort End
12 32 45) 56 99

 

Bubble sort of Java base algorithm

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.