数据结构C实现排序:直接插入、归并和快速排序(递增)学号
数据结构(C语言版清华大学出版社)-章课后部分答案

第八章选择题1. C2.A3.B4.C5.D6.B7.B8.A9.D 10.D 11.C 12.C填空题1.n、n+12. 43.8.25( 折半查找所在块 )4.左子树、右子树5.266.顺序、(n+1)/2、O(log2n)7.m-1、[m/2]-18.直接定址应用题1.进行折半查找时,判定树是唯一的,折半查找过程是走了一条从根节点到末端节点的路径,所以其最大查找长度为判定树深度[log2n]+1.其平均查找长度约为[log2n+1]-1.在二叉排序树上查找时,其最大查找长度也是与二叉树的深度相关,但是含有n个节点的二叉排序树不是唯一的,当对n个元素的有序序列构造一棵二叉排序树时,得到的二叉排序树的深度也为n,在该二叉树上查找就演变成顺序查找,此时的最大查找长度为n;在随机情况下二叉排序树的平均查找长度为1+4log2n。
因此就查找效率而言,二分查找的效率优于二叉排序树查找,但是二叉排序树便于插入和删除,在该方面性能更优。
3. 评价哈希函数优劣的因素有:能否将关键字均匀的映射到哈希表中,有无好的处理冲突的方法,哈希函数的计算是否简单等。
冲突的概念:若两个不同的关键字Ki和Kj,其对应的哈希地址Hash(Ki) =Hash(Kj),则称为地址冲突,称Ki和K,j为同义词。
(1)开放定址法(2)重哈希法(3)链接地址法4.(1)构造的二叉排序树,如图(2)中序遍历结果如下:10 12 15 20 24 28 30 35 46 50 55 68(4)平均查找长度如下:ASLsucc = (1x1+2x2+3x3+4x3+5x3)/12 = 41/128.哈希地址如下:H(35) = 35%11 = 2H(67) = 67%11 = 1H(42) = 42%11 = 9H(21) = 21%11 = 10H(29) = 29%11 = 7H(86) = 86%11 = 9H(95) = 95%11 = 7H(47) = 47%11 = 3H(50) = 50%11 = 6H(36) = 36%11 = 3H(91) = 91%11 = 3第九章选择题1. D2.C3.B4.D5.C6.B7.A8.A9.D 10.D填空题1.插入排序、交换排序、选择排序、归并排序2.移动(或者交换)3.归并排序、快速排序、堆排序4.保存当前要插入的记录,可以省去在查找插入位置时的对是否出界的判断5.O(n)、O(log2n)6.直接插入排序或者改进了的冒泡排序、快速排序7.Log2n、n8.完全二叉树、n/29.1510.{12 38 25 35 50 74 63 90}应用题11.(1)Shell排序(步长为5 3 1)每趟的排序结果初始序列为100 87 52 61 27 170 37 45 61 118 14 88 32步长为5的排序14 37 32 61 27 100 87 45 61 118 170 88 52步长为3的排序结果14 27 32 52 37 61 61 45 88 87 170 100 118步长为1的排序结果14 27 32 37 45 52 61 61 87 88 100 118最后结果14 27 32 37 45 52 61 61 87 88 100 118 170(2)快速排序每趟的排序结果如图初始序列100 87 52 61 27 170 37 45 61 118 14 88 32第一趟排序[32 87 52 61 27 88 37 45 61 14]100[118 170]第二趟排序[14 27]32[61 52 88 37 45 61 87]100 118[170]第三趟排序14[27]32[45 52 37]61[88 61 87]100 118[170]第四趟排序14[27]32[37]45[52]61[87 61]88 100 118[170]第五趟排序14[27]32[37]45[52]61[87 61]88 100 118[170]最后结果14[27]32[37]45[52]61[61]87 88 100 118[170](3)二路归并排序每趟的排序结果初始序列[100][87][52][61][27][170][37][45][61][118][14][88][32]第一趟归并[87 100][52 61][27 170][37 45][61 118][14 88][32]第二趟归并[52 61 87 100][27 37 45 170][14 61 88 118][32]第三趟归并排序[27 37 45 52 61 87 100 170][14 32 61 88 118]第四趟归并排序[14 27 32 37 45 52 61 61 87 88 100 118 170]最后结果14 27 32 37 45 52 61 61 87 88 100 118 17012.采用快速排序时,第一趟排序过程中的数据移动如图:算法设计题1.分析:为讨论方便,待排序记录的定义为(后面各算法都采用此定义):#define MAXSIZE 100 /* 顺序表的最大长度,假定顺序表的长度为100 */ typedef int KeyType; /* 假定关键字类型为整数类型 */typedef struct {KeyType key; /* 关键字项 */OtherType other; /* 其他项 */}DataType; /* 数据元素类型 */typedef struct {DataType R[MAXSIZE+1]; /* R[0]闲置或者充当哨站 */int length; /* 顺序表长度 */}sqList; /* 顺序表类型 */设n个整数存储在R[1..n]中,因为前n-2个元素有序,若采用直接插入算法,共要比较和移动n-2次,如果最后两个元素做一个批处理,那么比较次数和移动次数将大大减小。
C++各类排序算法介绍PPT课件

}
r[i]=x;
qksort(r,t,j-1);
2021/2/10
qksort(r,j+1,w);
21
}
– 算法评价 • 时间复杂度 – 最好情况(每次总是选到中间值作枢轴) T(n)=O(nlog2n) – 最坏情况(每次总是选到最小或最大元素 作枢轴)T(n)=O(n² )
T(n)=O(n²)
• 根据“寻找”插入位置的方法不同,插入法可分为:直 插排序、二分插入排序、希尔排序。
• (1) 直接插入排序
– 若将一个未排序的元素L[i]插入到已排序的具有i-1个 元素的序列的适当位置,步骤如下:
• a. 从右向左顺序搜索已排序的序列,若已排序序 列中的元素比L[i]大,则后移一个位置,直至找到 一个元素L[j-1](0≤j-1≤i-1)的关键字值比L[i]的关键 字值小;
• 希尔排序可提高排序速度,因为 – 分组后n值减小,n² 更小,而T(n)=O(n² ),所 以T(n)从总体上看是减小了 – 关键字较小的记录跳跃式前移,在进行最后一 趟增量为1的插入排序时,序列已基本有序
• 增量序列取法 – 无除1以外的公因子 – 最后一个增量值必须为1
2021/2/10
14
关键字小,则可分别对这两部分记录进行排序,以达到 整个序列有序。 关键字通常取第一个记录的值为基准值。
– 排序过程:对r[s……t]中记录进行一趟快速排序,附设两 个指针i和j,设基准值记录rp=r[s],x=rp.key
• 初始时令i=s,j=t
• 首先从j所指位置向前搜索第一个关键字小于x的记录, 并和rp交换
9.3 交换排序
• (0) 基本思想:
– 两两比较待排序的数据元素的关键字,如果发生逆序, 则交换之,直到全部对象都排好序为止。
c#实现的几种排序方法

