当前位置:网站首页>高攀不起的希尔排序,直接插入排序
高攀不起的希尔排序,直接插入排序
2022-07-01 21:45:00 【老鱼37】
因为期末考试断更了这么久不好意思,从今天起平均每天输出一篇高质量文章供大家享用!!!
First:
直接插入排序:InsertSort
整体思想:

完整代码实现:
//直接插入排序
void InsertSort(int* arr, int len)
{
for (int i = 0; i < len - 1; i++)
{
int end = i;
int tmp = arr[end + 1];
while (end >= 0)
{
if (tmp < arr[end])
{
arr[end + 1] = arr[end];
--end;
}
else
{
break;
}
}
arr[end + 1] = tmp;
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
InsertSort(arr, len);
PrintSort(arr, len);
}下面介绍的是曾经你对他爱答不理,现在你高攀不起的希尔排序
希尔排序是对直接插入排序的优化
整体思想:

1、预排序---将数据变成快要排序好的数据
步骤:先分小组gap,然后再进行排序

当gap到达1的时候就排序好了
是不是很神奇
总结希尔排序特点:
完整代码实现:
//希尔排序
void ShellSort(int* arr, int len)
{
int gap = len;
while (gap > 1)
{
gap = gap / 3 + 1;//保证最后一个数是1
for (int i = 0; i < len - gap; i++)
{
int end = i;
int tmp = arr[end + gap];
while (end >= 0)
{
if (tmp < arr[end])
{
arr[end + gap] = arr[end];
end -= gap;
}
else
{
break;
}
}
arr[end + gap] = tmp;
}
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
ShellSort(arr, len);
PrintSort(arr, len);
}小伙伴们快点上机操作一下吧!!!
如有错误,请多多指教!
边栏推荐
- 业务可视化-让你的流程图'Run'起来
- 信标委云原生专题组组长,任重道远!
- Separate the letters and numbers in the string so that the letters come first and the array comes last
- plantuml介绍与使用
- ICML2022 | 基于元语义正则化的介入性对比学习
- 手动实现function isInstanceOf(child,Parent)
- Make a three digit number of all daffodils "recommended collection"
- 物联网rfid等
- Aidl basic use
- 收到一封CTO来信,邀约面试机器学习工程师
猜你喜欢

locust 系列入门

Introduction à l'ingénierie logicielle (sixième édition) notes d'examen de Zhang haifan

Mask wearing detection method based on yolov5

MySQL learning notes - SQL optimization of optimization

二叉树的基本操作

首席信息官对高绩效IT团队定义的探讨和分析

Why does blocprovider feel similar to provider?

从MLPerf谈起:如何引领AI加速器的下一波浪潮

Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market

plantuml介绍与使用
随机推荐
Getting started with the lockust series
上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
使用闭包实现点击按钮切换 toggle
【MySQL】explain的基本使用以及各列的作用
Show member variables and methods in classes in idea
统计字符中每个字符出现的个数
详解ThreadLocal
详解Volatile关键字
名单揭晓 | 2021年度中国杰出知识产权服务团队
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
MySQL empties table data
工控设备安全加密的意义和措施
【MySQL】数据库优化方法
业务可视化-让你的流程图'Run'起来
Introduction and download of the latest version of airserver2022
The difference between NiO and traditional IO
详解JMM
IDA动态调试apk