1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 #define MAXLENGTH 21
 6 
 7 typedef struct
 8 {
 9     int key;
10 } Record;
11 
12 typedef struct
13 {
14     Record r[MAXLENGTH];
15     int length;
16 } DataList;
17 
18 void printList(DataList *list)
19 {
20     int i = 0;
21     for(i-0; i<list->length; i++)
22     {
23         printf("%d  ",list->r[i]);
24     }
25     printf("\n");
26 }
27 
28 
29 void selection_sort(DataList *list)
30 {
31     int i = 0;
32     int n = list->length;
33     int j = 0;    
34     int k = 0;
35     for(i=1;i<list->length;i++)
36     {
37         k = i;
38         /**
39          *查找右子表的最小值
40          */
41         for(j=i+1;j<n;j++)
42         {
43             if(list->r[k].key>list->r[j].key)
44             {
45                 k=j;
46             }
47         }
48 
49         if(k!=i)
50         {
51             list->r[0] = list->r[i];
52             list->r[i] = list->r[k];
53             list->r[k] = list->r[0];
54         }
55     }
56 
57 }
58 
59 int main()
60 {
61     DataList *list = (DataList *)malloc(1*sizeof(DataList));
62     memset(list,0,sizeof(DataList));
63 
64     srand(time(0));
65 
66     int m = 0;
67     int index = 1;
68     for(m=0; m<5; m++)
69     {
70         int i = 0;
71         i = rand() % 100;
72         list->r[index].key = i;
73         list->length++;
74         index++;
75     }
76     
77     printList(list);
78 
79     selection_sort(list);
80 
81     printList(list);
82 }