#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 SelectSort(SqList *L){ int i,j,pos_min; for(i=1;i<L->length;i++){ pos_min = i; for(j=i+1;j<=L->length;j++){ //从i后面记录最小值下标 if(L->r[j] < L->r[pos_min]){ pos_min = j; } } if(i!=pos_min){ //然后进行交换 Swap(L,i,pos_min); } } return; } 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}; SqList L; Assignment(&L,10,a); SelectSort(&L); Output(L); return 0; }
时间: 2024-10-14 21:05:51