起泡排序bubblesort
来源:互联网 发布:农村淘宝是做什么的 编辑:程序博客网 时间:2024/04/28 21:50
cmprtimes 记录比较的次数,用了flag以后可以减少比较次数,原因是如果某一轮没有交换数据,那么序列就已经是有序的了,不需要再做任何操作。
#include <stdio.h>#include <stdlib.h>void bubbleSort(int *arr,int n){ int i = 0; int j = 0; int cmprtimes = 0; int flag; for(i = 0;i<n-1;i++){ flag = 0; for(j = 0;j<n-1-i;j++){ cmprtimes++; if(arr[j] > arr[j+1]){ flag = 1; int tmp; tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } if(flag == 0) break; } printf("%d\n",cmprtimes); int k; for(k = 0;k<n;k++){ printf("%d ",arr[k]); }}int main(){ int arr[5] = {1,5,2,3,4}; bubbleSort(arr,5); printf("\nHello world!\n"); return 0;}
0 0
- 起泡排序bubblesort
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序
- 起泡排序/快速排序
- 排序:起泡排序法
- c++实现起泡排序
- javamail发送邮件的简单实例
- 环境的力量
- 详解js跨域问题(推荐阅读)
- Jeecg二级联动
- cogs爱争吵的猴子 题解
- 起泡排序bubblesort
- php函数(今天才看到的记载下来)
- CRichEditCtrl 控件设置透明后,未手动刷新使得显示字体重叠
- Java:单例模式的七种写法
- 你必须非常努力,才能看起来毫不费力
- java ArrayList的排序
- 设计模式——适配器模式
- xiaomiwifi
- OKHttp使用教程