选择排序算法(C语言实现),C语言实现选择排序算法

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]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值