当前位置: 首页 > news >正文

排序算法【希尔排序】

一、原理

(1)步长为4时候的插入排序

(2)步长为2的时候的插入排序

(3)步长为1的时候的插入排序

二、代码如下所示:

#ifndef __TEST_H__
#define __TEST_H__
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>bool check(int* ptr, int begin, int len);
int* CreateArray(int len);/* 测试排序算法的宏定义* fun:函数指针* arr:数组地址* n  :数组元素个数*/
#define TEST_MY(fun, arr, n) { \printf("Test %s: ", #fun); \int* ptr = (int*)malloc(n*sizeof(int)); \memcpy(ptr, arr, n*sizeof(int)); \long long begin = clock(); \fun(ptr, 0, n); \long long end = clock(); \if (check(ptr, 0, n)) { \printf(" ok "); \} else { \printf(" error "); \} \printf(" len:%d   Tim: %lld ms \r\n", n, (end - begin)*1000/ CLOCKS_PER_SEC); \free(ptr); \
}/** 交换两个变量值的宏定义 */#endif
#include "test.h"
#include <string.h>/* 检查数组中的数据是不是从小到大的顺序 */
bool check(int* ptr, int begin, int len)
{for (int i = begin; i < len - 2; i++){if (ptr[i] > ptr[i + 1]){return false;}}return true;
}/* 创建一个指定长度的乱序数组 */
int* CreateArray(int len)
{int* array = (int*)malloc(sizeof(int) * len);for (int i = 0; i < len; i++){array[i] = rand() % 10000;}return array;
}
#include <stdio.h>
#include "test.h"/* 插入排序 */
void insert_sort(int* arr, int begin, int len)
{int j, data;for (int i = begin + 1; i < len; i++){j = i;while (j > begin && arr[j] < arr[j - 1]){data = arr[j];arr[j] = arr[j - 1];arr[j - 1] = data;j -= 1;                 //放在最后在减少,放在最前面会出错。}}
}/* 无监督插入排序算法 */
void unguarded_insert_sort(int* arr, int begin, int len, int step)
{int data;int min_index = begin;//找到最小值for (int i = begin + step; i < len; i+= step){if (arr[i] < arr[min_index]){min_index = i;}}//向前插入,如果直接交换,就不是插入了。while (min_index > begin){data = arr[min_index];arr[min_index] = arr[min_index - step];arr[min_index - step] = data;min_index -= step;}//无监督向前插入排序for (int i = begin + 2 * step; i < len; i+= step){int j = i;while (arr[j] < arr[j - step])   //最后一次循环总是不符合条件的{data = arr[j];arr[j] = arr[j - step];arr[j - step] = data;j -= step;}}
}/* 希尔排序 */
void shell_sort(int* arr, int begin, int len)
{int k = 2, n = (len - begin), step;do{step = (n / k == 0 ? 1 : n / k);for (int i = begin; i < begin + step; i++){unguarded_insert_sort(arr, begin, len, step);}k *= 2;              //步长每次乘以2} while (step !=1);      //step的步长不为1
} void main()
{int* arr = CreateArray(10000);//TEST_MY(selection_sort, arr, 10000);TEST_MY(shell_sort, arr, 10000);/*unguarded_insert_sort(arr, 0, 10000, 1);*/printf("data:%d, %d, %d, %d\r\n", arr[0], arr[1], arr[2], arr[3]);free(arr);return;
}

运行的结果如下所示:

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python识别车辆标志
  • 前端开发攻略---图片裁剪上传的原理
  • Hackademic.RTB1靶场实战【超详细】
  • S71200 - 编程 - 笔记
  • ZooKeeper 集群的详细部署
  • eNSP 华为三层交换机实现VLAN间通信
  • 【课程总结】day23:大模型训练策略(BERT模型与GLM模型)
  • 【若依 - 前后端不分离版】SysCaptchaController 详解:生成与处理验证码
  • springboot2.x到spring3.x的一些变化和示例说明
  • 花钱买不到系列之—linux系统调用
  • 嵌入式学习Day29---Linux软件编程---网络编程
  • 力扣--最长公共前缀
  • C++ 对象构造语义学——局部对象、全局对象的构造和析构
  • MINIO图片地址浏览器打开不显示
  • python中的列表、元组、字典之间的区别
  • HTML5新特性总结
  • java8-模拟hadoop
  • Vue.js 移动端适配之 vw 解决方案
  • Vue学习第二天
  • 大主子表关联的性能优化方法
  • 设计模式 开闭原则
  • 使用前端开发工具包WijmoJS - 创建自定义DropDownTree控件(包含源代码)
  • 自定义函数
  • [地铁译]使用SSD缓存应用数据——Moneta项目: 低成本优化的下一代EVCache ...
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • # 数据结构
  • (C++)八皇后问题
  • (LLM) 很笨
  • (Matlab)基于蝙蝠算法实现电力系统经济调度
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (二)Linux——Linux常用指令
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (三)Kafka离线安装 - ZooKeeper开机自启
  • (算法)大数的进制转换
  • (转)VC++中ondraw在什么时候调用的
  • (转)程序员技术练级攻略
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • ***通过什么方式***网吧
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .Net Core 微服务之Consul(三)-KV存储分布式锁
  • .Net mvc总结
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .net访问oracle数据库性能问题
  • .net经典笔试题
  • .NET周刊【7月第4期 2024-07-28】
  • .sh
  • [《百万宝贝》观后]To be or not to be?
  • [20170705]lsnrctl status LISTENER_SCAN1
  • [AIGC] 深入浅出 Python中的`enumerate`函数
  • [C++]18:set和map的使用
  • [CQOI 2010]扑克牌
  • [Excel] vlookup函数