Bubble Sort and complexity analysis

http://www.cnblogs.com/jiqingwu/p/bubble_sort_analysis.html

  • Problem: Given a sequence of integers, in ascending order (specifically, a non-descending order) are arranged in the sequence of integers.
  • Input: a sequence of integers.
  • Output: sequence of integers, wherein the integer ascending order.

Because the C language textbooks Hao strong, we are most familiar is probably the bubble sort.
The following is a bubble sort of a C language, ais the first address of the array,  size is the number of array elements.

Thought bubble sort is to allow the maximum number of floating to the last position in the array, followed by a large number of floats to the array penultimate position ......
Of course, you can also descending order, can take from back to front bubble. Wherein the comparison operation and the adjacent exchanger elements.

void bubble_sort(int *a, int size)
{
  int i, j, t;
  for(i = 1; i < size; ++i){
    for(j = 0; j < size -i; ++j){
      if(a[j] > a[j+1]){
        t = a[j];
        a[j] = a[j+1];
        a[j+1] = t;
      }
    } // end for j
  }// end for i
}

Time complexity analysis. The outer loop performs its N - 1 times. Most performed N times when the inner loop, at least when performed once, the average execution  (N+1)/2times.
It is more about the body exchange cycle execution  (N - 1)(N + 1) / 2 = (N^2 - 1)/2(which N^2is modeled Latex the notation, N represents the square). Accordance with the principles of the computational complexity, the constant is removed, excluding the highest coefficient, degree of complexity O(N^2).

Performance bubbling Algorithm. There is room for improvement in the performance of the above algorithm. Given a sequence of integers  [9, 3, 4, 5, 7], each time to complete the outer loop algorithm described above, to change the sequence of integers:

9, 3, 4, 5, 7
3, 4, 5, 7, 9 (i = 1)
3, 4, 5, 7, 9 (i = 2)
3, 4, 5, 7, 9 (i = 3)
3, 4, 5, 7, 9 (i = 4)

We found that for the first time after the completion of the outer loop, sorting is complete. The latter cycle only relatively, but no exchange.
When the primary outer loop, no exchange occurs adjacent to the element, it shows the array has been ordered, this time may be out of the loop.
In this way, we can set a Boolean variable, whether the exchange takes place once the outer loop recording, if the exchange did not occur, the algorithm returns.

Improved bubble sort C language as follows:

void bubble_sort_enhanced(int *a, int size)
{
    int i, j, t;
    unsigned char swapped;
    for(i = 1; i < size; ++i) {
        swapped = 0;
        for(j = 0; j < size - i; ++j) {
            if(a[j] > a[j+1]){
                t = a[j];
                a[j] = a[j+1];
                a[j+1] = t;
                swapped = 1;
            }
        }
        if(!swapped)
            break;
    }
}

按照改进的算法,对于一个已经有序的数组,算法完成第一次外层循环后就会返回。
实际上只发生了 N - 1次比较,所以最好的情况下,该算法复杂度是O(N)


Guess you like

Origin blog.csdn.net/Kurry4ever_/article/details/80883491