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)。
程序如下:
运行结果为:
上述的程序是我根据伪代码编写的。
视频tutorial的代码如下:
运行结果如下:
不难看出, 最坏的情况的时间复杂度是: O(n^2)
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)
相关文章推荐
- C/C++实现如下 根据Merge Sort原理, 自己实现的归并排序算法+详细注释+代码(C#,C/C++)
- Merge sort in C++
- C++ 排序函数 sort(),qsort()的用法【转】
- C++ 排序函数 sort(),qsort()的用法
- C++使用sort对于结构体排序
- sort c++ 用法
- insert sort with C++
- cplusplus.com C++ : Reference : STL Algorithms : sort
- C++_GUI Programming with Qt4 第二章Sort例子的注意细节
- Heap Sort in C++
- 插入排序法(InsertSort)c++实现
- C++中堆(优先队列)的应用:make_heap, pop_heap, push_heap, sort_heap, priority_queue
- 插入排序法(InsertSort)c++实现 ZT
- 归并排序:Sort:Merge sort using temporary int array(c++)
- C++中sort的用法示例
- base sort using C++
- Sort and Search in C and C++
- shell sort with C++
- C++ sort() 函数用法
- C++ STL list 成员函数 sort算法分析