实验五附加

简介: 六、 源程序(带注释) #include #include #include #define MAX 100  //定义数组的长度 #define M  5  //希尔排序的趟数 int sort[MAX];  //定义要排序的数组 i...

六、 源程序(带注释)

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#define MAX 100  //定义数组的长度

#define M  5  //希尔排序的趟数

int sort[MAX];  //定义要排序的数组

int move,compare;//移动次数,比较次数

/*********************起泡排序***************************/

void Bubble_Sort()

{

       int sort1[MAX];

       int temp,i;

       move=0,compare=0;//初始化移动次数,比较次数

       for(i=0;i<MAX;i++)       sort1[i]=sort[i];

       printf("*******起泡排序*******/n");

       for(i=1;i<=MAX-1;i++)

              for(int j=0;j<MAX-i;j++)

              {     compare++;

                     if(sort1[j]>sort1[j+1])

                     {temp=sort1[j];sort1[j]=sort1[j+1];sort1[j+1]=temp;move+=3;}

              }

       printf("排序结果为:/n");

       for(i=0;i<MAX;i++)       {printf("%5d",sort1[i]);if((i+1)%10==0) putchar('/n');}

       printf("/n比较次数为:%d/n移动次数为:%d/n",compare,move);

}

/*******************直接插入排序*************************/

void InsertSort()

{     int sort1[MAX];

       int i,temp,j;

       move=0,compare=0;//初始化移动次数,比较次数

       for(i=0;i<MAX;i++)       sort1[i]=sort[i];

       printf("*******直接插入排序*******/n");

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

       {     compare++;

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

              {     temp=sort1[i];

                     sort1[i]=sort1[i-1];

                     move+=2;

                     for(j=i-2;j>=0;j--)

                     {     compare++;

                            if(temp<sort1[j])

                            {   sort1[j+1]=sort1[j];

                                   move++;

                            }

                            else

                            {     sort1[j+1]=temp;

                                   move++;

                                   break;

                            }

                     }

                     if(j<0)

                     {sort1[0]=temp; move++;}

              }

       }

       printf("排序结果为:/n");

       for(i=0;i<MAX;i++)       {printf("%5d",sort1[i]);if((i+1)%10==0)putchar('/n');}

       printf("/n比较次数为:%d/n移动次数为:%d",compare,move);

}

/********************简单选择排序****************************/

int selectminkey(int sort1[],int i)

{     int min=i;

       for(int j=i+1;j<MAX;j++)

       {     compare++;

              if(sort1[min]>sort1[j])   min=j;

       }

       return min;

}

void SelectSort()//简单选择排序

{     int sort1[MAX];

       int i,j,temp;

       move=0,compare=0;//初始化移动次数,比较次数

       for(i=0;i<MAX;i++)       sort1[i]=sort[i];

       printf("*******简单选择排序*******/n");

       for(i=0;i<MAX-1;i++)

       {     j=selectminkey(sort1,i);

              if(i!=j)

              {     temp=sort1[i];

                     sort1[i]=sort1[j];

                     sort1[j]=temp;

                     move+=3;

              }

       }

       printf("排序结果为:/n");

       for(i=0;i<MAX;i++)       {printf("%5d",sort1[i]);if((i+1)%10==0)putchar('/n');}

       printf("/n比较次数为:%d/n移动次数为:%d",compare,move);

}

/***********************快速排序**************************/

int partition(int sort1[],int low,int high)

{

       int temp;

       temp=sort1[low];

       move++;

       while(low<high)

       {

              while(low<high&&sort1[high]>=temp)

              {compare++;--high;}

              compare++;

              sort1[low]=sort1[high];

              move++;

              while(low<high&&sort1[low]<=temp)

              {compare++;++low;}

              compare++;

              sort1[high]=sort1[low];

              move++;

       }

       sort1[low]=temp;

       move++;

       return low;

}

void QuickSort(int sort1[],int low,int high)

{     int pivotloc;

       if(low<high)

       {     pivotloc=partition(sort1,low,high);

              QuickSort(sort1,low,pivotloc-1);

              QuickSort(sort1,pivotloc+1,high);

       }

}

void PrintfQuickSort()

{

       int i;

    int sort1[MAX];

       move=0,compare=0;//初始化移动次数,比较次数

    printf("*******快速排序*******/n");

       for(i=0;i<MAX;i++)       sort1[i]=sort[i];

       QuickSort(sort1,0,(MAX-1));

       printf("排序结果为:/n");

       for(i=0;i<MAX;i++)       {printf("%5d",sort1[i]);if((i+1)%10==0)putchar('/n');}

       printf("/n比较次数为:%d/n移动次数为:%d",compare,move);

}

/*******************希尔排序************************/

void ShellInsert(int sort1[],int dk)

{     int sentinel;//哨兵

       for(int i=dk;i<MAX;i++)

       {     compare++;

              if(sort1[i]<sort1[i-dk])

              {     move++;

                     sentinel=sort1[i];

                     for(int j=i-dk;j>=0;j-=dk)

                     {     compare++;

                            if(sentinel<sort1[j])

                            {     move++;

                                   sort1[j+dk]=sort1[j];

                            }

                            else break;

                     }

                     move++;

                     sort1[j+dk]=sentinel;

              }

       }

}

void ShellSort()//希尔排序

