上述代码用c++语言呈现

时间: 2024-02-09 10:11:54 浏览: 20
好的,下面是使用 C++ 语言实现的上述代码: 1. 简单选择排序: ```c++ void selection_sort(int array[], int n) { for (int i = 0; i < n - 1; i++) { int min_index = i; for (int j = i + 1; j < n; j++) { if (array[j] < array[min_index]) { min_index = j; } } if (min_index != i) { int temp = array[i]; array[i] = array[min_index]; array[min_index] = temp; } } } ``` 2. 直接插入排序: ```c++ void insertion_sort(int array[], int n) { for (int i = 1; i < n; i++) { int key = array[i]; int j = i - 1; while (j >= 0 && array[j] > key) { array[j + 1] = array[j]; j--; } array[j + 1] = key; } } ``` 3. 冒泡排序: ```c++ void bubble_sort(int array[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } ``` 4. 希尔排序: ```c++ void shell_sort(int array[], int n) { for (int gap = n / 2; gap > 0; gap /= 2) { for (int i = gap; i < n; i++) { int temp = array[i]; int j = i; while (j >= gap && array[j - gap] > temp) { array[j] = array[j - gap]; j -= gap; } array[j] = temp; } } } ``` 5. 快速排序: ```c++ void quick_sort(int array[], int left, int right) { if (left >= right) { return; } int pivot_index = partition(array, left, right); quick_sort(array, left, pivot_index - 1); quick_sort(array, pivot_index + 1, right); } int partition(int array[], int left, int right) { int pivot = array[right]; int i = left - 1; for (int j = left; j < right; j++) { if (array[j] < pivot) { i++; int temp = array[i]; array[i] = array[j]; array[j] = temp; } } int temp = array[i + 1]; array[i + 1] = array[right]; array[right] = temp; return i + 1; } ``` 最后,在主函数中测试这些算法的代码如下: ```c++ #include <iostream> using namespace std; void selection_sort(int array[], int n); void insertion_sort(int array[], int n); void bubble_sort(int array[], int n); void shell_sort(int array[], int n); void quick_sort(int array[], int left, int right); int partition(int array[], int left, int right); int main() { int array[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; int n = sizeof(array) / sizeof(int); cout << "Original array: "; for (int i = 0; i < n; i++) { cout << array[i] << " "; } cout << endl; selection_sort(array, n); cout << "Selection sort: "; for (int i = 0; i < n; i++) { cout << array[i] << " "; } cout << endl; int array2[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; insertion_sort(array2, n); cout << "Insertion sort: "; for (int i = 0; i < n; i++) { cout << array2[i] << " "; } cout << endl; int array3[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; bubble_sort(array3, n); cout << "Bubble sort: "; for (int i = 0; i < n; i++) { cout << array3[i] << " "; } cout << endl; int array4[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; shell_sort(array4, n); cout << "Shell sort: "; for (int i = 0; i < n; i++) { cout << array4[i] << " "; } cout << endl; int array5[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; quick_sort(array5, 0, n - 1); cout << "Quick sort: "; for (int i = 0; i < n; i++) { cout << array5[i] << " "; } cout << endl; return 0; } ``` 希望这些代码能够帮到你,如果还有其他问题,欢迎随时提问。

相关推荐

最新推荐

recommend-type

C/C++语言宏定义使用实例详解

主要介绍了 C/C++语言宏定义使用实例详解的相关资料,需要的朋友可以参考下
recommend-type

约瑟夫环问题用C++代码实现

8. 【题目】约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为1的人开始报数,数到k的那个人出列;他的下一个人又从1开始报数,数到k的那个人又...
recommend-type

使用C++调用Python代码的方法详解

主要介绍了使用C++调用Python代码并给大家介绍了.py和.pyc的区别,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

学籍管理系统源代码 c++.docx

为了学校更加高效,快捷,方便的管理学生信息,并实现以下功能: (1)对学生信息进行录入:先输入学生的学籍,然后输入学生姓名,年龄,性别,籍贯,系别,专业,班级等,最后输入学生状态(入学)。...
recommend-type

C++语言数据结构 串的基本操作实例代码

主要介绍了C语言数据结构 串的基本操作实例代码的相关资料,需要的朋友可以参考下
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

MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用

![MATLAB取整函数与Web开发的作用:round、fix、floor、ceil在Web开发中的应用](https://img-blog.csdnimg.cn/2020050917173284.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2thbmdqaWVsZWFybmluZw==,size_16,color_FFFFFF,t_70) # 1. MATLAB取整函数概述** MATLAB取整函数是一组强大的工具,用于对数值进行
recommend-type

我想做python的算法工程师,我应该学什么?学习的顺序是什么?网上有什么推荐的免费课程吗?回答具体精确一点不要太笼统

对于想要成为 Python 算法工程师的人来说,他们应该先从掌握 Python 的基础语法开始,然后学习数据结构和算法,并且要深入了解 Python 中的一些科学计算和数据处理库,比如 NumPy、Pandas 等。 学习的顺序可以是先学习基础语法和编程技巧,然后再学习数据结构和算法相关的知识,最后深入了解数据处理和科学计算相关的库。 对于免费课程的推荐,我建议你可以先去 Coursera、edX、Udacity 等网站上寻找相关课程,这些网站上有很多优质的 Python 编程和算法课程,你可以根据自己的需求和学习进度进行选择。此外,还可以考虑一些在线编程网站,如 HackerRank、L
recommend-type

JSBSim Reference Manual

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