您的位置:首页 > 编程语言 > C语言/C++

C++ bubber sort

2014-07-09 21:27 127 查看
bubber sort:, 也就是常说的冒泡法排序。 是所有排序算法中比较简单地一个了。

bubber sort 的伪代码如下:

procedure bubbleSort( A:list of sortable items)

n = length(A)

repeat

swapped = false

for i = 1to n-1 inclusivedo

/* if this pair is out of order */

if A[i-1]> A[i]then

/* swap them and remember something changed */

swap( A[i-1], A[i])

swapped = true

end
if

end for

until not swapped

end
procedure



举个例子, the array of numbers "5 1 4 2 8",使用bubber sort 从小到大的对这个数组排序。 则有如下:



Now, the array is already sorted, but our algorithm does not know if it is completed.

The algorithm needs one whole pass without any swap to know it is sorted.(注意啊 , 算法并没有结束)





bubber sort的算法分析:

冒泡排序的最坏的时间复杂度是O(n^2)。



程序如下:

//Bubber sort for array based list

#include <iostream>
#include <ctime>
#include <cstdlib> // 产生随机数
#include <iomanip>

using namespace std;

template <class elemType>
void print(elemType list[], int length);

// do swap

template <class elemType>
void swap(elemType[], int, int);

//bubber sort 总程序
template <class elemType>
void bubberSort(elemType[], int);

int main() {
   int intList[100];
   int num;
   for (int i = 0; i < 100; i++){
      num = (rand() + time(0)) %1000;
      intList[i] = num;
   }
   cout << "intList before sorting: " << endl;
   print(intList, 100);
   cout << endl << endl;
   bubberSort(intList, 100);
   cout << "intList after bubber sort: " << endl;

   print(intList, 100);
   cout << endl;

   system("Pause");
   return 0;

}

template <class elemType>
void print(elemType list[], int length) {
   int count = 0;
   for(int i = 0; i < length; i++) {
      cout << setw(5) << list[i];
      count++;
      if(count % 10 == 0)
         cout << endl;
   }
}

template <class elemType>
void swap(elemType list[], int first, int second) {
   elemType temp;
   temp = list[first];
   list[first] = list[second];
   list[second] = temp;
}

template <class elemType>
void bubberSort(elemType list[], int length) {
   bool swaped = true;

   while(swaped) {
      swaped = false;
      for (int i = 1 ; i < length; i++) {

         if(list[i-1] > list[i]) {
            swap(list, i - 1, i);
            swaped = true;
         }
      }
   }

}


运行结果为:





上述的程序是我根据伪代码编写的。

视频tutorial的代码如下:

//Bubber sort for array based list

#include <iostream>
#include <ctime>
#include <cstdlib> // 产生随机数
#include <iomanip>

using namespace std;

template <class elemType>
void print(elemType list[], int length);

//bubber sort
template <class elemType>
void bubberSort(elemType[], int);

int main() {
   int intList[100];
   int num;
   for (int i = 0; i < 100; i++){
      num = (rand() + time(0)) %1000;
      intList[i] = num;
   }
   cout << "intList before sorting: " << endl;
   print(intList, 100);
   cout << endl << endl;
   bubberSort(intList, 100);
   cout << "intList after bubber sort: " << endl;

   print(intList, 100);
   cout << endl;

   system("Pause");
   return 0;

}

template <class elemType>
void print(elemType list[], int length) {
   int count = 0;
   for(int i = 0; i < length; i++) {
      cout << setw(5) << list[i];
      count++;
      if(count % 10 == 0)
         cout << endl;
   }
}

template <class elemType>
void bubberSort(elemType list[], int length) {
  for (int i = 1; i < length; i++) { // i is 第几次的 iteration
     for (int index = 0; index < length - 1; index++) {
        if(list[index] > list[index + 1]) {
           elemType temp = list[index];
           list[index] = list[index + 1];
           list[index + 1] = temp;
        }
     }
  }
}


运行结果如下:



不难看出, 最坏的情况的时间复杂度是: O(n^2)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: