The simple choice of sorting is also called the direct selection sort, the basic idea is as follows:
For a group of keywords {k1,k2,..., kn}, first select the minimum value from the K1,k2,..., kn, and if it is a KZ, swap the KZ with the K1; then select the minimum value from the K2,k3, ..., Kn, and then swap the KZ with the K2. So choose and switch n-2, n-1, select the minimum value from Kn-1 and kn the KZ swaps the KZ with the Kn-1,
The last remaining is the maximum value in the sequence, which is formed by an ordered sequence of small to large.
The algorithm has a time complexity of N (n-1)/2=0 (n2).
Implementation code:
#include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX 1000 #define N 8 typedef struct {
int R[max];
int length;
}sqlist;
void print (SqList L) {int i;
for (i=1;i<l.length;i++) printf ("%d,", l.r[i]);
printf ("%d", l.r[i]);
printf ("\ n");
} 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,min;
for (i=1;i<l->length;i++) {min=i; for (j=i+1;j<=l->length;j++) {if (L->r[min]>l->r[j]) {min=j
;
} if (i!=min) {swap (l,i,min);
int main () {int i;
int d[n]={1,9,2,5,8,11,6,3};
SqList L0;
for (i=0;i<n;i++) {l0.r[i+1]=d[i];
} l0.length=n;
printf ("Pre-sort: \t\t\t");
Print (L0);
printf ("Simple selection Sort: \t\t");
Selectsort (&l0); PrintL0);
return 0;
}
Results: