/**
*冒泡排序
*平均时间复杂度O(n^2)
*/
#include<stdio.h>
#define ElemType int
void swap(ElemType *a,ElemType *b){
ElemType temp;
temp = *a;
*a = *b;
*b = temp;
}
void bubble_sort(ElemType a[],int len){
int i,j;
for(i=0;i<len-1;i++){
for(j=0;j<len-1-i;j++){
if(a[j]>a[j+1]){
swap(&a[j],&a[j+1]);
}
}
}
}
int main(){
ElemType a[5]={9,1,7,8,6};
bubble_sort(a,5);
for(int i=0;i<5;i++){
printf("%d ",a[i]);
}
return 0;
}
算法-003 冒泡排序算法
最新推荐文章于 2023-05-18 15:59:22 发布