开发者社区> 问答> 正文

从键盘输入10个数,编程实现分别用插入排序,交换排序,选择排序算法进行排序,输出排序后的序列。

麻烦给所编的程序进行注释,谢谢。

展开
收起
知与谁同 2018-07-19 17:59:46 4062 0
3 条回答
写回答
取消 提交回答
  • 这个时候,玄酱是不是应该说点什么...
    pascal or c
    2019-07-17 22:50:42
    赞同 展开评论 打赏
  • 胜天半子
    你用的什么编程软件。
    2019-07-17 22:50:42
    赞同 展开评论 打赏
  • #include <stdio.h>

    #include <windows.h>

    #define MAX 255

    int R[MAX];

    void Insert_Sort(int n)

    { /* 对数组R中的记录R[1..n]按递增序进行插入排序  */

        int i,j;

        for(i=2;i<=n;i++) /* 依次插入R[2],…,R[n] */

      if(R[i]<R[i-1])

      {/* 若R[i]大于等于有序区中所有的R,则R[i] */

       /* 应在原有位置上 */

       R[0]=R[i];j=i-1; /* R[0]是哨兵,且是R[i]的副本 */

       do{ /* 从右向左在有序区R[1..i-1]中查找R[i]的插入位置 */

        R[j+1]=R[j]; /* 将关键字大于R[i]的记录后移 */

        j--;

       }while(R[0]<R[j]);  /* 当R[i]≥R[j]时终止 */

       R[j+1]=R[0]; /* R[i]插入到正确的位置上 */

      }

    }

    void Bubble_Sort(int n)

    { /* R(l..n)是待排序的文件,采用自下向上扫描,对R做冒泡排序 */

     int i,j;

     int exchange; /* 交换标志 */

     for(i=1;i<n;i++){ /* 最多做n-1趟排序 */

      exchange=0; /* 本趟排序开始前,交换标志应为假 */

      for(j=n-1;j>=i;j--) /* 对当前无序区R[i..n]自下向上扫描 */

       if(R[j+1]<R[j]){/* 交换记录 */

        R[0]=R[j+1]; /* R[0]不是哨兵,仅做暂存单元 */

        R[j+1]=R[j];

        R[j]=R[0];

        exchange=1; /* 发生了交换,故将交换标志置为真 */

       }

       if(!exchange) /* 本趟排序未发生交换,提前终止算法 */

        return;

     }

    }

    void Select_Sort(int n)

    {

     int i,j,k;

     for(i=1;i<n;i++)

     {/* 做第i趟排序(1≤i≤n-1) */

      k=i;

      for(j=i+1;j<=n;j++) /* 在当前无序区R[i..n]中选key最小的记录R[k] */

       if(R[j]<R[k])

        k=j; /* k记下目前找到的最小关键字所在的位置 */

       if(k!=i)

       { /* 交换R[i]和R[k] */

        R[0]=R[i]; R[i]=R[k]; R[k]=R[0]; /* R[0]作暂存单元 */

       } /* endif */

     } /* endfor */

    } /* end of Select_Sort */

    main()

    {

     int i,n;

     char SelectKey ;

     system("cls");

     puts("Please input total element number of the sequence:");

     scanf("%d",&n);

     if(n<=0||n>MAX)

     {

      printf("n must more than 0 and less than %d.\n",MAX);

      return 0 ;

     }

     puts("Please input the elements one by one:");

     for(i=1;i<=n;i++)

      scanf("%d",&R[i]);

     puts("The sequence you input is:\n");

     for(i=1;i<=n;i++)

      printf("%4d",R[i]);

      printf("\n");

     printf("Please select the sort method!\n");

     do 

     {

     

      puts("=========================");

      puts("|  Please select key:   |");

      puts("|  1. Insert_Sort       |");

      puts("|  2. Bubble_Sort       |");

      puts("|  3. Select_Sort       |");

      puts("=========================");

      SelectKey = getchar();

      SelectKey = getchar();

     }while( SelectKey!='1' && SelectKey!='2' && SelectKey!='3' && SelectKey!='4' );

     switch(SelectKey)

      {

      case '1':Insert_Sort(n); break;

      case '2':Bubble_Sort(n); break;

      case '3':Select_Sort(n); break;

      default: printf("Please input the correct select!");

      }

     

     puts("\nThe sequence after sort is:");

     for(i=1;i<=n;i++)

      printf("%4d",R[i]);

     puts("\n Press any key to quit...");

     getchar();

     getchar();

     return 0 ;

    }

    以前分别写过这些排序 下午花了些时间 把三个程序整合在一起。希望对你有用。是用C写的,可以在VC中运行。运行截图在附件。

    2019-07-17 22:50:42
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
数据+算法定义新世界 立即下载
袋鼠云基于实时计算的反黄牛算法 立即下载
Alink:基于Apache Flink的算法平台 立即下载