Bubble sort (Bubble sort)

Source: Internet
Author: User

Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The name of this algorithm is because the larger the element will slowly "float" to the top of the sequence, hence its name.algorithm principleThe bubbling Sort algorithm works as follows: (from back to forward)
    1. Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
    2. Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
    3. Repeat the above steps for all elements, except for the last one.
    4. Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.

Algorithm AnalysisComplexity of TimeIf 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 StabilityThe 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.
// Exchange Index I and index J values    Private void int int j) {        T tmp;         = Data[i];         = Data[j];         = tmp;    }
//-----Bubble Sort Method Time complexity O (n^2)-----     Public voidBubblesort (t[] data) {intI, J;  for(i = 0; i < data.length-1; i++) {             for(j = 0; J < data.length-1-I; j + +) {                if(Data[j].compareto (data[j + 1]) > 0) {Swap (data, J+ 1, J); }            }        }    }     Public voidBubblesort (int[] num) {        //Defining temporary variables        inttemp = 0; //number of external loop control comparisons         for(inti = 0; i < num.length; i++) {                  //Internal loop control comparison post shift             for(intj = num.length-1; J > i; j--) {                      if(num[j-1]<Num[j]) {Temp= Num[j-1]; Num[j-1] =Num[j]; NUM[J]=temp; }                  }              }              //Output Array         for(inti = 0; i < num.length; i++) {System.out.println (Num[i]+","); }       }

Bubble sort (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.