标签:
三种写法
#include<cstdio> #include<cstdlib> #include<cstring> #define MAXSIZE 10 typedef struct{ int r[MAXSIZE+1]; int length; }SqList; void Swap(SqList *L,int i,int j){ int temp = L->r[i]; L->r[i] = L->r[j]; L->r[j] = temp; } void BubbleSort(SqList *L){ int i,j; for(i=1;i<L->length;i++){ for(j=i+1;j<=L->length;j++){ if(L->r[i]>L->r[j]){ Swap(L,i,j); } } } } void BubbleSort1(SqList *L){ int i,j; for(i=1;i<L->length;i++){ for(j=L->length-1;j>=i;j--){ if(L->r[i]>L->r[j]){ Swap(L,i,j); } } } } void BubbleSort2(SqList *L){ int i,j; bool flag = true; for(i=1;i<L->length && flag;i++){ flag = false; for(j=L->length-1;j>=i;j--){ if(L->r[j]>L->r[j+1]){ Swap(L,j,j+1); flag = true; } } } } void Assignment(SqList *L,int len,int *a){ int i; L->length = len; for(i=1;i<=len;i++){ L->r[i] = a[i-1]; } return ; } void Output(SqList L){ int i; for(i=1;i<=L.length;i++){ printf("%d ",L.r[i]); } printf("\n"); return; } int main(){ int a[10] = {9,1,5,8,3,7,4,6,2,10}; int i,j; SqList L; Assignment(&L,10,a); BubbleSort(&L); Output(L); Assignment(&L,10,a); BubbleSort1(&L); Output(L); Assignment(&L,10,a); BubbleSort2(&L); Output(L); return 0; }
标签:
原文地址:http://www.cnblogs.com/zhuozhuo/p/5449506.html