📄 各种排序算法的c语言实现.txt
字号:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<time.h>
#define Up 0x4800
#define Down 0x5000
#define Enter 0x1c0d
#define NO_ENTER 10
#define LEN 20000
int data[LEN+1];
int long_n;
int g,h=1;
FILE *fp;
void Insort(int data[])
{
int i,j;
for(i=2;i<=long_n;i++)
{
data[0]=data;
j=i-1;
while(data[0]<data[j])
{data[j+1]=data[j];
j--;
}
data[j+1]=data[0];
if(long_n<=50)
{if(long_n<=50)
{printf("\n");
printf(" Insert Sort\n");
printf(" %d times:\n",i-1);
printf(" ");
for(g=1;g<=long_n;g++)
printf("%d ",data[g]);
if(g==8) {printf("\n");printf(" ");}
}
getch();}
}
}
void Bubble_sort(int data[])
{int i,j,t;
for(j=1;j<=long_n;j++)
{for(i=1;i<=long_n-j;i++)
{if(data>data[i+1])
{t=data;
data=data[i+1];
data[i+1]=t;
}
}
if(long_n<=50)
{if(long_n<=50)
{printf("\n");
printf(" Bubble Sort\n");
printf(" %d times:\n",j);
printf(" ");
for(g=1;g<=long_n;g++)
printf("%d ",data[g]);
if(g==8) {printf("\n");printf(" ");}
}
getch();
} }
}
void Selectsort(int data[])
{
int i,j,min,temp;
for(i=1;i<=long_n;i++)
{min=i;
for(j=i+1;j<=long_n;j++)
if(data[min]>data[j])
min=j;
temp=data;
data=data[min];
data[min]=temp;
if(long_n<=50)
{if(long_n<=50)
{printf("\n");
printf(" Select Sort\n");
printf(" %d times:\n",i);
printf(" ");
for(g=1;g<=long_n;g++)
printf("%d ",data[g]);
if(g==8) {printf("\n");printf(" ");}
}
getch(); }
}
}
void Quick_sort(int data[],int low,int high)
{int pivotloc;
if(long_n<=50)
{if(long_n<=50)
{if(h!=0)
{printf("\n");
printf(" Quick Sort\n");
printf(" %d times:\n",h);
printf(" ");
for(g=1;g<=long_n;g++)
printf("%d ",data[g]);
if(g==8) {printf("\n");printf(" ");}
}
getch();} } h++;
if(low<high)
{pivotloc=Partition(data,low,high);
Quick_sort(data,low,pivotloc-1);
Quick_sort(data,pivotloc+1,high);
if(long_n<=50)
{if(long_n<=50)
{printf("\n");
printf(" Quick Sort\n");
printf(" %d times:\n",h);
printf(" ");
h++;
for(g=1;g<=long_n;g++)
printf("%d ",data[g]);
if(g==8) {printf("\n");printf(" ");}
}
getch();}
}
}
int Partition(int data[],int low,int high)
{int pivotkey;
data[0]=data[low];
pivotkey=data[low];
while(low<high)
{while(low<high&&data[high]>=pivotkey) --high;
data[low]=data[high];
while(low<high&&data[low]<pivotkey) ++low;
data[high]=data[low];
}
data[low]=data[0];
return low;
}
void HeapAdjust(int data[],int s,int m)
{
int j,rc;
rc=data[s];
for(j=2*s;j<=m;j*=2)
{if(j<m&&data[j]>data[j+1]) ++j;
if(rc<data[j]) break;
data[s]=data[j];
s=j;
}
data[s]=rc;
}
void Heap_sort(int data[])
{
int i,temp;
for(i=long_n/2;i>0;--i)
HeapAdjust(data,i,long_n);
for(i=long_n;i>1;--i)
{
if(long_n<=50)
{if(long_n<=50)
{printf("\n");
printf(" Heap Sort\n");
printf(" %d times:\n",h);
printf(" ");
h++;
for(g=1;g<=long_n;g++)
printf("%d ",data[g]);
if(g==8) {printf("\n");printf(" ");}
}
getch();}
temp=data[1];
data[1]=data;
data=temp;
HeapAdjust(data,1,i-1);
}
}
main()
{
int gdriver=DETECT,gmode;
int x,y,ch,y_pos,b_k,position,lon,wide,c_lon,c_wide,deltx,delty,middle,count,depth;
time_t start,end;
char *key="b";
char *key1;
struct number
{
int left;
int top;
int right;
int bottom;
}number[6];
struct character
{
int x;
int y;
char title[20];
}c_size[6];
strcpy(c_size[0].title,"Insert sort");
strcpy(c_size[1].title,"Bubble sort");
strcpy(c_size[2].title,"Select sort");
strcpy(c_size[3].title,"Quick sort");
strcpy(c_size[4].title,"Heap sort");
strcpy(c_size[5].title,"Quit");
fp=fopen("sort.txt","w+");
while(1)
{
textcolor(YELLOW);
clrscr();
gotoxy(20,10);
putch(218);
for(b_k=0;b_k<30;b_k++) putch(196);
putch(191);
for(b_k=0;b_k<2;b_k++)
{gotoxy(20,11+b_k);putch(179);
gotoxy(51,11+b_k); putch(179);
}
gotoxy(20,12);putch(192);
for(b_k=0;b_k<30;b_k++) putch(196);
putch(217);
gotoxy(21,11);
printf("please iuput the key:");
scanf("%s",key1);
if(strcmp(key1,key)==0) break;
else {printf("\n\n\n");
printf(" Error!");
getch();
clrscr();
}
}
loop: lon=150;
wide=250;
c_lon=165;
c_wide=260;
deltx=100;
delty=30;
middle=40;
count=1;
depth=1;
initgraph(&gdriver,&gmode,"");
setbkcolor(BLUE);
cleardevice();
for(x=0;x<6;x++)
{
number[x].left=wide;
number[x].right=wide+deltx;
number[x].top=lon;
number[x].bottom=lon+delty;
lon+=middle;
}
for(x=0;x<6;x++) a
{c_size[x].x=c_wide;
c_size[x].y=c_lon;
c_lon+=delty+10;
}
for(x=0;x<6;x++)
{setfillstyle(1,7);
bar(number[x].left,number[x].top,number[x].right,number[x].bottom);
setcolor(5);
line(number[x].left,number[x].bottom,number[x].right,number[x].bottom);
line(number[x].right,number[x].top,number[x].right,number[x].bottom);
}
setcolor(12);
for(x=0;x<6;x++)
outtextxy(c_size[x].x,c_size[x].y,c_size[x].title);
x=0;
setcolor(4);
outtextxy(c_size[x].x,c_size[x].y,c_size[x].title);
setcolor(8);
outtextxy(210,400,"Designed by Lei 2004 1");
position=1;
y=NO_ENTER;
while(y==NO_ENTER)
{ch=bioskey(0);
switch(ch)
{case Up:
if(position>1)
{
position--;
setcolor(12);
outtextxy(c_size[x].x,c_size[x].y,c_size[x].title);
x--;
setcolor(4);
outtextxy(c_size[x].x,c_size[x].y,c_size[x].title);
printf("\7");
}break;
case Down:
if(position<6)
{
position++;
setcolor(12);
outtextxy(c_size[x].x,c_size[x].y,c_size[x].title);
x++;
setcolor(4);
outtextxy(c_size[x].x,c_size[x].y, c_size[x].title);
printf("\7");
}break;
case Enter: y=position;break;
}
}
cleardevice();
closegraph();
randomize();
if(y==6) {fclose(fp);exit(0);}
textcolor(YELLOW);
clrscr();
gotoxy(20,10);
putch(218);
for(b_k=0;b_k<30;b_k++) putch(196);
putch(191);
for(b_k=0;b_k<2;b_k++)
{gotoxy(20,11+b_k);putch(179);
gotoxy(51,11+b_k); putch(179);
}
gotoxy(20,12);putch(192);
for(b_k=0;b_k<30;b_k++) putch(196);
putch(217);
gotoxy(21,11);
printf("please iuput the number:");
scanf("%d",&long_n);
printf("\n\n\n");
for(x=1;x<=long_n;x++)
data[x]=rand()%5000;
if(long_n<=50)
{printf(" the orginal datas:\n");
printf(" ");
for(x=1;x<=long_n;x++)
{printf("%d ",data[x]);
if(x%11==0)
printf("\n");
}}
printf("\n\n");
switch(y)
{ case 1: start=time(NULL);Insort(data);end=time(NULL); break;
case 2: start=time(NULL);Bubble_sort(data);end=time(NULL); break;
case 3: start=time(NULL);Selectsort(data);end=time(NULL); break;
case 4: start=time(NULL);h=0;Quick_sort(data,1,long_n);end=time(NULL); break;
case 5: start=time(NULL);h=1;Heap_sort(data); end=time(NULL);break;
}
printf("\n\n");
clrscr();
printf("The sorted datas:\n");
for(x=1;x<=long_n;x++)
{printf("%-7d",data[x]);
fprintf(fp,"%-7d",data[x]);
count++;
if(count==10) {count=0;printf("\n");depth++;}
if(depth==25)
{
depth=1;
printf("\n");
printf(" to be continued...");
getch();
clrscr();
}
} getch();
printf("\nused time: ");
printf("%2.2fseconds",difftime(end,start));
getch();
goto loop;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -