C++ bubber sort
来源:互联网 发布:移动通信 算法 招聘 编辑:程序博客网 时间:2024/06/06 01:49
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 swaptemplate <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 sorttemplate <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++ bubber sort
- Sort-C
- C++sort
- c sort
- SORT BY C
- linux sort uniq -c
- linux sort uniq -c
- C++~~~std::sort
- 【C++】STL的sort
- C ++ sort()与stable_sort()
- C++sort 函数用法
- C++: selection sort
- C语言--sort
- C++sort 函数用法
- C ++ sort()与stable_sort()
- C#ArrayList-sort
- C++STL:sort/qsort
- C++sort 函数用法
- netTcpBinding
- cocos2dx3.0 超级马里奥开发笔记(一)——loadingbar、TableView和pageview的使用
- IOS中XML数据类型的两种解析
- 用代码编辑色块
- 微软的可疑更新DhMachineSvc.exe
- C++ bubber sort
- 初探linux子系统集之led子系统(二)
- Eclipse常用快捷键使用
- SGU 495. Kids and Prizes(概率dp)
- Spring配置数据源的三种方式(dbcp,c3p0,jndi)
- Andoird 项目 国际化方案 以及实现
- Party at Hali-Bula - POJ 3342 树形dp
- 在Mac OS X 10.9.4 Mavericks编译boost 1.55,并在xcode中使用.
- 简单实用国产JQuery UI 框架 之 DWZ富客户端框架