C语言实现选择排序

C语言实现选择排序

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

 #include <stdio.h>
#include <stdlib.h>
#include <windows.h> #define LENGTH 20 const WORD FORE_BLUE = FOREGROUND_BLUE|FOREGROUND_INTENSITY;
const WORD FORE_GREEN = FOREGROUND_GREEN|FOREGROUND_INTENSITY;
const WORD FORE_RED = FOREGROUND_RED|FOREGROUND_INTENSITY;
const WORD FORE_WHITE = FOREGROUND_RED | FOREGROUND_GREEN|FOREGROUND_BLUE; void sort_selection(int *a);
void display(int *a);
void details(int *a);
void setcolor(HANDLE outhandle,int *a,int index,WORD rgb); void display(int *a)
{
int i;
for (i=;i<LENGTH;i++)
{
printf("%2d ",a[i]);
}
printf("\n");
} void sort_selection(int *a)
{
int i,j,min,temp;
for (i=;i<LENGTH-;i++)
{
min=i;
for (j=i+;j<=LENGTH-;j++)
{
if (a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
int main()
{
int array[LENGTH]={,,,,,,,,,,
,,,,,,,,,};
int array2[LENGTH]={,,,,,,,,,,
,,,,,,,,,};
printf("Before sort:\n");
display(array);
sort_selection(array);
printf("Success sort:\n");
display(array);
printf("Press to display details:\n");
getch();
details(array2);
getch();
return ;
}
void setcolor(HANDLE outhandle,int *a,int index,WORD rgb)
{
if (index<||index>LENGTH-)
{
return;
}
COORD xy={,};
xy.Y=;
xy.X=index*;
SetConsoleTextAttribute(outhandle, rgb);
SetConsoleCursorPosition(outhandle,xy);
printf(" ");
SetConsoleCursorPosition(outhandle,xy);
printf("%2d",a[index]);
}
void details(int *a)
{
int i,j,min,temp;
HANDLE outhandle=GetStdHandle(STD_OUTPUT_HANDLE);
display(a);
for (i=;i<LENGTH-;i++)
{
min=i;
setcolor(outhandle,a,min,FORE_BLUE);
Sleep();
for (j=i+;j<=LENGTH-;j++)
{
setcolor(outhandle,a,j,FORE_RED);
Sleep();
if (a[j]<a[min])
{
setcolor(outhandle,a,min,FORE_WHITE);
min=j;
setcolor(outhandle,a,min,FORE_BLUE);
Sleep();
}
else
{
setcolor(outhandle,a,j,FORE_WHITE);
Sleep();
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
setcolor(outhandle,a,min,FORE_WHITE);
setcolor(outhandle,a,i,FORE_GREEN);
Sleep();
}
setcolor(outhandle,a,LENGTH-,FORE_GREEN);
CloseHandle(outhandle);
}

点击显示伪代码

C语言实现选择排序算法-LMLPHP

05-11 22:42