博客
关于我
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/

你可能感兴趣的文章
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>
Node.js 实现类似于.php,.jsp的服务器页面技术,自动路由
查看>>
node.js 怎么新建一个站点端口
查看>>
Node.js 文件系统的各种用法和常见场景
查看>>