Sort algorithm (I) bubble sort

Source: Internet
Author: User

Bubble sort (Bubble sort) , it repeatedly visits the elements to be sorted, compares the adjacent two elements in turn, and, if they are in the wrong order, swaps them until no elements need to be exchanged again, sorting is done. The algorithm is named because the smaller (or larger) element will slowly "float" through the switch to the top of the sequence.

The bubbling Sorting algorithm works as follows:

    • Compare adjacent elements, if the previous one is larger than the last one, put them two swap positions
    • Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. When this is done, the final element will be the largest number.
    • Repeat the above steps for all elements, except for the last
    • repeat the above steps each time for less and fewer elements until there are no pairs of numbers to compare

Code instance   array to sort: int[] arr={25,18,21,9}; 

    • the array before sorting is: 9
    • the order of the array after the 1th trip 1th time: 9
    • 1th Trip 2nd order of array : 9
    • order of the array after the 1th trip 3rd time: 9 + li>
    • order of the array after the 1th trip: 9
    • the order of the array after the 2nd trip 1th Time: 9 +
    • 2nd Trip 2nd Order of the array: 9 page
    • 2nd trip order of the array after sorting: 18 9 21 25
    • 3rd order 1th time after ordering of the array: 9 (+)
    • Span style= "COLOR: #ff00ff" > order of the array after the 3rd trip: 9
    • < strong> the sorted array is: 9 +

The advantage of bubbling sorting: each trip is sorted less once, because a larger value is found for each sort of trip. As in the example above: After the first comparison, the last number must be the largest one, and when the second pass is sorted, only the number other than the last number can be compared, and a maximum number is also found after the number of the second comparison, and the third comparison is You only need to compare the number other than the last two numbers, and so on ... That is, not a trip to compare, each trip less than once, to a certain extent, reduce the amount of the algorithm.

In terms of time complexity:

1. If our data is in the right order, we need to take a trip to complete the sequencing. The required number of comparisons C and the number of records moved m are the minimum value, namely: cmin=n-1; Mmin=0; Therefore, the best time complexity for bubble sorting is O (n).

2. If unfortunately our data is reversed, we need to n-1 the order. Each order is n-i compared (1≤i≤n-1), 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: O (n2).

Summary: The total average time complexity of the bubble sort is: O (n2)

 PackageCom.yyx.sortingAlgorithm;/*** Bubble Sort * Yyx February 3, 2018*/ Public classBubblesort { Public Static voidMain (string[] args) {inttemp; int[] arr={25,18,21,9}; System.out.print ("The array before sorting is:");  for(intNum:arr) {System.out.print (num+" "); }         for(inti=0;i<arr.length-1;i++){             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;                } System.out.println (); System.out.print ("First" + (i+1) + "goto" + (j+1) + "Order of the array after order:");  for(intNum:arr) {System.out.print (num+" ");            }} System.out.println (); System.out.print ("First" + (i+1) + "Order of the array after sequencing:");  for(intNum:arr) {System.out.print (num+" ");        } System.out.println ();        } System.out.println (); System.out.print ("The sorted array is:");  for(intNum:arr) {System.out.print (num+" "); }    }}

Optimization

A common improvement to bubble sorting is to add the symbolic variable Exchange, which is used to flag whether there is a data exchange during a trip sort. If there is no data exchange at the time of a trip, all data is in order and can be sorted immediately, avoiding unnecessary comparisons

 PackageCom.yyx.sortingAlgorithm;/*** Bubble Sort * Yyx February 3, 2018*/ Public classBubblesort { Public Static voidMain (string[] args) {inttemp; BooleanBchange =false; int[] arr={25,18,21,9};  for(inti=0;i<arr.length-1;i++){             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; Bchange=true; }                          }             //bchange==false means no Exchange .            if(bchange==false){                 Break; }} System.out.print ("The sorted array is:");  for(intNum:arr) {System.out.print (num+" "); }    }}

Sort algorithm (I) bubble sort

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.