实验5 附加

本文详细介绍了六种经典排序算法:起泡排序、直接插入排序、简单选择排序、快速排序、希尔排序和堆排序,并通过C语言实现这些算法。每种算法都附有详细的注释说明,便于读者理解其工作原理及步骤。

六、 源程序(带注释)

#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;//退出程序

              }

}

### 计算机网络 ICMP 实验教程 #### 1. 实验目的 本实验旨在通过实际操作加深对 Internet 控制消息协议 (ICMP) 的理解和应用。具体来说,将学习如何利用 ICMP 协议发送请求以及接收响应来测试主机之间的连通性。 #### 2. 实验环境搭建 为了完成此实验,在模拟环境中设置了一个简单的局域网结构[^1]: - **设备**:PC 和服务器各一台; - **连接方式**:两者经由路由器相连; - 使用 Cisco Packet Tracer 或其他类似软件构建上述拓扑,并确保所有组件正常工作; #### 3. ICMP 请求与应答过程分析 当从客户端发出 `ping` 指令时,会触发一系列事件[^2]: - 客户端向目标地址发起 Echo Request(回声请求),即所谓的 "Ping" 动作; - 如果路径畅通无阻,则远程机器收到该请求后返回一个 Echo Reply(回应回声)给源节点; - 这一交互过程中产生的数据包可以通过抓取工具观察其具体内容; ```bash # 在 Linux/Unix 系统上运行 ping 命令的例子 $ ping www.example.com PING www.example.com (93.184.216.34): 56 data bytes 64 bytes from 93.184.216.34: icmp_seq=0 ttl=57 time=12.3 ms ... ``` 以上命令展示了基本的 Ping 测试流程及其输出格式。每次成功的往返通信都会显示 TTL(生存时间)、延迟时间和序列号等信息。 #### 4. 数据捕捉与解析 借助于像 Wireshark 或者内置于仿真平台中的 Sniffer 工具,能够实时监控并记录下整个交流期间所交换的数据帧。对于每一个捕获到的 ICMP 报文而言,重要字段包括但不限于版本号、头部长度、服务类型、总长度、标识符、标志位、片偏移量、TTL、校验和及选项列表等等。 #### 5. 结果讨论 通过对收集来的样本进行深入剖析,可以得出关于当前链路质量的一些结论,比如是否存在丢包现象、平均延时是多少等问题。这些指标有助于评估网络性能状况并定位潜在故障点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值