数据结构开发总结报告 ——内部排序综合设计程序的编程实现
#include<stdio.h>
#define MaxSize 20
#include<windows.h>
typedef int KeyType;
typedef struct
{
KeyType key;
} RecType;
void cd();
void InsertSort(RecType R[],int n);
void out(RecType R[],int n);
void ShellSort(RecType R[],int n);
void BubbleSortl(RecType R[],int n);
void SelectSort(RecType R[],int n);
void Pos(int x, int y);
int mup();
int main(){
int s,v,i,n=10;
mup();
Pos(35,13);
printf("请输入你要的操作 ");
scanf("%d", &v);
if(v==1){
cd();
RecType R[]={12,8,44,6,73,4,54,2,1,9};
Pos(40,38);
scanf("%d", &s);
while (s != 0) {
switch (s)
{
case 1:Pos(30,40);printf(" 1,直接插入排序\n");InsertSort(R,n);out(R,n);break;
case 2:Pos(30,43);printf(" 2,折半插入排序\n"); InsertSort(R,n);out(R,n); break;
case 3:Pos(30,46);printf(" 3,希尔排序\n"); ShellSort(R,n);out(R,n);break;
case 4:Pos(30,49);printf(" 4,冒泡排序\n"); BubbleSortl(R,n);out(R,n); break;
case 5:Pos(30,52);printf(" 5,简单选择排序\n");SelectSort(R,n);out(R,n); break;
default: Pos(32,55);printf("请输入正确的数字\n"); break;
}
Pos(40,42+s*2.5);
scanf("%d", &s);
}
}
if(v==2)
printf("退出\n");
}
void cd(){
int i;
for(i = 20;i<78;i+=2)
{
Pos(i, 20);
printf("■");
Pos(i, 37);
printf("■");
}
for (i = 20; i<38; i++)
{
Pos(20, i);
printf("■");
Pos(76, i);
printf("■\n");
}
Pos(30,21);
printf("---------内排序系统---------\n");
Pos(35,23);
printf("1,直接插入排序\n");
Pos(35,25);
printf("2,折半插入排序\n");
Pos(35,27);
printf("3,希尔排序\n");
Pos(35,29);
printf("4,冒泡排序\n");
Pos(35,31);
printf("5,简单选择排序\n");
Pos(35,33);
printf("0,退出系统\n");
Pos(35,35);
printf("请输入你要的操作\n");}
void InsertSort(RecType R[],int n)
{
int i,j;
RecType tmp;
for (i=1; i<n; i++)
{
tmp=R[i];
j=i-1;
while (j>=0 && tmp.key<R[j].key)
{
R[j+1]=R[j];
j--;
}
R[j+1]=tmp;
}
}
void InsertSort1(RecType R[],int n)
{
int i,j,low,high,mid;
RecType tmp;
for (i=1; i<n; i++)
{
tmp=R[i];
low=0;
high=i-1;
while (low<=high)
{
mid=(low+high)/2;
if (tmp.key<R[mid].key)
high=mid-1;
else
low=mid+1;
}
for (j=i-1; j>=high+1; j--)
R[j+1]=R[j];
R[high+1]=tmp;
}
}
void ShellSort(RecType R[],int n){
int i,j,d;
RecType tmp;
d=n/2;
while(d>0){
for(i=d;i<n;i++){
tmp=R[i];
j=i-d;
while(j>=0&&tmp.key<R[j].key){
R[j+d]=R[j];
j=j-d;
}
R[j+d]=tmp;
}
d=d/2;
}
}
void BubbleSortl(RecType R[],int n){
int i,j,t;
bool exchange;
for(i=0;i<n-1;i++){
exchange=false;
for(j=n-1;j>i;j--)
if(R[j].key<R[j-1].key){
t=R[j].key;
R[j].key=R[j-1].key;
R[j-1].key=t;
exchange=true;
}
if(!exchange)
return;
}
}
void SelectSort(RecType R[],int n){
int i,j,k,t;
for (i=0;i<n-1;i++){
k=i;
for(j=i+1;j<n;j++)
if(R[j].key<R[k].key)
k=j;
if(k!=i){
t=R[i].key;
R[i].key=R[k].key;
R[k].key=t;
}
}
}
void out(RecType R[],int n){
int i;
printf("\t\t\t\t");
for (i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
}
int mup()
{
int i;
Pos(30,5);
printf(" ----------内排序系统--------------\n");
Pos(30,7);
printf(" ----------1进入内排序系统---------\n");
Pos(30,9);
printf(" ----------2退出-------------------\n");
for(i = 20;i<78;i+=2)
{
Pos(i, 0);
printf("■");
Pos(i, 15);
printf("■");
}
for (i = 1; i<16; i++)
{
Pos(20, i);
printf("■");
Pos(76, i);
printf("■\n");
}
}
void Pos(int x, int y)
{
COORD pos;
HANDLE hOutput;
pos.X = x;
pos.Y = y;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput, pos);
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2266b3cac272634f2b3f8fde3cc5f004.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c3aacedfc223e075dfeba07c03f255e1.png)