博客
关于我
C++shell sort希尔排序的实现算法之一(附完整源码)
阅读量:255 次
发布时间:2019-03-01

本文共 1321 字,大约阅读时间需要 4 分钟。

C++希尔排序实现算法

C++希尔排序实现算法完整源码

了解希尔排序的基本原理及其在C++中的实现方法

希尔排序的基本原理

  • 希尔排序是一种优化的选择排序算法
  • 其核心思想是将数组分成若干个"轮"进行排序
  • 每一轮中选择一个"距离"进行排序
  • 随着轮数的增加,距离逐渐减小,最终达到1
  • 这种方法可以有效减少每次选择最小值的时间复杂度
#include 
using namespace std;void shellSort(int array[], int size){ int n = size; int k = 1; // 初始化分组距离 while (k < n) { // 将数组分成k个组 for (int i = 0; i < n; i++) { int temp = array[i]; int j; for (j = i - k; j >= 0 && array[j] > temp; j--) { array[j] = temp; } array[j] = temp; } // 调整数组以进行下一轮排序 k *= 2; } // 排序完成 cout << "希尔排序完成!" << endl;}int main(){ int size = 10; int array[size] = {65, 45, 13, 18, 97, 1, 77, 52, 26, 83}; cout << "排序前的数组:"; for (int i = 0; i < size; i++) cout << array[i] << " "; cout << endl; shellSort(array, size); cout << "排序后的数组:"; for (int i = 0; i < size; i++) cout << array[i] << " "; cout << endl; return 0;}

转载地址:http://yfsx.baihongyu.com/

你可能感兴趣的文章
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
Nodejs连接mysql
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
node安装及配置之windows版
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node读取并输出txt文件内容
查看>>
NOIp2005 过河
查看>>
NOIp模拟赛二十九
查看>>
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>