用C++写一段代码 定义待排序表的长度为n。 初始化随机数发生器,产生n个随机数构成的顺序表。 创建输出排序表函数,取得系统时间函数,直接插入排序函数,冒泡排序函数,快速排序函数,直接选择排序函数,筛选函数,对排序函数,归并排序函数。 主程序调用各个排序函数,输出各排序算法对长度为n的顺序表进行排序所耗费的时间为多少毫秒。

时间: 2024-02-06 12:10:29 浏览: 25
以下是C++代码: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; void initArray(int a[], int n) { srand(time(NULL)); // 初始化随机数发生器 for (int i = 0; i < n; i++) { a[i] = rand() % 100; // 生成0-99之间的随机数 } } void printArray(int a[], int n) { for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } void insertionSort(int a[], int n) { int i, j, temp; for (i = 1; i < n; i++) { temp = a[i]; for (j = i - 1; j >= 0 && a[j] > temp; j--) { a[j + 1] = a[j]; } a[j + 1] = temp; } } void bubbleSort(int a[], int n) { int i, j, temp; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } } void quickSort(int a[], int left, int right) { if (left >= right) { return; } int i = left, j = right, pivot = a[(left + right) / 2]; while (i <= j) { while (a[i] < pivot) { i++; } while (a[j] > pivot) { j--; } if (i <= j) { swap(a[i], a[j]); i++; j--; } } quickSort(a, left, j); quickSort(a, i, right); } void selectionSort(int a[], int n) { int i, j, minIndex, temp; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) { if (a[j] < a[minIndex]) { minIndex = j; } } if (minIndex != i) { temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp; } } } void sieveSort(int a[], int n) { int i, j, temp, k = n / 2; while (k >= 1) { for (i = k; i < n; i++) { temp = a[i]; j = i - k; while (j >= 0 && a[j] > temp) { a[j + k] = a[j]; j -= k; } a[j + k] = temp; } k /= 2; } } void merge(int a[], int left, int mid, int right) { int i = left, j = mid + 1, k = 0; int temp[right - left + 1]; while (i <= mid && j <= right) { if (a[i] <= a[j]) { temp[k++] = a[i++]; } else { temp[k++] = a[j++]; } } while (i <= mid) { temp[k++] = a[i++]; } while (j <= right) { temp[k++] = a[j++]; } for (i = left, k = 0; i <= right; i++, k++) { a[i] = temp[k]; } } void mergeSort(int a[], int left, int right) { if (left >= right) { return; } int mid = (left + right) / 2; mergeSort(a, left, mid); mergeSort(a, mid + 1, right); merge(a, left, mid, right); } int main() { int n = 10; // 待排序表的长度 int a[n]; // 声明顺序表 initArray(a, n); // 初始化顺序表 cout << "原始数组:"; printArray(a, n); // 输出原始顺序表 clock_t start, end; // 声明计时器 // 直接插入排序 start = clock(); insertionSort(a, n); end = clock(); cout << "直接插入排序后的数组:"; printArray(a, n); cout << "直接插入排序耗时:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl; // 冒泡排序 initArray(a, n); // 重新初始化顺序表 start = clock(); bubbleSort(a, n); end = clock(); cout << "冒泡排序后的数组:"; printArray(a, n); cout << "冒泡排序耗时:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl; // 快速排序 initArray(a, n); // 重新初始化顺序表 start = clock(); quickSort(a, 0, n - 1); end = clock(); cout << "快速排序后的数组:"; printArray(a, n); cout << "快速排序耗时:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl; // 直接选择排序 initArray(a, n); // 重新初始化顺序表 start = clock(); selectionSort(a, n); end = clock(); cout << "直接选择排序后的数组:"; printArray(a, n); cout << "直接选择排序耗时:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl; // 筛选排序 initArray(a, n); // 重新初始化顺序表 start = clock(); sieveSort(a, n); end = clock(); cout << "筛选排序后的数组:"; printArray(a, n); cout << "筛选排序耗时:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl; // 归并排序 initArray(a, n); // 重新初始化顺序表 start = clock(); mergeSort(a, 0, n - 1); end = clock(); cout << "归并排序后的数组:"; printArray(a, n); cout << "归并排序耗时:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "毫秒" << endl; return 0; } ```

相关推荐

最新推荐

recommend-type

基于FPGA的真随机数发生器设计与实现

设计并实现了一种基于FPGA的真随机数发生器,利用一对振荡环路之间的相位漂移和抖动以及亚稳态作为随机源,使用线性反馈移位寄存器的输出与原始序列运算作为后续处理。在Xilinx Virtex-5平台的测试实验中,探讨了...
recommend-type

一个php生成16位随机数的代码(两种方法)

分享一个php生成16位随机数的代码,php生成随机数的二种方法。 方法1 复制代码 代码如下: &lt;?php $a = mt_rand(10000000,99999999); $b = mt_rand(10000000,99999999); echo $a.$b; 方法2: &lt;?php $a = range(0...
recommend-type

详解Python利用random生成一个列表内的随机数

主要介绍了详解Python利用random生成一个列表内的随机数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

python在一个范围内取随机数的简单实例

python在一个范围内取随机数的方法: 可以利用random.uniform()函数来实现。 random.uniform()函数用于生成一个指定范围内的随机浮点数,如:【random.uniform(10, 20)】。 random.randint(a, b),用于生成一个指定...
recommend-type

年终工作总结汇报PPTqytp.pptx

年终工作总结汇报PPTqytp.pptx
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

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

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