📄 sort.txt
字号:
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <time.h>
#define ALT_F 33 /*定义键盘编码*/
#define ALT_S 31
#define ALT_D 32
#define ENTER 13
#define ESC 27
#define KEY_DOWN 80
#define KEY_UP 72
#define OVERFLOW -1
#define ERROR 0
#define OK 1
char *mainmenu[]={"File","Sort","Demo"}; /*主菜单项*/
char *red[]={"F","S","D"};
char *fileSM[]={"Creat","Display","Quit"};
char *sortSM[]={"Bubble","Select","Insert","BinaryInsert","Quick","Shell","Heap"};
char *demoSM[]={"Bubble","DelayExchange","Shell","ShellMetzner","Quick","Insert"};
char *menubuf[25*80*2],*winbuf[25*80*2]; /*定义保存文本的缓冲区*/
int i,key,x,y;
#define MAXSIZE 10000 /* 最大长度MAXSIZE */
typedef struct /* 记录类型 */
{ int key; /* 关键字为整型 */
/* InfoType otherinfo; 其它数据类型(可选项)*/
}RecType; /* 记录类型名 */
typedef struct
{
RecType *r; /* r[0]用作监视哨 */
int length; /* 实际表长 */
}SeqList; /* 记录表类型 */
int get_key();
void box(int startx,int starty,int endy,int endx);
int display_fileSM(void);
int display_sortSM(void);
int get_key (void) /*取键扫描码*/
{
int key;
while(bioskey(1)==0);
key=bioskey(0);
key=key&0xff?key&0xff:key>>8;
return(key);
}
void box(int startx,int starty,int endy,int endx) /*画矩形下拉框函数*/
{
int i;
gotoxy(startx,starty);
putch(0xda);
for(i=startx+1;i<endx;i++)putch(0xc4);
putch(0xbf);
for(i=starty+1;i<endy;i++)
{
gotoxy(startx,i);putch(0xb3);
gotoxy(endx,i);putch(0xb3);
}
gotoxy(startx,endy);
putch(0xc0);
for (i=startx+1;i<endx;i++)
putch(0xc4);
putch(0xd9);
return;
}
void display_mainmenu( )
{
textbackground(BLUE);
clrscr();
textmode (C80);
window(1,2,80,25);
textbackground(BLUE);
clrscr();
window(1,1,80,1);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
window(1,1,80,2); /* 定义显示菜单的窗口*/
for(i=0;i<3;i++)
{
x=wherex();
y=wherey();
cprintf(" %s",mainmenu[i]); /*显示各项菜单*/
gotoxy(x,y);
textcolor(RED);
cprintf(" %s",red[i]);
gettext(1,1,80,25,winbuf);
x=x+15;
gotoxy(x,y);
textcolor(BLACK);
}
return;
}
int display_fileSM(void)
{
textbackground(BLACK);
textcolor(WHITE);
gotoxy(3,1);
cprintf("%s",mainmenu[0]);
window(2,2,17,6);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
window(2,2,17,7);
box(1,1,5,16);
for(i=2;i<5;i++)
{
gotoxy(2,i);
cprintf(" %s",fileSM[i-2]);
}
gettext(2,2,17,3,menubuf);
textbackground(GREEN);
textcolor(WHITE);
gotoxy(2,2);
cprintf(" %s",fileSM[0]);
y=2;
key=get_key();
while(key!=ENTER&&key!=ESC)
{
if(key==KEY_UP||key==KEY_DOWN)
{
puttext(2,y,17,y+1,menubuf);
if(key==KEY_UP) y=y==2?4:y-1;
if(key==KEY_DOWN)y=y==4?2:y+1;
gettext(2,y,17,y+1,menubuf);
textbackground(GREEN);
textcolor(WHITE);
gotoxy(2,y);
cprintf(" %s",fileSM[y-2]);
}
key=get_key();
}
if (key==ESC)
{
window(1,1,80,2);
puttext(1,1,80,25,winbuf);
return 0;
}
if(key==ENTER)
{
window(1,1,80,2);
puttext(1,1,80,25,winbuf);
return(y-1);
}
}
int display_sortSM(void)
{
textbackground(BLACK);
textcolor(WHITE);
gotoxy(18,1);
cprintf("%s",mainmenu[1]);
window(15,2,30,10);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
window(15,2,30,11);
box(1,1,9,16);
for(i=2;i<9;i++)
{
gotoxy(2,i);
cprintf(" %s",sortSM[i-2]);
}
gettext(15,2,30,3,menubuf);
textbackground(GREEN);
textcolor(WHITE);
gotoxy(2,2);
cprintf(" %s",sortSM[0]);
y=2;
key=get_key();
while(key!=ENTER&&key!=ESC)
{
if(key==KEY_UP||key==KEY_DOWN)
{
puttext(15,y,30,y+1,menubuf);
if(key==KEY_UP) y=y==2?8:y-1;
if(key==KEY_DOWN)y=y==8?2:y+1;
gettext(15,y,30,y+1,menubuf);
textbackground(GREEN);
textcolor(WHITE);
gotoxy(2,y);
cprintf(" %s",sortSM[y-2]);
}
key=get_key();
}
if (key==ESC)
{
window(1,1,80,2);
puttext(1,1,80,25,winbuf);
return 0;
}
if(key==ENTER)
{
window(1,1,80,2);
puttext(1,1,80,25,winbuf);
return(y-1);
}
}
int display_demoSM(void)
{
textbackground(BLACK);
textcolor(WHITE);
gotoxy(33,1);
cprintf("%s",mainmenu[2]);
window(30,2,45,9);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
window(30,2,45,10);
box(1,1,8,16);
for(i=2;i<8;i++)
{
gotoxy(2,i);
cprintf(" %s",demoSM[i-2]);
}
gettext(30,2,45,3,menubuf);
textbackground(GREEN);
textcolor(WHITE);
gotoxy(2,2);
cprintf(" %s",demoSM[0]);
y=2;
key=get_key();
while(key!=ENTER&&key!=ESC)
{
if(key==KEY_UP||key==KEY_DOWN)
{
puttext(30,y,45,y+1,menubuf);
if(key==KEY_UP) y=y==2?7:y-1;
if(key==KEY_DOWN)y=y==7?2:y+1;
gettext(30,y,45,y+1,menubuf);
textbackground(GREEN);
textcolor(WHITE);
gotoxy(2,y);
cprintf(" %s",demoSM[y-2]);
}
key=get_key();
}
if (key==ESC)
{
window(1,1,80,2);
puttext(1,1,80,25,winbuf);
return 0;
}
if(key==ENTER)
{
window(1,1,80,2);
puttext(1,1,80,25,winbuf);
return(y-1);
}
}
int Display(SeqList *H)
{
int j;
printf("\n\n**************************************\n");
for(j=1;j<=H->length;j++)
printf("%d\t",H->r[j].key);
printf("\n**************************************\n");
getch();
display_mainmenu( );
return OK;
}
int CreatSequence(SeqList **H)
{
int j,select,length=0;
printf("\n\n Input the record size:");
scanf("%d",&length);
if(length>MAXSIZE)
{
printf("\n\n Size too large.");
return OVERFLOW;
}
(*H)->r=(RecType *)malloc(sizeof(RecType)*length);
if( !((*H)->r) )
{
printf(" Malloc space error!!\n");
return ERROR;
}
(*H)->length=length;
printf("\n\n 1.Forward-sequence.");
printf("\n\n 2.Backward-sequence.");
printf("\n\n 3.Random-sequence.");
select=getch();
switch(select)
{
case '1': for(j=1;j<=length;j++)
(*H)->r[j].key=j;
printf("\n\n...Forward-Sequence created..."); break;
case '2': for(j=1;j<=length;j++)
(*H)->r[j].key=(*H)->length-j;
printf("\n\n...Backward-Sequence created..."); break;
case '3': randomize( );
for(j=1;j<=length;j++)
(*H)->r[j].key=random((*H)->length);
printf("\n\n...Random-Sequence created..."); break;
default: printf("\n\n...Cancel creating Sequence...");
free( (*H)->r ); break;
}
Display(*H);
return OK;
}
/********************************各种排序算法************************************/
void BubbleSort(RecType r[],int n)
{ int i,j,swap;
RecType temp;
for (i=1;i<n;i++)
{ swap=0;
for(j=n-1;j>=i;j--)
{
if (r[j].key>r[j+1].key)
{ temp=r[j]; /* 交换记录 */
r[j]=r[j+1];
r[j+1]=temp;
swap=1; /* 置交换标志为1 */
}
}
if(!swap) break;
}
return ;
}
void SelectSort(RecType r[],int n)
{ int i,j,min;
RecType temp; /* 交换记录的中间变量 */
for (i=1;i<n;i++) /* 共n-1趟(遍) */
{ min=i; /* r[i]为最小记录r[min] */
for (j=i+1;j<=n;j++)
if (r[j].key<r[min].key)
min=j; /* 修改min */
if (min!=i) /* 若r[min]不是r[i] */
{ temp=r[min]; /* 交换r[min]和r[i] */
r[min]=r[i]; r[i]=temp;
}
}
return ;
}
void InsertSort(RecType r[],int n) /* 对数组r[1..n]中的n个记录作插入排序 */
{ int i,j;
for(i=2;i<=n;i++)
{ r[0]=r[i]; /* 待插记录r[i]存入监视哨中 */
j=i-1; /* 已排序的范围1 - i-1 */
/* 从r[i-1]开始向左扫描 */
while(r[0].key<r[j].key)
{ r[j+1]=r[j]; /* 记录后移 */
j--; /* 继续向左扫描 */
}
r[j+1]=r[0]; /* 插入记录r[0],即原r[i] */
}
return ;
}
void BinaryInsertSort(RecType r[],int n) /* 对数组r[1..n]中的n个记录作折半插入排序 */
{ int i,j;
int low,high,m;
for(i=2;i<=n;i++)
{ r[0]=r[i]; /* 待插记录r[i]存入监视哨中 */
low = 1 ; high = i - 1;
while ( low <= high )
{ m = ( low + high ) / 2 ;
if ( r[0].key <r[m].key )
high = m-1; /* 插入位置可能在high之后 */
else low = m+1; /* 插入位置可能在low之前 */
} /* 结束时表示插入在high和low之间 */
for (j = i-1; j>=high+1;--j )
r[j+1] = r[j];
r[high + 1] = r[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -