[算法练习]冒泡排序的C语言实现
2017-03-22 22:43
477 查看
#include <stdio.h> int length; void bubble_sort(int A[]){ int i; int len=length; int temp; int n=0;//n is the times of exchange operation while(len>=2){ for(i=0;i<len-1;i++){ /* Bubble A[i]: move i from 0 to len-2, and compare A[i] and A[i+1], if A[i]>A[i+1] then exchange them, thus at last the largest number in the array move to the last of this array. then len <- len-1, and repeat the step above. */ if(A[i]>A[i+1]){ temp=A[i]; A[i]=A[i+1]; A[i+1]=temp; n++; } } len--; } printf("\ntimes of exchange operation is %d",n); } int main(){ int a[]={13,4,9,5,2,7,16,21,12,1}; int b[]={3,5,7,8,12,5,13,16,21,15}; length=sizeof(a)/sizeof(a[0]);//get length of a[] int i; printf("before:\t"); for(i=0;i<length;i++){ printf("%d ",a[i]); } bubble_sort(a); printf("\nafter:\t"); for(i=0;i<length;i++){ printf("%d ",a[i]); } printf("\n--------------------------\n"); printf("\nbefore:\t"); for(i=0;i<length;i++){ printf("%d ",b[i]); } bubble_sort(b); printf("\nafter:\t"); for(i=0;i<length;i++){ printf("%d ",b[i]); } getchar(); }
相关文章推荐
- c语言实现冒泡排序、希尔排序等多种算法示例
- 初入C语言!整数算法练习1:通过编程实现,统计1~n有多少个9 ? 提示:n通过参数传入!
- [算法练习]堆排序的C语言实现
- [算法练习]快速排序的C语言实现
- 算法导论之插入排序,选择排序,归并排序,冒泡排序,希尔排序,堆排序,快速排序的c语言实现
- [算法练习]选择排序的C语言实现
- [算法练习]插入排序的C语言实现
- 冒泡排序及其改进算法C语言实现
- 排序算法的C语言实现以及各个算法的时间复杂度和空间复杂度分析(冒泡排序)
- md5 算法的c语言实现及应用
- 10个重要的算法C语言实现源代码(其2--牛顿插值)
- 数值计算方法与算法:C语言实现利用Gauss消元法解方程组
- 10个重要的算法C语言实现源代码(其4和5---龙贝格和牛顿迭代)
- 10个重要的算法C语言实现源代码(其3--高斯列主元消去法)
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 10个重要的算法C语言实现源代码:拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔
- 冒泡排序(bubble sort)算法实现
- 冒泡排序(bubble sort)算法实现
- 10个重要的算法C语言实现源代码(8-9-10-----秦九昭和幂法和高斯塞德尔)
- 有关统计单词频率的算法c语言实现