Bubble Sort algorithm

Source: Internet
Author: User

The first two articles introduce two very simple and basic sorting algorithms--choose sort and insert sort, in addition to these two, bubble sort is also a very simple sort algorithm. Similarly, this article mainly from the "Basic principles, sequencing processes, core code, algorithm performance, stability, reference code" and other aspects of this algorithm introduced.

Rationale: Compare the size of the two adjacent elements in turn, and if the previous element is greater than (or less than) the next element, then two elements swap positions. This allows each trip to determine a maximum (or minimum) element, each of which can put the largest (or smallest) element at the end until all elements are in order.

Sorting process: The following sequence:5 3 0 4 1 9 7 2 6 8 For example, the bold element represents each of the elements participating in the comparison, the bold element represents the element that is already lined up (not participating in the comparison), and the red element represents the largest element for each row.

Number of Trips Before sorting After sorting Description
1 5 3 0 4 1 9 7 2 6 8 3 0 4 1 5 7 2 6 8 9 9 element Maximum, moving to the last position, at which point the position of the 9 element is determined
2 3 0 4 1 5 7 2 6 8 9 0 3 1 4 5 2 6 7 8 9 8 element Max, moved to 9 element front position, 8 element position determined
3 0 3 1 4 5 2 6 7 8 9 0 1 3 4 2 5 6 7 8 9 7 element Max, moved to 8 element front position, 7 element position determined
4 0 1 3 4 2 5 6 7 8 9 0 1 3 2 4 5 6 7 8 9 6 element Max, moved to 7 element front position, 6 element position determined
5 0 1 3 2 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 5 element Max, moved to 6 element front position, 5 element position determined
6 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 4 element Max, moved to 5 element front position, 4 element position determined
7 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 3 element Max, moved to 4 element front position, 3 element position determined
8 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 2 element Max, moved to 3 element front position, 2 element position determined
9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 1 element Max, moved to 2 element front position, 1 element position determined

The final sort result is: 0 1 2 3 4 5 6 7 8 9. Last trip because only the first two elements were compared, all the elements in the back were ordered, so after the end of the trip, the sequence was ordered. It is important to note that the maximum (or minimum) element can be determined for each sequencing, but the moving element may not be the largest (or smallest) element, such as the position of the 5 element in the first trip. As you can see, if the sequence length is n, a total of N-1 is ordered, each of which can identify an element, so the next step involved in the comparison will be less than the previous one.

Core code: Take Java as an example.

public static void sort (int[] a) {    int n = a.length;  Sequence length for    (int i = 0; i < n-1; i++) {  //Sort trip number for        (int j = 0; J < N-i-1; J + +) {            if (a[j + 1] < A[j]) {                  int temp = a[j];                A[J] = a[j + 1];                A[j + 1] = temp;}}}    }

Algorithmic performance: For sequences where all elements are equal or already ordered, only (N-1) + (N-2) +......+1=n (N-1)/2 comparisons are performed, no movement occurs, and N (N-1)/2 comparisons are performed for the reverse sequence, but each comparison swaps the elements. Each interchange will move two elements, so there will be N (N-1) moves. In general, the time complexity is O (N2) and the space complexity is O (1).

Stability: Bubbling sorting is the process of moving large elements to the back of a sequence, each of which is an adjacent two-element comparison, and the interchange is an adjacent two-element interchange. So if two or more elements in the sequence are equal, the order will not be exchanged, and if two or more equal elements are not adjacent, they will not be exchanged even if they are adjacent by the preceding 22 interchange, so that the relative position of the equal elements is not changed, so the bubbling sort is stable.

Reference code: Take Java as an example.

Import java.util.random;/* * Bubble sort */public class Bubblesort {public static void sort (int[] a) {int n = a.length  ; Sequence length for (int i = 0; i < n-1; i++) {//Sort trip number for (int j = 0; J < N-i-1; J + +) {if (                    A[j + 1] < a[j]) {int temp = a[j];                    A[J] = a[j + 1];                A[j + 1] = temp;        }}}} public static void Main (string[] args) {Random random = new random ();        int[] arg1 = new INT[20];        for (int n=0;n<20;n++) {//Generate 20 random numbers from [0-100] arg1[n] = random.nextint (100);        } System.out.println ("Before sorting:");        for (int i = 0; i < arg1.length; i++) {System.out.print (arg1[i]+ "");        } System.out.println ("\ n sort after:");  Long startTime = System.currenttimemillis ();        Gets the start time sort (arg1);  Long endTime = System.currenttimemillis (); Get end time for (int i = 0; i < arg1.length; i++) {System.out.print (arg1[i]+ "");    } System.out.println ("\ n Sort Duration:" + (Endtime-starttime) + "MS"); }}

Operation Result:

Before ordering: 83 13 36 11 58 75 68 43 73 72 24 81 98 14 27 23 1 46 60 3 sorted: 1 3 11 13 14 23 24 27 36 43 46 58 60 68 72 73 75 81 83 9 8 Sort Duration: 0ms
Zhuanzi http://www.cnblogs.com/Y-oung/p/7820294.html

Bubble Sort 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.