Sort of a common algorithm "bubble sort"

Source: Internet
Author: User

Bubble sort is a sort of algorithm that we basically get into contact with in the beginning of programming, since it is more concise and more image.

Its idea is to let each of the adjacent elements compare, if they do not follow the ascending or descending, then exchange their positions, repeat the operation, the largest or smallest elements like bubbles, rise to the top, the remaining elements repeat this operation, all the elements can be ordered.

The way it works:

1. Compare from the back, if the size between adjacent elements is not in ascending or descending order to compare them, if not specify the rules, then exchange their positions

2. According to (1), compare each pair in turn until the end

3. In addition to the above-selected maximum or minimum elements, repeat the above steps until there are no numbers to compare

so we can see that, without any optimization, the number of times it needs to be compared is O (n^2), which requires O (n) + O (1) space

Let's look at the code below:

    private static void Swap (int[] array, int from, int. to) {        int temp;        temp = Array[from];        Array[from] = array[to];        Array[to] = temp;    }    public static void sort (int[] array) {for        (int i = 0; i < array.length-1; i++) {for            (int j = 0; J < Array. Length-i-1; J + +) {                if (Array[j] > Array[j+1]) {                    swap (array, j+1, j);}        }}    
We have said that each time we need to compare adjacent elements, if one is not the last traversal, we find that there is no exchange, then it means that our array is already in order, so there is no need to continue the subsequent operation. So we can set a flag bit to check if we still need to compare.

    public static void sort (int[] array) {for        (int i = 0; i < array.length-1; i++) {            Boolean flag = true;            for (int j = 0; J < array.length-i-1; j + +) {                if (Array[j] > Array[j+1]) {                    swap (array, j + 1, j);                    Flag = false;                }            }            if (flag) {                return;}}    }
However, we seem to be able to find bubble sort also has a feature, bubble sort after the array, its top must be orderly, when we in a trip the last Exchange element position above is ordered, and is the largest or smallest of several elements. So in the next iteration, we don't have to compare them, and then combine the above flags, and we can change it to:

    public static void sort (int[] array) {        int exchange = ARRAY.LENGTH-1;           Location of the last interchange        while (Exchange > 0) {            int bound = Exchange;                   Last position of each Exchange            exchange  = 0;            for (int j = 0; J < bound; J + +) {                if (Array[j] > Array[j+1]) {                    swap (array, j + 1, j);                    Exchange = J;}}}    

Here, let's summarize:

Bubble sort, average time complexity O (n^2), Worst time complexity O (n^2), Optimal time complexity O (n), Worst space complexity O (n) + O (1)

Well, if you have a better way, welcome the message to discuss.


Sort the common algorithm one "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.