{     int sort1[MAX];

       int dlta[M];  //增量序列

       int temp=1,i;

       move=0,compare=0;//初始化移动次数,比较次数

       for(i=0;i<MAX;i++)       sort1[i]=sort[i];

       printf("*******希尔排序*******/n");

       dlta[0]=1;

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

       {     dlta[i]=temp+1;

              temp*=2;

       }

       for(i=M-1;i>=0;i--)

              ShellInsert(sort1,dlta[i]);

       printf("排序结果为:/n");

       for(i=0;i<MAX;i++)       {printf("%5d",sort1[i]);if((i+1)%10==0)putchar('/n');}

       printf("/n比较次数为:%d/n移动次数为:%d",compare,move);

}

/********************堆排序***********************/

void HeapAdjust(int sort1[], int i,int n)

{     int j, k;

       int flag=1;

       j=2*i;

       k=sort1[i];

       move++;

       while (j<=n&&flag==1)

       {     if (j<n&& sort1[j] < sort1[j+1])

              {     compare++;

                     if(sort1[j] < sort1[j+1])  j++;

              }

              compare++;

              if (k >= sort1[j])  flag = 0;

              else

              {     move++;

                     sort1[j/2] = sort1[j];

                     j *= 2;

              }

       }

       move++;

       sort1[j/2] = k;

}

void HeapSort()//堆排序

{     int i;

       int sort1[MAX];

       int temp;

       move=0,compare=0;//初始化移动次数,比较次数

       for(i=0;i<MAX;i++)       sort1[i]=sort[i];

       for (i=MAX/2;i>=0;i--)  HeapAdjust(sort1,i,MAX-1);

 

       for (i=MAX-1;i>0;i--)

       {     move+=3;

              temp= sort1[i];

              sort1[i] = sort1[0];

              sort1[0] = temp;

              HeapAdjust(sort1, 0, i-1);

       }

       printf("*******堆排序*******/n");

       printf("排序结果为:/n");

       for(i=0;i<MAX;i++)       {printf("%5d",sort1[i]);if((i+1)%10==0)putchar('/n');}

       printf("/n比较次数为:%d/n移动次数为:%d",compare,move);

}

/********************菜单函数*********************/

int menu()

{

           int d;

 printf("/n选择排序方法:");

              printf("1:   起泡排序/n");

              printf("2:   直接插入排序/n");

              printf("3:   简单选择排序/n");

              printf("4:   快速排序/n");

              printf("5:   希尔排序/n");

              printf("6:   堆排序/n");

           printf("7:   退出....../n");

              scanf("%d",&d);

              return d;

}

/********************主函数***********************/

void main()

{

       int quit=0;

       int i;

       printf("要排序的随机数组是:/n");

       srand((unsigned)time(NULL));/*用随机函数产生数组*/

       for(i=0;i<MAX;i++)

       {     sort[i]=rand()%199;

              printf("%5d",sort[i]);

              if((i+1)%10==0)putchar('/n');

       }

while(!quit)    

              switch(menu())

              {

              case 1:Bubble_Sort();break;//起泡排序

              case 2:InsertSort();break;//直接插入排序

              case 3:SelectSort();break;//简单选择排序

              case 4:PrintfQuickSort();break;//快速排序

              case 5:ShellSort();break; //希尔排序

              case 6:HeapSort();break;//堆排序

              case 7:quit=1;//退出程序

              }

}

目录
相关文章
|
JSON Serverless API
Agent平台快速体验
通过《智能体(Agent)平台介绍》您了解到Agent及其构建平台的价值,通过《在函数计算上部署专属的Agent平台》你获得了专属的Agent平台,本篇进入更加激动人心的使用阶段,一起探索Agent的魅力吧
1938 6
Agent平台快速体验
|
SQL 缓存 JSON
vue利用级联选择器实现全国省市区乡村五级菜单联动
vue利用级联选择器实现全国省市区乡村五级菜单联动
|
缓存 Shell Linux
【Shell 命令集合 链接器(linker)工具】Linux ld命令 将目标文件与库链接为可执行文件或库文件
【Shell 命令集合 链接器(linker)工具】Linux ld命令 将目标文件与库链接为可执行文件或库文件
1040 0
|
人工智能 自然语言处理 数据可视化
2025年适合大型企业的BI产品及企业高效运用BI工具指南
本文将聚焦适合大型企业的BI产品选型,深入解析主流产品优势,并结合实践经验探讨企业如何用好BI工具,为各行业企业提供专业参考。
|
11月前
|
存储 安全 API
隐私合规红线不能碰:大模型微调3大重灾区防护手册
本文聚焦大模型微调中训练数据、中间产物与部署链路三大隐私泄露重灾区,剖析90%开发者易踩的技术陷阱,从分层脱敏、差分隐私到权限管控,提供全链路可落地的防护方案,并结合性能与安全双重验证,助力企业实现合规与效能双赢。
隐私合规红线不能碰:大模型微调3大重灾区防护手册
|
IDE 搜索推荐 程序员
《CodeBuddy:像哆啦A梦一样智能的编程助手》
本文介绍腾讯云代码助手CodeBuddy——智能编程伙伴,宛如哆啦A梦般的存在。它具备智能辅助、个性化学习、多场景适配等优势,支持主流IDE与多种编程语言,保护代码隐私并开源透明。通过上下文理解、实时错误检测等功能提升开发效率;根据编码风格优化建议,构建知识图谱。下载链接提供,安装后即可在IDE中使用,助你成为更高效的开发者。
2038 17
《CodeBuddy:像哆啦A梦一样智能的编程助手》
|
JavaScript Java 应用服务中间件
|
传感器
示例四、 开关式霍尔传感器测转速
示例四、 开关式霍尔传感器测转速
1587 0
示例四、 开关式霍尔传感器测转速
|
存储 关系型数据库 MySQL
B树和B+树的区别
B树和B+树的区别
923 1