1 #include
2 #include
3 #include
4
5 #define LENGTH 20
6
7 const WORD FORE_BLUE = FOREGROUND_BLUE|FOREGROUND_INTENSITY;
8 const WORD FORE_GREEN = FOREGROUND_GREEN|FOREGROUND_INTENSITY;
9 const WORD FORE_RED = FOREGROUND_RED|FOREGROUND_INTENSITY;
10 const WORD FORE_WHITE = FOREGROUND_RED | FOREGROUND_GREEN|FOREGROUND_BLUE;
11
12 void sort_selection(int *a);
13 void display(int *a);
14 void details(int *a);
15 void setcolor(HANDLE outhandle,int *a,int index,WORD rgb);
16
17 void display(int *a)
18 {
19 int i;
20 for (i=0;i
21 {
22 printf("%2d ",a[i]);
23 }
24 printf("\n");
25 }
26
27 void sort_selection(int *a)
28 {
29 int i,j,min,temp;
30 for (i=0;i
31 {
32 min=i;
33 for (j=i+1;j<=LENGTH-1;j++)
34 {
35 if (a[j]
36 min=j;
37 }
38 temp=a[i];
39 a[i]=a[min];
40 a[min]=temp;
41 }
42 }
43 int main()
44 {
45 int array[LENGTH]={20,62,30,50,80,37,40,22,55,44,
46 77,85,18,44,90,73,26,10,46,64};
47 int array2[LENGTH]={20,62,30,50,80,37,40,22,55,44,
48 77,85,18,44,90,73,26,10,46,64};
49 printf("Before sort:\n");
50 display(array);
51 sort_selection(array);
52 printf("Success sort:\n");
53 display(array);
54 printf("Press to display details:\n");
55 getch();
56 details(array2);
57 getch();
58 return 0;
59 }
60 void setcolor(HANDLE outhandle,int *a,int index,WORD rgb)
61 {
62 if (index<0||index>LENGTH-1)
63 {
64 return;
65 }
66 COORD xy={0,0};
67 xy.Y=5;
68 xy.X=index*3;
69 SetConsoleTextAttribute(outhandle, rgb);
70 SetConsoleCursorPosition(outhandle,xy);
71 printf(" ");
72 SetConsoleCursorPosition(outhandle,xy);
73 printf("%2d",a[index]);
74 }
75 void details(int *a)
76 {
77 int i,j,min,temp;
78 HANDLE outhandle=GetStdHandle(STD_OUTPUT_HANDLE);
79 display(a);
80 for (i=0;i
81 {
82 min=i;
83 setcolor(outhandle,a,min,FORE_BLUE);
84 Sleep(200);
85 for (j=i+1;j<=LENGTH-1;j++)
86 {
87 setcolor(outhandle,a,j,FORE_RED);
88 Sleep(100);
89 if (a[j]