Bubble sort of algorithm

Source: Internet
Author: User

Bubble sort

Introduced:

Bubble sort (Bubble sort) is a simple sort algorithm . 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 algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

Steps:

    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.

Public  void sort (Int[] arr) {        int len= arr.length;        int temp;         for (int i=1;i<len;i++) {             for  (int j = 0; j < len-i  ; j++)  {                 if (arr[j]>arr[j+1 ]) {                     temp=arr[j+1];                     arr[j+1]=arr[j];                     arr[j]=temp;                 }             }        }    }

Time Complexity Analysis:

Worst case scenario:

When the array is in reverse order, the worst time complexity is O (n^2);

The best case scenario:

The question of the complexity of the time is controversial. The best time complexity for the usual task bubbling is O (n), which is actually not related to the deviation of understanding. For the above code: regardless of whether the element is ordered, if () judgment is to go through, but the order of the word swap () operation does not go, But actually the traversal of the array is the same. The time complexity of the best case is still O (n^2).

How did O (n) come about? O (n) words should only be scanned on one side of the array. The following improved bubbling optimal time complexity is O (n);

Public void bubblesort (int arr[])  {    boolean didSwap=false;     for (int i = 0, len = arr.length; i <  len - 1; i++)  {        didswap = false;/ /each time you need to reset         for (int j = 0; j <  len - i - 1; j++)  {             if (Arr[j + 1] < arr[j])  {                 swap (arr, j, j + 1);                 didSwap = true;             }         }  &nBsp;     if (Didswap == false)//indicates that there is no element exchange occurring during a comparison, at which point the entire array is ordered. You can stop the sort from continuing.             return;    }     }//worst: O (n^2)//best: O (n)

     The above code is a little bit of an improvement on bubbles, here's a further improvement: two-way bubbling algorithm

Public void sort (Int[] array)  {        //element position has interacted , set to true        boolean flag = false;         /* Outer loop Each cycle is completed to determine two values, a maximum of one minimum, so the number of cycles halved,          if the array length is odd, the last loop will have one number remaining, this value must be an intermediate value, no further          columns, and if the array length is even, Cyclic array.length/2*/        int loop = array.length/2;         for (int n = 0;n < loop;n++   ) {            flag = false;              for (int m = n;m  < array.length  - n -1; m++) {            &nbsP;    if (array[m] > array[m+1]) {                     flag = true;                      //forward bubbling                      int temp = array[m];                     array[m] = array[m+1];                      array[m+1] = temp;                 }                 //array.length - 1 -m  the 1th element of the array.length - m -2, the 2nd element of the countdown                  if (array[array.length - 1 -m]  < array[array.length - m -2]) {                     flag = true;                     //Reverse Bubbling                      int temp = array[array.length - 1 -m];                     array[array.length  - 1 -m] = array[array.length - m -2];                     array[array.length - m -2]  = temp;                }             }             if (Flag==false) {                  return;                 }        }     }

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