c++实现1.定义待排序表的长度为n。 2.初始化随机数发生器,产生n个随机数构成的顺序表。 3.创建输出排序表函数,取得系统时间函数,直接插入排序函数,冒泡排序函数,快速排序函数,直接选择排序函数,筛选函数,对排序函数,归并排序函数。 4.主程序调用各个排序函数,输出各排序算法对长度为n的顺序表进行排序所耗费的时间

时间: 2024-02-05 16:13:09 浏览: 22
下面是一份使用C++实现各种排序算法并测试时间的示例代码: ```cpp #include <iostream> #include <cstdlib> #include <ctime> #include <chrono> using namespace std; using namespace std::chrono; // 直接插入排序 void insertSort(int arr[], int n) { for (int i = 1; i < n; i++) { int j = i; int tmp = arr[j]; while (j > 0 && tmp < arr[j-1]) { arr[j] = arr[j-1]; j--; } arr[j] = tmp; } } // 冒泡排序 void bubbleSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-1-i; j++) { if (arr[j] > arr[j+1]) { swap(arr[j], arr[j+1]); } } } } // 快速排序 void quickSort(int arr[], int left, int right) { if (left >= right) return; int i = left, j = right, pivot = arr[left]; while (i < j) { while (i < j && arr[j] >= pivot) j--; arr[i] = arr[j]; while (i < j && arr[i] <= pivot) i++; arr[j] = arr[i]; } arr[i] = pivot; quickSort(arr, left, i-1); quickSort(arr, i+1, right); } // 直接选择排序 void selectSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { int minIdx = i; for (int j = i+1; j < n; j++) { if (arr[j] < arr[minIdx]) { minIdx = j; } } swap(arr[i], arr[minIdx]); } } // 筛选排序 void siftDown(int arr[], int i, int n) { int tmp = arr[i]; while (2*i + 1 < n) { int j = 2*i + 1; if (j+1 < n && arr[j+1] > arr[j]) j++; if (tmp >= arr[j]) break; arr[i] = arr[j]; i = j; } arr[i] = tmp; } void heapSort(int arr[], int n) { for (int i = n/2-1; i >= 0; i--) { siftDown(arr, i, n); } for (int i = n-1; i > 0; i--) { swap(arr[0], arr[i]); siftDown(arr, 0, i); } } // 归并排序 void merge(int arr[], int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int* L = new int[n1]; int* R = new int[n2]; for (int i = 0; i < n1; i++) { L[i] = arr[left+i]; } for (int i = 0; i < n2; i++) { R[i] = arr[mid+1+i]; } int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k++] = L[i++]; } else { arr[k++] = R[j++]; } } while (i < n1) { arr[k++] = L[i++]; } while (j < n2) { arr[k++] = R[j++]; } delete[] L; delete[] R; } void mergeSort(int arr[], int left, int right) { if (left >= right) return; int mid = (left + right) / 2; mergeSort(arr, left, mid); mergeSort(arr, mid+1, right); merge(arr, left, mid, right); } // 输出数组 void printArr(int arr[], int n) { for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; } int main() { srand(time(NULL)); int n; cout << "请输入待排序表的长度:"; cin >> n; int* arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = rand() % 100; } auto start = high_resolution_clock::now(); insertSort(arr, n); auto stop = high_resolution_clock::now(); cout << "直接插入排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl; start = high_resolution_clock::now(); bubbleSort(arr, n); stop = high_resolution_clock::now(); cout << "冒泡排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl; start = high_resolution_clock::now(); quickSort(arr, 0, n-1); stop = high_resolution_clock::now(); cout << "快速排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl; start = high_resolution_clock::now(); selectSort(arr, n); stop = high_resolution_clock::now(); cout << "直接选择排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl; start = high_resolution_clock::now(); heapSort(arr, n); stop = high_resolution_clock::now(); cout << "筛选排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl; start = high_resolution_clock::now(); mergeSort(arr, 0, n-1); stop = high_resolution_clock::now(); cout << "归并排序用时:" << duration_cast<microseconds>(stop-start).count() << " us" << endl; delete[] arr; return 0; } ``` 这份代码实现了6种排序算法,包括直接插入排序、冒泡排序、快速排序、直接选择排序、筛选排序、归并排序。在主程序中,我们先生成一个长度为n的随机数组,然后依次调用各个排序函数,计算每个函数的运行时间,并输出到屏幕上。

相关推荐

最新推荐

recommend-type

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

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

JavaScript实现随机数生成器(去重)

本文实例为大家分享了js实现随机数生成器的具体代码,供大家参考,具体内容如下 1.实验前准备: Math函数的理解 数组方法的理解 2.实验操作: &lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta ...
recommend-type

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

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

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

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

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt

麦肯锡-年月―中国xx集团战略咨询项目建议书.ppt
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的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。