C语言《数据结构与算法》大作业--------内部排序(包含五种排序方法 代码+截图)

数据结构开发总结报告 ——内部排序综合设计程序的编程实现

实现各个排序算法(直接插入排序,折半插入排序,希尔排序,冒泡排序,简单选择排序)

#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) //对R[0..n-1]按递增有序进行直接插入排序
{
    
    
    int i,j;
    RecType tmp;
    for (i=1; i<n; i++)
    {
    
    
        tmp=R[i];
        j=i-1;            //从右向左在有序区R[0..i-1]中找R[i]的插入位置
        while (j>=0 && tmp.key<R[j].key)
        {
    
    
            R[j+1]=R[j]; //将关键字大于R[i].key的记录后移
            j--;
        }
        R[j+1]=tmp;      //在j+1处插入R[i]
    }
}
//折半插入排序
void InsertSort1(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序
{
    
    
    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);
 }


在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ring__wang/article/details/108768213