C语言实现选择排序算法

新人新气象,我又来了,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 }
点击显示伪代码

 

转载于:https://www.cnblogs.com/lisg001/p/9086204.html

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页