c++ 实现五种基础的排序算法

C++  /  houtizong 发布于 2年前   180
#include<iostream>using namespace std;//辅助函数,交换两数之值template<class T>void mySwap(T &x, T &y){T temp = x;x = y;y = temp;}const int size = 10;//一、用直接插入排序法对数组a中元素进行升序排序/*直接插入排序的基本思想是:*顺序地把待排序序列中的各个记录按其关键字的大小,插入到已排序的序列的适当位置。*/template<class T>void insertionSort( T a[], int n){//将下标为1~n-1的元素逐个插入到已排序序列中适当的位置for (int i = 1; i != n; i++){//从a[i-1]开始向a[0]方向扫描各元素,寻找适当位置插入a[i]int j = i;T temp = a[i];while (j > 0 && temp < a[j - 1]){//逐个比较,直到temp>=a[j-1]时,j便是应插入的位置//若达到j==0,则0是应插入的位置a[j] = a[j - 1];//将元素逐个后移,以便找到插入位置使可立即插入j--;}//插入位置已找到,立即插入a[j] = temp;for (int i = 0; i != n; i++){//输出每步交换cout << a[i] << " ";}cout << endl;}}//二、用选择法对数组a中元素进行升序排序/**选择排序的基本思想是:*不断从待排序的序列中选取关键字最小的记录放到已排序的记录序列的后面,直到序列中所有记录都已排序为止。*/template<class T>void selectionSort(T a[], int n){for (int i = 0; i != n - 1; i++){int leastIndex = i;//最小元素的下标初值设为i//在元素a[i+1]....a[n-1]中逐个比较,显出最小值for (int j = i + 1; j < n; j++){if (a[j] < a[leastIndex])//smallIndex始终记录当前找到的最小值的下标leastIndex = j;}mySwap(a[i], a[leastIndex]);//将这一趟找到的最小元素与a[i]交换for (int i = 0; i != n; i++){//输出每步交换cout << a[i] << " ";}cout << endl;}}//三、用起泡法对数组a中元素进行升序排序/*起泡排序的基本思想是:*将待排序序列中第一个记录的关键字R1与第二个记录的关键字R2做比较,*如果R1>R2,则交换R1和R2的位置,否则不交换;*然后继续对当前序列中的第二个记录和第三个记录同样的处理,依此类推。*/template<class T>void bubbleSort(T a[], int n){int i = n - 1;//i是下一趟需参与排序交换的元素的最大下标while (i > 0){//继续排序过程,直到最后一趟排序没有交换发生,或已达n-1趟int lastExchangeIndex = 0;//每一趟开始时,设置交换标志为0for (int j = 0; j < i; j++){//每一趟对元素a[0]....a[i]进行比较和交换if (a[j + 1] < a[j]){//如果元素a[j+1]<a[j],交换mySwap(a[j], a[j + 1]);for (int x = 0; x != size; x++){    //输出每步交换cout << a[x] << " ";}cout << endl;lastExchangeIndex = j;//记录被交换的的一对元素中较小的下标}}i = lastExchangeIndex;//将i设置为本趟被交换的最后一对元素中较小的下标}}//四、用希尔排序法对数组a中元素进行升序排序/*希尔排序的基本思想是:*不断把待排序的记录分成若干个小组,对同一组内的记录进行排序,*在分组时,始终保证当前组内的记录个数超过前面分组排序时组内的记录个数。*/template<class T>void shellSort(T a[], int n){for (int i = 0; i != size; i++){cout << a[i] << " ";}cout << endl;for (int i = n / 2; i > 0; i /= 2){for (int j = i; j < n; j++){int temp = a[j];int k = 0;for (k = j; k >= i; k -= i){if (temp < a[k - i]){a[k] = a[k - i];}elsebreak;}a[k] = temp;for (int i = 0; i != size; i++){cout << a[i] << " ";}cout << endl;}}}//五、用快速排序法对数组a中元素进行升序排序/**该方法的基本思想是:1.先从数列中取出一个数作为基准数。2.分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。3.再对左右区间重复第二步,直到各区间只有一个数。*/void quickSort(int s[], int l, int r){if (l< r){int 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;for (int i = 0; i != size; i++){cout << s[i] << " ";}cout << endl;quickSort(s, l, i - 1); // 递归调用quickSort(s, i + 1, r);}}int main(){int a[10] = { 13, 3, 1, 5, 2, 421, 2, 4, 3, 15 };int b[10] = { 13, 3, 1, 5, 2, 421, 2, 4, 3, 15 };int c[10] = { 13, 3, 1, 5, 2, 421, 2, 4, 3, 15 };int d[10] = { 13, 3, 1, 5, 2, 421, 2, 4, 3, 15 };int e[10] = { 13, 3, 1, 5, 2, 421, 2, 4, 3, 15 };cout <<  endl;for (int i = 0; i != size; i++){cout << a[i] << " ";}cout << endl;cout << "*********直接插入排序法************" << endl;insertionSort(a, size);cout << "*******直接插入排序法结束**********" << endl;cout << endl;for (int i = 0; i != size; i++){cout << b[i] << " ";}cout << endl;cout << "*********选择排序法************" << endl;selectionSort(b, size);cout << "*******选择排序法结束**********" << endl;cout << endl;for (int i = 0; i != size; i++){cout << c[i] << " ";}cout << endl;cout << "*********起泡排序法************" << endl;bubbleSort(c, size);cout << "*******起泡排序法结束**********" << endl;cout << endl;for (int i = 0; i != size; i++){cout << d[i] << " ";}cout << endl;cout << "*********希尔排序法************" << endl;shellSort(d, size);cout << "*******希尔排序法结束**********" << endl;cout << endl;for (int i = 0; i != size; i++){cout << d[i] << " ";}cout << endl;cout << "*********快速排序法************" << endl;quickSort(e, 0,size-1);cout << "*******快速排序法结束**********" << endl;for (int i = 0; i != size; i++){cout << e[i] << " ";}cout << endl;}

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客