c#实现的⼏种排序⽅法1.经典排序算法 – 插⼊排序Insertion sort插⼊排序就是每⼀步都将⼀个待排数据按其⼤⼩插⼊到已经排序的数据中的适当位置,直到全部插⼊完毕。
插⼊排序⽅法分直接插⼊排序和折半插⼊排序两种,这⾥只介绍直接插⼊排序,折半插⼊排序留到“查找”内容中进⾏。
图1演⽰了对4个元素进⾏直接插⼊排序的过程,共需要(a),(b),(c)三次插⼊。
public void Sort(int[] arr){for (int i = 1; i < arr.Length; i++){int t = arr[i];int j = i;while ((j > 0) && (arr[j - 1] > t)){arr[j] = arr[j - 1];//交换顺序--j;}arr[j] = t;}}折半排序算法是对直接插⼊算法的⼀种优化,优化的核⼼是:通过折半查看有序数组中间位置的数值(a)与待插⼊的数值(temp)的⼤⼩,如果a>=temp,则转向折半的左区间继续折半查找;如果a<temp,则转向折半后的右区间继续折半查找。
直到左右下标相同时,此时折半的下标也指向相同的位置,再做最后⼀次循环,最终的结果是:左右下标相差1,并且原来左侧的下标指向⼤于temp的位置,原来右侧的下标指向了⼩于temp的位置,即:array[biggerIndex] < temp < array[smallerIndex]。
//折半排序算法(传递待排数组名,即:数组的地址。
故形参数组的各种操作反应到实参数组上)private static void BinaryInsertionSortFunction(int[] array){try{int smallerIndex = 0; //记录有序数组的起始位置int biggerIndex = 0; //记录有序数组的终⽌位置int midIndex = 0; //记录获取有序数组的中间位置(折半法的关键:折半的位置)int temp; //记录带排的数值for (int i = 1; i < array.Length; i++) //循环向有序数组中插⼊数值(i从1开始,因为操作的是同⼀个数组){temp = array[i]; //记录待插⼊有序数组的数值biggerIndex = i - 1;//当smallerIndex==biggerIndex时,进⼊最后⼀次循环:smallerIndex指向⼤于temp的数组位置,biggerIndex指向⼩于temp的数组位置while (smallerIndex <= biggerIndex){midIndex = (smallerIndex + biggerIndex) / 2; //确定折半的位置if(array[midIndex] >= temp) //折半位置的数值 >= temp{biggerIndex = midIndex - 1; //biggerIndex以midIndex为基础向前移动⼀位}else{smallerIndex = midIndex + 1; //smallerIndex以midIndex为基础向后移动⼀位}}for (int j = i - 1; j >biggerIndex; j--) //将有序数组中⼤于temp的数值分别向后移动⼀位{array[j + 1] = array[j]; //}array[biggerIndex + 1] = temp; //将temp插⼊biggerIndex + 1,因为此时array[biggerIndex]<temp<array[smallerIndex]}}catch (Exception ex){ }}2. //选择排序public static void SelectionSort(int[] num){int min, temp;for (int i = 0; i < num.Length-1; i++){min = i;for (int j =i+1; j < num.Length; j++){if (num[j] < num[min]){min = j;}}temp = num[i];num[i] = num[min];num[min] = temp;}}3. //冒泡排序(Bubble Sort)的基本思想是:将相邻的记录的关键码进⾏⽐较,若前⾯记录的关键码⼤于后⾯记录的关键码,则将它们交换,否则不交换。
数据结构课程设报告—各种排序算法的比较

数据结构课程设计报告几种排序算法的演示1、需求分析:运行环境:Microsoft Visual Studio 20052、程序实现功能:3、通过用户键入的数据, 经过程序进行排序, 最后给予数据由小到大的输出。
排序的方式包含教材中所介绍的几种常用的排序方式:直接插入排序、折半插入排序、冒泡排序、快速排序、选择排序、堆排序、归并排序。
每种排序过程中均显示每一趟排序的细节。
程序的输入:输入所需排序方式的序号。
输入排序的数据的个数。
输入具体的数据元素。
程序的输出:输出排序每一趟的结果, 及最后排序结果1、设计说明:算法设计思想:a交换排序(冒泡排序、快速排序)交换排序的基本思想是: 对排序表中的数据元素按关键字进行两两比较, 如果发生逆序(即排列顺序与排序后的次序正好相反), 则两者交换位置, 直到所有数据元素都排好序为止。
b插入排序(直接插入排序、折半插入排序)插入排序的基本思想是: 每一次设法把一个数据元素插入到已经排序的部分序列的合适位置, 使得插入后的序列仍然是有序的。
开始时建立一个初始的有序序列, 它只包含一个数据元素。
然后, 从这个初始序列出发不断插入数据元素, 直到最后一个数据元素插到有序序列后, 整个排序工作就完成了。
c选择排序(简单选择排序、堆排序)选择排序的基本思想是: 第一趟在有n个数据元素的排序表中选出关键字最小的数据元素, 然后在剩下的n-1个数据元素中再选出关键字最小(整个数据表中次小)的数据元素, 依次重复, 每一趟(例如第i趟, i=1, …, n-1)总是在当前剩下的n-i+1个待排序数据元素中选出关键字最小的数据元素, 作为有序数据元素序列的第i个数据元素。
等到第n-1趟选择结束, 待排序数据元素仅剩下一个时就不用再选了, 按选出的先后次序所得到的数据元素序列即为有序序列, 排序即告完成。
d归并排序(两路归并排序)1、两路归并排序的基本思想是: 假设初始排序表有n个数据元素, 首先把它看成是长度为1的首尾相接的n个有序子表(以后称它们为归并项), 先做两两归并, 得n/2上取整个长度为2的归并项(如果n为奇数, 则最后一个归并项的长度为1);再做两两归并, ……, 如此重复, 最后得到一个长度为n的有序序列。
CC++实现快速排序算法的思路及原理解析

CC++实现快速排序算法的思路及原理解析⽬录快速排序2. 实现原理3. 动态演⽰4. 完整代码5. 结果展⽰6. 算法分析快速排序1. 算法思想快速排序的基本思想:通过⼀趟排序将待排记录分隔成独⽴的两部分,其中⼀部分记录的关键字均⽐另⼀部分的关键字⼩,则可分别对这两部分记录继续进⾏排序,以达到整个序列有序。
2. 实现原理2.1、设置两个变量 low、high,排序开始时:low=0,high=size-1。
2.2、整个数组找基准正确位置,所有元素⽐基准值⼩的摆放在基准前⾯,所有元素⽐基准值⼤的摆在基准的后⾯默认数组的第⼀个数为基准数据,赋值给key,即key=array[low]。
因为默认数组的第⼀个数为基准,所以从后⾯开始向前搜索(high–),找到第⼀个⼩于key的array[high],就将 array[high] 赋给 array[low],即 array[low] = array[high]。
(循环条件是 array[high] >= key;结束时 array[high] < key)此时从前⾯开始向后搜索(low++),找到第⼀个⼤于key的array[low],就将 array[low] 赋给 array[high],即 array[high] = array[low]。
(循环条件是 array[low] <= key;结束时 array[low] > key)循环 2-3 步骤,直到 low=high,该位置就是基准位置。
把基准数据赋给当前位置。
2.3、第⼀趟找到的基准位置,作为下⼀趟的分界点。
2.4、递归调⽤(recursive)分界点前和分界点后的⼦数组排序,重复2.2、2.3、2.4的步骤。
2.5、最终就会得到排序好的数组。
3. 动态演⽰4. 完整代码三个函数基准插⼊函数:int getStandard(int array[],int low,int high)(返回基准位置下标)递归排序函数:void quickSort(int array[],int low,int high)主函数:int main()#include <stdio.h>#include <stdlib.h>void display(int* array, int size) {for (int i = 0; i < size; i++) {printf("%d ", array[i]);}printf("\n");}int getStandard(int array[], int i, int j) {// 基准数据int key = array[i];while (i < j) {// 因为默认基准是从左边开始,所以从右边开始⽐较// 当队尾的元素⼤于等于基准数据时,就⼀直向前挪动 j 指针while (i < j && array[j] >= key) {j--;}// 当找到⽐ array[i] ⼩的时,就把后⾯的值 array[j] 赋给它if (i < j) {array[i] = array[j];}// 当队⾸元素⼩于等于基准数据时,就⼀直向后挪动 i 指针while (i < j && array[i] <= key) {i++;}// 当找到⽐ array[j] ⼤的时,就把前⾯的值 array[i] 赋给它if (i < j) {array[j] = array[i];}}// 跳出循环时 i 和 j 相等,此时的 i 或 j 就是 key 的正确索引位置// 把基准数据赋给正确位置array[i] = key;return i;}void QuickSort(int array[], int low, int high) {// 开始默认基准为 lowif (low < high) {// 分段位置下标int standard = getStandard(array, low, high);// 递归调⽤排序// 左边排序QuickSort(array, low, standard - 1);// 右边排序QuickSort(array, standard + 1, high);}}// 合并到⼀起快速排序// void QuickSort(int array[], int low, int high) {// if (low < high) {// int i = low;// int j = high;// int key = array[i];// while (i < j) {// while (i < j && array[j] >= key) {// j--;// }// if (i < j) {// array[i] = array[j];// }// while (i < j && array[i] <= key) {// i++;// }// if (i < j) {// array[j] = array[i];// }// }// array[i] = key;// QuickSort(array, low, i - 1);// QuickSort(array, i + 1, high);// }// }int main() {int array[] = {49, 38, 65, 97, 76, 13, 27, 49, 10};int size = sizeof(array) / sizeof(int);// 打印数据printf("%d \n", size);QuickSort(array, 0, size - 1);display(array, size);// int size = 20;// int array[20] = {0}; // 数组初始化// for (int i = 0; i < 10; i++) { // 数组个数// for (int j = 0; j < size; j++) { // 数组⼤⼩// array[j] = rand() % 1000; // 随机⽣成数⼤⼩ 0~999// }// printf("原来的数组:");// display(array, size);// QuickSort(array, 0, size - 1);// printf("排序后数组:");// display(array, size);// printf("\n");// }return 0;}5. 结果展⽰(递归调⽤,不好展⽰每次排序结果)6. 算法分析时间复杂度:最好: O ( n l o g 2 n ) O(n log_{2} n) O(nlog2n)最坏: O ( n 2 ) O(n^2) O(n2)平均: O ( n l o g 2 n ) O(n log_{2} n) O(nlog2n)空间复杂度: O ( n l o g 2 n ) O(n log_{2} n) O(nlog2n)稳定性:不稳定到此这篇关于C/C++实现快速排序算法的思路及原理解析的⽂章就介绍到这了,更多相关C++实现快速排序算法内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。
c语言链表排序算法

c语言链表排序算法在C语言中,链表的排序可以使用多种算法,如插入排序、归并排序、快速排序等。
以下是一个简单的插入排序算法的示例,用于对链表进行排序:C:#include<stdio.h>#include<stdlib.h>struct Node {int data;struct Node* next;};void insert(struct Node** head, int data) {struct Node* newNode= (struct Node*)malloc(sizeof(struct Node));newNode->data = data;newNode->next = NULL;if (*head == NULL) {*head = newNode;return;}struct Node* current = *head;while (current->next != NULL) {current = current->next;}current->next = newNode;}void sortList(struct Node** head) { struct Node* current = *head;while (current != NULL) {struct Node* next = current->next; while (next != NULL) {if (current->data > next->data) { int temp = current->data;current->data = next->data;next->data = temp;}next = next->next;}current = current->next;}}void printList(struct Node* head) { while (head != NULL) {printf("%d ", head->data);head = head->next;}}int main() {struct Node* head = NULL;insert(&head, 5);insert(&head, 2);insert(&head, 4);insert(&head, 1);insert(&head, 3);printf("Before sorting: ");printList(head);sortList(&head);printf("\nAfter sorting: ");printList(head);return0;}这个程序定义了一个链表节点结构体Node,其中包含一个整型数据data 和一个指向下一个节点的指针next。
数据结构(c言版)课件_第八章_排序_(严蔚敏、吴伟民编_清华大学出版社)

算法描述
算法评价
时间复杂度
记录移动次数
最好情况:0
最坏情况:3(n-1)
比较次数: n1 (n i) 1 (n2 n)
i 1
2
T(n)=O(n²)
空间复杂度:S(n)=O(1)
Ch8_6.c
堆排序
堆的定义:n个元素的序列(k1,k2,……kn),当且仅当 满足下列关系时,称之为堆
增量序列取法 无除1以外的公因子 最后一个增量值必须为1
8.2 交换排序
冒泡排序
排序过程
将第一个记录的关键字与第二个记录的关键字进行比较,若 为逆序r[1].key>r[2].key,则交换;然后比较第二个记录与第 三个记录;依次类推,直至第n-1个记录和第n个记录比较为 止——第一趟冒泡排序,结果关键字最大的记录被安置在最 后一个记录上
二趟排序:13 4 48 38 27 49 55 65 97 76
Ch8_3.c
希尔排序特点
子序列的构成不是简单的“逐段分割”,而是将相隔某个增 量的记录组成一个子序列
希尔排序可提高排序速度,因为 分组后n值减小,n²更小,而T(n)=O(n²),所以T(n)从总体 上看是减小了
关键字较小的记录跳跃式前移,在进行最后一趟增量为1 的插入排序时,序列已基本有序
9776
7163
6257 13
4390 27
3308
38
9173 76
7267 13
6350 27
49 30
49
927 13
7360 27
65 30
65
9370 76
2977 30 76
3初0 9第7 第 第 第 第 第 始一二三四五六 关趟趟趟趟趟趟 键 字
sort c++用法

sort c++用法1、快速排序(Quick Sort)核心思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
C++实现:// 快速排序void quick_sort(int s[], int l, int r){if (l < r){//Swap(s[l], s[(l + r) / 2]); //将中间的这个数和第一个数交换参见注1int i = l, j = r, x = s[l];while (i < j){while(i < j && s[j] >= x) // 从右向左找第一个小于x的数 j--;if(i < j)s[i++] = s[j];while(i < j && s[i] < x) // 从左向右找第一个大于等于x的数i++;if(i < j)s[j--] = s[i];}s[i] = x;quick_sort(s, l, i - 1); // 递归调用quick_sort(s, i + 1, r);}}2、归并排序(Merge Sort)核心思想:将待排序数组(链表)分为若干子序列,每个子序列是有序的,然后再把有序子序列合并为整体有序序列。
C++实现:// 归并排序void merge_sort(int s[], int l, int r){if (l >= r) return ;int mid = (l + r) / 2;merge_sort(s, l, mid);merge_sort(s, mid + 1, r);merge(s, l, mid, r);}void merge(int s[], int l, int m, int r){int i, j, k;int *p = new int[r - l + 1];for (i = m + 1; i > l; i--)a[i - 1] = a[i];for (j = m; j < r; j++)a[r + m - j] = a[j + 1];for (k = l; k <= r; k++){if (a[i] < a[j])p[k] = a[i++];elsep[k] = a[j--];}for (k = l; k <= r; k++)a[k] = p[k];delete []p;}3、堆排序(Heap Sort)核心思想:将待排序的序列构建成一个大顶堆,此时,整个序列的最大值就是堆顶的根节点。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验课题:【用C描述课本的同学】有以下结构体构成的数组:struct StudentInfo{ char ID[10];char * name;float score;}StuInfo[12]={{"0800301105", "JACK", 95},{"0800201505", "LUN", 85},{"0400820115", "MARY", 75.5},{"0400850122", "KATE", 78.9},{"0500201011", "LILI", 88},{"0800401105", "JACK", 96},{"0600830105", "JAN", 98.4},{"0952520012", "SAM", 75},{"9721000045", "OSCAR", 64},{"0700301105", "JACK", 97},{"0458003312", "ZOE", 68.9},{"0400830211", "BOBI", 87.6}};1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。
程序代码:第一种:#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>#define Cutoff (3)struct StudentInfo{ char ID[10];char * name;double score;}StuInfo[12]={{"0800301105", "JACK", 95},{"0800201505", "LUN", 85},{"0400820115", "MARY", 75.5},{"0400850122", "KATE", 78.9},{"0500201011", "LILI", 88},{"0800401105", "JACK", 96},{"0600830105", "JAN", 98.4},{"0952520012", "SAM", 75},{"0721000045", "OSCAR", 64},{"0700301105", "JACK", 97},{"0458003312", "ZOE", 68.9},{"0400830211", "BOBI", 87.6} ,};void InsertionSort(struct StudentInfo A[],int N){int j,p;struct StudentInfo Tmp;for(p=1;p<N;p++){Tmp = A[p];for(j=p; j>0&&strcmp(A[j-1].ID,Tmp.ID)>0 ; j--){A[j]=A[j-1];}A[j]=Tmp;}}void InsertionSort1(struct StudentInfo A[],int N){int j,p;struct StudentInfo Tmp;for(p=1;p<N;p++){Tmp = A[p];for(j=p; j>0&&strcmp(A[j-1].name,)>0 ; j--){A[j]=A[j-1];}A[j]=Tmp;}}void Merge(struct StudentInfo A[],struct StudentInfo TmpArray[],int Lpos,int Rpos,int RightEnd) {int i,LeftEnd,NumElements,TmpPos;LeftEnd=Rpos-1;TmpPos=Lpos;NumElements=RightEnd-Lpos+1;while(Lpos<=LeftEnd && Rpos<=RightEnd){if(strcmp(A[Lpos].name,A[Rpos].name)<=0){TmpArray[TmpPos++]=A[Lpos++];}else{TmpArray[TmpPos++]=A[Rpos++];}}while(Lpos<=LeftEnd){TmpArray[TmpPos++]=A[Lpos++];}while(Rpos<=RightEnd){TmpArray[TmpPos++]=A[Rpos++];}for(i=0;i<NumElements;i++,RightEnd--){A[RightEnd]=TmpArray[RightEnd];}}void MSort(struct StudentInfo A[],struct StudentInfo TmpArray[],int Left,int Right) {int Center;if(Left<Right){Center=(Left+Right)/2;MSort(A,TmpArray,Left,Center);MSort(A,TmpArray,Center+1,Right);Merge(A,TmpArray,Left,Center+1,Right);}}void Mergesort(struct StudentInfo A[],int N){struct StudentInfo *TmpArray;TmpArray=malloc(N*sizeof(struct StudentInfo));if(TmpArray !=NULL){MSort(A,TmpArray,0,N-1);free(TmpArray);}else{printf("No space for tmp array!!");}}void Swap(struct StudentInfo A[],struct StudentInfo B[]){struct StudentInfo *Tmp;Tmp=A;A=B;B=Tmp;}struct StudentInfo Median3(struct StudentInfo A[],int Left,int Right) {struct StudentInfo Tmp;int Center=(Left+Right)/2;if(strcmp(A[Left].name,A[Center].name)>0){Swap(&A[Left],&A[Center]);}if(strcmp(A[Left].name,A[Right].name)>0){Swap(&A[Left],&A[Right]);}if(strcmp(A[Center].name,A[Right].name)>0){Swap(&A[Center],&A[Right]);}Swap(&A[Center],&A[Right-1]);return A[Right-1];}void Qsort(struct StudentInfo A[],int Left,int Right){int i,j;struct StudentInfo Pivot,Tmp;if(Left+Cutoff<=Right){Pivot=Median3(A,Left,Right);i=Left;j=Right-1;for(;;){while(strcmp(A[++i].name,)<0){}while(strcmp(A[--j].name,)>0){}if(i<j){Swap(&A[i],&A[j]);Tmp=A[i];A[i]=A[j];A[j]=Tmp;}elsebreak;}Swap(&A[i],&A[Right-1]);Qsort(A,Left,i-1);Qsort(A,i+1,Right);}{InsertionSort1(A+Left,Right-Left+1);}}void Quicksort(struct StudentInfo A[],int N){Qsort(A,0,N-1);}第二种:void main(){int i=0;InsertionSort(StuInfo,12);for(i=0;i<12;i++){printf("%s,%s,%0.1f\n",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);}printf("\n\n");Mergesort(StuInfo,12);for(i=0;i<12;i++){printf("%s,%s,%0.1f\n",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);}printf("\n\n");Quicksort(StuInfo,12);for(i=0;i<12;i++){printf("%s,%s,%0.1f\n",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);}}#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct StudentInfo ElementType;struct StudentInfo{char ID[11];char *name;double score;}StuInfo[12]={{"0800301105","JACK",95},{"0800201505","LUN",85},{"0400820115","MARY",75.05},{"0400850122","KATE",78.9},{"0500201011","LILI",88},{"0800401105","JACK",96},{"0600830105","JAN",98.4},{"0952520012","SAM",75},{"9721000045","OSCAR",64},{"0700301105","JACK",97},{"0458003312","ZOE",68.9},{"0400830211","BOBI",87.6}};void InsertionSort(ElementType A[],int N){int j,P;ElementType Tmp;for(P=1;P<N;P++){Tmp=A[P];for(j=P;j>0&&strcmp(A[j-1].ID,Tmp.ID)>0;j--)A[j]=A[j-1];A[j]=Tmp;}}void InsertionSort2(ElementType A[],int N){int j,P;ElementType Tmp;for(P=1;P<N;P++){Tmp=A[P];for(j=P;j>0&&strcmp(A[j-1].name,)>0;j--)A[j]=A[j-1];A[j]=Tmp;}}void Merge (ElementType A[],ElementType TmpArray[],int Lpos,int Rpos,int RightEnd) {int i,LeftEnd,NumElements,Tmpos;LeftEnd=Rpos-1;Tmpos=Lpos;NumElements=RightEnd-Lpos+1;while(Lpos<=LeftEnd&&Rpos<=RightEnd){if(strcmp(A[Lpos].name,A[Rpos].name)<=0)TmpArray[Tmpos++]=A[Lpos++];elseTmpArray[Tmpos++]=A[Rpos++];}while(Lpos<=LeftEnd)TmpArray[Tmpos++]=A[Lpos++];while(Rpos<=RightEnd)TmpArray[Tmpos++]=A[Rpos++];for(i=0;i<NumElements;i++,RightEnd--)A[RightEnd]=TmpArray[RightEnd];}void MSort(ElementType A[],ElementType TmpArray[],int Left,int Right){int center;if(Left<Right){center=(Left+Right)/2;MSort(A,TmpArray,Left,center);MSort(A,TmpArray,center+1,Right);Merge(A,TmpArray,Left,center+1,Right);}}void Mergsort(ElementType A[],int N){ElementType *TmpArray;TmpArray=(ElementType*)malloc(N*sizeof(ElementType));if(TmpArray!=NULL){MSort(A,TmpArray,0,N-1);free(TmpArray);}elseprintf("No space for tmp array!!");}void Swap(ElementType *A,ElementType *B){ElementType XX;XX=*A;*A=*B;*B=XX;}ElementType Median3(ElementType A[],int Left,int Right){int Center=(Left+Right)/2;if(strcmp(A[Left].name,A[Center].name)>0)Swap(&A[Left],&A[Center]);if(strcmp(A[Left].name,A[Right].name)>0)Swap(&A[Left],&A[Right]);if(strcmp(A[Center].name,A[Right].name)>0)Swap(&A[Center],&A[Right]);Swap(&A[Center],&A[Right-1]);return A[Right-1];}#define Cutoff 3void Qsort(ElementType A[],int Left,int Right){int i,j;ElementType Pivot;if(Left+Cutoff<=Right){Pivot=Median3(A,Left,Right);i=Left;j=Right-1;for(;;){while(strcmp(A[++i].name,)<0){}while(strcmp(A[--j].name,)>0){}if(i<j)Swap(&A[i],&A[j]);elsebreak;}Swap(&A[i],&A[Right-1]);Qsort(A,Left,i-1);Qsort(A,i+1,Right);}elseInsertionSort2(A+Left,Right-Left+1);}void PrintInfo(){int i;printf(" 学号ID 姓名分数\n");for(i=0;i<12;i++){printf("%-15s",StuInfo[i].ID);printf("%-10s",StuInfo[i].name);printf("%.1f\n",StuInfo[i].score);}}void main(){printf("原顺序为:\n\n");PrintInfo();printf("用插入法排序为:\n\n");InsertionSort(StuInfo,12);PrintInfo();printf("用归并法排序为:\n\n");Mergsort(StuInfo,12);PrintInfo();printf("用快速法排序为:\n\n");Qsort(StuInfo,0,11);PrintInfo();}。