Bubble sort of sorting algorithm

Source: Internet
Author: User

Points:Algorithmic thinking (that's how you want to sort) and its core code (JAVA)

algorithm idea : Bubble sort as the name implies, the basic object is a bubble (representing an element or number to be sorted), according to the order from large to small or from small to large, by comparing the size of the adjacent bubbles to the water or the bottom of the bubble.

algorithm Abstract explanation : There is a C language based on the usual knowledge of the array, the default ordering from small to large and a total of N to sort the number, then the output of the list[0]~list[n] should be from small to large arrangement. In a 22 comparison, such as list[i]>list[i+1], the position of two digits is exchanged, otherwise it will not change. And then go on and on to get the entire sorted array.

Core code:
1  Public voidBubblesort (int[] list) {2 3     //number of times to traverse4      for(inti = 0; i < list.length-1; i++) {5 6         //Compare the size of two adjacent numbers from the back to the next, and then, after traversing once, place the small number I in the array in the first position.7 8          for(intj = list.length-1; J > i; j--) {9 Ten             //compares adjacent elements if the preceding number is greater than the subsequent number, then the interchange One  A             inttemp = 0;//the number of temporary numbers to exchange -  -             if(List[j-1] >List[j]) { the  -temp = List[j-1]; -  -LIST[J-1] =List[j]; +  -LIST[J] =temp; +  A             } at  -         } -  -   -  -System.out.format ("%d trip: \ t", i); in  - printall (list); to  +     } -  the}
Algorithm understanding

sort Time : Best case I is already in order, need O (n), the worst is reverse o (n^2); Simply put, the closer the data is to the positive order, the better the algorithm.

stability : The bubble sort is to move the small element forward or move the large element backwards. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements.

So the order of the same elements is not changed, so bubble sort is a stable sorting algorithm .

Optimization

A common improvement to bubble sorting is to add the flag variable nochange, which is used to flag whether there is a data exchange in a sequencing process.

If there is no data exchange at the time of a trip, all data is ordered and the sort is immediately ended, avoiding unnecessary comparisons.

1 //An optimization algorithm for Bubblesort2 3  Public voidBubblesort_2 (int[] list) {4 5     inttemp = 0;//the number of temporary numbers to exchange6 7     BooleanNoChange =false;//Swap Flags8 9  Ten  One     //number of times to traverse A  -      for(inti = 0; i < list.length-1; i++) { -  theNoChange =false; -  -         //Compare the size of two adjacent numbers from the back to the next, and then, after traversing once, place the small number I in the array in the first position. -  +          for(intj = list.length-1; J > i&& (false! = NoChange ); j--) { -  +             //compares adjacent elements if the preceding number is greater than the subsequent number, then the interchange A  at             if(List[j-1] >List[j]) { -  -temp = List[j-1]; -  -LIST[J-1] =List[j]; -  inLIST[J] =temp; -  toNoChange =true; +  -             } the  *         } $ Panax Notoginseng   -  the         //if the flag is false, it means that this round traversal is not commutative, it is an ordered sequence, and can end the sort +  A//if(false==NoChange) Break (where the IF statement and break are actually unnecessary, just add this judging condition to the for-judging loop statement, so I commented out) the  +
- $ $ -System.out.format ("%d trip: \ t", i); - the printall (list); - Wuyi } the -}

Bubble sort of sorting 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.