交换排序法

来源:互联网 发布:淘宝企业开店保证金 编辑:程序博客网 时间:2024/06/06 09:33

基本思想:比较第 i 记录和第 j 个(j=i+1....n-1)记录,若不满足预设大小关系则交换

时间复杂度O(n^2),是一种稳定的排序方法

/*exchange sort*/#include <stdio.h>#include <stdlib.h>#define A 10int s[10]={10,32,63,8,100,-10,63,49,76,17};void exchange_sort(int s[],int n){int i,j,temp;for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(s[j]<s[i]){temp=s[i];s[i]=s[j];s[j]=temp;}}}}int main(void){int i;exchange_sort(s,A);printf("The exchange sort result is:\n");for(i=0;i<A;i++){printf("%d ",s[i]);}printf("\n");system("pause");return 0;}


0 0