用c语言写出比较直接插入排序,快速排序,希尔排序,冒泡排序,简单选择排序,堆排序,归并排序,基数排序的关键字比较次数和关键字移动次数的代码,要求待排序的表长不能小于100;其中的数据要用伪随机数生成程序产生至少要用5组不同的输入数据比较;代码要给出注释

时间: 2024-03-17 18:46:05 浏览: 20
以下是使用C语言实现各种排序算法的关键字比较次数和关键字移动次数的代码。其中,数据使用伪随机数生成程序产生,共使用了5组不同的输入数据进行比较,排序算法包括直接插入排序、快速排序、希尔排序、冒泡排序、简单选择排序、堆排序、归并排序、基数排序。 ``` #include <stdio.h> #include <stdlib.h> #include <time.h> // 生成伪随机数 int* generateRandomArray(int length) { int* arr = (int*)malloc(sizeof(int) * length); srand((unsigned)time(NULL)); for (int i = 0; i < length; i++) { *(arr + i) = rand() % 10000; } return arr; } // 直接插入排序 void straightInsertionSort(int* arr, int length, int* compareCount, int* moveCount) { int temp, j; for (int i = 1; i < length; i++) { temp = *(arr + i); j = i - 1; (*compareCount)++; while (j >= 0 && temp < *(arr + j)) { *(arr + j + 1) = *(arr + j); j--; (*moveCount)++; (*compareCount)++; } *(arr + j + 1) = temp; (*moveCount)++; } } // 快速排序 void quickSort(int* arr, int start, int end, int* compareCount, int* moveCount) { if (start >= end) { return; } int i = start, j = end, pivot = *(arr + start); while (i < j) { (*compareCount)++; while (i < j && *(arr + j) >= pivot) { j--; (*compareCount)++; } if (i < j) { *(arr + i) = *(arr + j); i++; (*moveCount)++; } (*compareCount)++; while (i < j && *(arr + i) < pivot) { i++; (*compareCount)++; } if (i < j) { *(arr + j) = *(arr + i); j--; (*moveCount)++; } } *(arr + i) = pivot; (*moveCount)++; quickSort(arr, start, i - 1, compareCount, moveCount); quickSort(arr, i + 1, end, compareCount, moveCount); } // 希尔排序 void shellSort(int* arr, int length, int* compareCount, int* moveCount) { int temp, gap = length / 2; while (gap > 0) { for (int i = gap; i < length; i++) { temp = *(arr + i); int j = i - gap; (*compareCount)++; while (j >= 0 && *(arr + j) > temp) { *(arr + j + gap) = *(arr + j); j -= gap; (*moveCount)++; (*compareCount)++; } *(arr + j + gap) = temp; (*moveCount)++; } gap /= 2; } } // 冒泡排序 void bubbleSort(int* arr, int length, int* compareCount, int* moveCount) { for (int i = 0; i < length - 1; i++) { for (int j = 0; j < length - 1 - i; j++) { (*compareCount)++; if (*(arr + j) > *(arr + j + 1)) { int temp = *(arr + j); *(arr + j) = *(arr + j + 1); *(arr + j + 1) = temp; (*moveCount) += 3; } } } } // 简单选择排序 void selectionSort(int* arr, int length, int* compareCount, int* moveCount) { for (int i = 0; i < length - 1; i++) { int minIndex = i; for (int j = i + 1; j < length; j++) { (*compareCount)++; if (*(arr + j) < *(arr + minIndex)) { minIndex = j; } } if (minIndex != i) { int temp = *(arr + i); *(arr + i) = *(arr + minIndex); *(arr + minIndex) = temp; (*moveCount) += 3; } } } // 堆排序 void heapSort(int* arr, int length, int* compareCount, int* moveCount) { // 构建初始堆 for (int i = length / 2 - 1; i >= 0; i--) { int parent = i, child = 2 * i + 1, temp = *(arr + i); while (child < length) { (*compareCount)++; if (child < length - 1 && *(arr + child) < *(arr + child + 1)) { child++; } (*compareCount)++; if (temp >= *(arr + child)) { break; } *(arr + parent) = *(arr + child); (*moveCount)++; parent = child; child = 2 * parent + 1; } *(arr + parent) = temp; (*moveCount)++; } // 调整堆 for (int i = length - 1; i > 0; i--) { int temp = *(arr + i); *(arr + i) = *arr; *arr = temp; (*moveCount) += 3; int parent = 0, child = 1; while (child < i) { (*compareCount)++; if (child < i - 1 && *(arr + child) < *(arr + child + 1)) { child++; } (*compareCount)++; if (temp >= *(arr + child)) { break; } *(arr + parent) = *(arr + child); (*moveCount)++; parent = child; child = 2 * parent + 1; } *(arr + parent) = temp; (*moveCount)++; } } // 归并排序 void mergeSort(int* arr, int left, int right, int* compareCount, int* moveCount) { if (left >= right) { return; } int mid = (left + right) / 2; mergeSort(arr, left, mid, compareCount, moveCount); mergeSort(arr, mid + 1, right, compareCount, moveCount); int* tempArr = (int*)malloc(sizeof(int) * (right - left + 1)); int i = left, j = mid + 1, k = 0; while (i <= mid && j <= right) { (*compareCount)++; if (*(arr + i) <= *(arr + j)) { *(tempArr + k) = *(arr + i); i++; } else { *(tempArr + k) = *(arr + j); j++; } k++; } while (i <= mid) { *(tempArr + k) = *(arr + i); i++; k++; } while (j <= right) { *(tempArr + k) = *(arr + j); j++; k++; } for (int m = 0; m < k; m++) { *(arr + left + m) = *(tempArr + m); (*moveCount)++; } free(tempArr); } // 基数排序 void radixSort(int* arr, int length, int* compareCount, int* moveCount) { int max = *arr; for (int i = 1; i < length; i++) { (*compareCount)++; if (*(arr + i) > max) { max = *(arr + i); } } int exp = 1; int* tempArr = (int*)malloc(sizeof(int) * length); while (max / exp > 0) { int bucket[10] = {0}; for (int i = 0; i < length; i++) { bucket[(*(arr + i) / exp) % 10]++; } for (int i = 1; i < 10; i++) { bucket[i] += bucket[i - 1]; } for (int i = length - 1; i >= 0; i--) { tempArr[bucket[(*(arr + i) / exp) % 10] - 1] = *(arr + i); bucket[(*(arr + i) / exp) % 10]--; } for (int i = 0; i < length; i++) { *(arr + i) = *(tempArr + i); (*moveCount)++; } exp *= 10; } free(tempArr); } int main() { int length = 100; int* arr1 = generateRandomArray(length); int* arr2 = generateRandomArray(length); int* arr3 = generateRandomArray(length); int* arr4 = generateRandomArray(length); int* arr5 = generateRandomArray(length); int compareCount, moveCount; compareCount = 0; moveCount = 0; straightInsertionSort(arr1, length, &compareCount, &moveCount); printf("直接插入排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); compareCount = 0; moveCount = 0; quickSort(arr2, 0, length - 1, &compareCount, &moveCount); printf("快速排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); compareCount = 0; moveCount = 0; shellSort(arr3, length, &compareCount, &moveCount); printf("希尔排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); compareCount = 0; moveCount = 0; bubbleSort(arr4, length, &compareCount, &moveCount); printf("冒泡排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); compareCount = 0; moveCount = 0; selectionSort(arr5, length, &compareCount, &moveCount); printf("简单选择排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); compareCount = 0; moveCount = 0; heapSort(arr1, length, &compareCount, &moveCount); printf("堆排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); compareCount = 0; moveCount = 0; mergeSort(arr2, 0, length - 1, &compareCount, &moveCount); printf("归并排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); compareCount = 0; moveCount = 0; radixSort(arr3, length, &compareCount, &moveCount); printf("基数排序:关键字比较次数:%d,关键字移动次数:%d\n", compareCount, moveCount); free(arr1); free(arr2); free(arr3); free(arr4); free(arr5); return 0; } ```

相关推荐

最新推荐

recommend-type

ansys maxwell

ansys maxwell
recommend-type

matlab基于不确定性可达性优化的自主鲁棒操作.zip

matlab基于不确定性可达性优化的自主鲁棒操作.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

导入numpy库,创建两个包含9个随机数的3*3的矩阵,将两个矩阵分别打印出来,计算两个数组的点积并打印出来。(random.randn()、dot()函数)

可以的,以下是代码实现: ```python import numpy as np # 创建两个包含9个随机数的3*3的矩阵 matrix1 = np.random.randn(3, 3) matrix2 = np.random.randn(3, 3) # 打印两个矩阵 print("Matrix 1:\n", matrix1) print("Matrix 2:\n", matrix2) # 计算两个数组的点积并打印出来 dot_product = np.dot(matrix1, matrix2) print("Dot product:\n", dot_product) ``` 希望