C语言实现选择排序算法

2018-06-27 09:47:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

新人新气象,我又来了,C语言实现选择排序。很基础的东西,原理什么的就不扯了。

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <windows.h>
  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<LENGTH;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<LENGTH-1;i++)
 31     {
 32         min=i;
 33         for (j=i+1;j<=LENGTH-1;j++)
 34         {
 35             if (a[j]<a[min])
 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<LENGTH-1;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]<a[min])
 90             {
 91                setcolor(outhandle,a,min,FORE_WHITE);
 92                min=j;
 93                setcolor(outhandle,a,min,FORE_BLUE);
 94                Sleep(100);
 95             }
 96             else
 97             {
 98                 setcolor(outhandle,a,j,FORE_WHITE);
 99                 Sleep(100);
100             }
101         }
102         temp=a[i];
103         a[i]=a[min];
104         a[min]=temp;
105         setcolor(outhandle,a,min,FORE_WHITE);
106         setcolor(outhandle,a,i,FORE_GREEN);
107         Sleep(200);
108     }
109     setcolor(outhandle,a,LENGTH-1,FORE_GREEN);
110     CloseHandle(outhandle);
111 }
点击显示伪代码

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:BZOJ4300: 绝世好题(dp)

下一篇:20个最强的基于浏览器的在线代码编辑器