冒泡排序(BubbleSort)
来源:互联网 发布:京瓷1025网络扫描 编辑:程序博客网 时间:2024/04/28 00:20
冒泡排序是一种交换排序,它的基本思想是:两两比较相信记录的关键字,如果反序则交换,直到没有反序的记录为止。时间复杂度为O(n)。
#include <iostream>using namespace std;void Swap(int *L, int i, int j){ int temp = L[i]; L[i] = L[j]; L[j] = temp;}void BubbleSort0(int *L,int n){int i,j;for (i=0; i<n; i++){for (j=i+1; j<n; j++){if (L[i] > L[j]){Swap(L,i,j);}}}}void BubbleSort(int *L, int n)//真正的冒泡排序{ int i,j; for(i=0; i<n; i++) { for (j=n-1; j>=i; j--) {if (L[j] > L[j+1])//区别于上面算法{Swap(L, j, j+1);} } }}void BubbleSort1(int *L, int n)//改进后的算法,减少了不必要的对比{ int i,j; bool flag = true; for (i=0; i<n&&flag; i++) { flag = false; for (j=n-1; j>=i; j--) { if (L[j] > L[j+1]) { Swap(L,j,j+1); flag = true; } } }}int main(){ int L1[9] = {9,1,5,8,3,7,4,6,2};BubbleSort(L1,9);for (int i = 0; i<9; i++){cout<<L1[i]<<" ";}cout<<endl;getchar();return 0;}
结果:
- 冒泡排序 冒泡排序:BubbleSort
- Java BubbleSort(冒泡排序)
- 冒泡排序[BubbleSort]
- 冒泡排序 BubbleSort
- BubbleSort(冒泡排序)
- 冒泡排序bubbleSort
- 冒泡排序 bubblesort
- 冒泡排序bubbleSort
- 冒泡排序(BubbleSort)
- bubbleSort (冒泡排序)
- bubbleSort - 冒泡法排序
- 冒泡排序(BubbleSort)
- 冒泡排序(BubbleSort)
- 冒泡排序(BubbleSort)
- 冒泡排序(BubbleSort)
- BubbleSort冒泡排序
- BubbleSort冒泡排序
- 冒泡排序(BubbleSort)
- p2psearcher源代码
- 使用NDK快速创建文件
- mongodb源码
- μC/OS-II学习之:任务,信号量、邮箱、队列及其区别 (转)
- 1202
- 冒泡排序(BubbleSort)
- IOS中正则表达式使用
- http1.0和http1.1区别
- 免费的Windows8启动程序?下载前请三思!
- 经典收藏 50个jQuery Mobile开发技巧集萃
- C++ 和 C 互相调用
- HTML+CSS
- Servlet操作Cookie说明
- 二叉搜索树