当前位置:网站首页>几种常见的排序
几种常见的排序
2022-07-24 04:38:00 【向往神秘的天堂】
排序
排序:排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作,在生活中排序的运用又很多,如某宝的商品排名,院校排名等。
常见的排序分为:插入排序、选择排序、交换排序、归并排序等,其中插入排序包含直接插入排序与希尔排序,选择排序包含:选择排序、堆排序,交换排序包括:冒泡排序、快速排序,归并排序包含其本身。
本次主要介绍插入排序与选择排序中的堆排序,插入排序的思路比较简单,其基本思想就像平时打扑克牌一样,将无序的牌从大到小或从小到大逐个插入有序的扑克牌中,直到所有的牌有序,更详细的描述如下:当插入第i(i>=1)个元素时,前面的array[0],array[1],…,array[i-1]已经排好序,此时用array[i]的排序码与array[i-1],array[i-2],…的排序码顺序进行比较,找到插入位置即将array[i]插入,原来位置上的元素顺序后移。
其主要代码如下所示:
void InserSort(int* a, int n)
{
for (int i = 0;i < n-1;++i)
{
int end = i;
int tmp = a[end + 1];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
--end;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
}
先理解while里面的内容,相当于单趟插入排序,假设end位置为3,则从数组的第三个数作为开始,从end向数组下标为0的位置逐次靠近,若a[end]>a[end+1]的话,将end位置的数往后挪一个,再将end–,就是逐次靠近数组下标为0的过程,直到前end的个数有序,而for语句的过程相当于控制前n个数有序的过程,这就是直接插入的过程,但是直接插入在时间复杂度上表现为O(N^2),因为在数组最坏的情况(数组为逆序,此时需要排升序),故针对此情况,希尔提出了一种改进直接插入排序的方法,即希尔排序。
希尔排序的过程由预排序到直接排序的过程,在预排序的过程中,使用间隔空间排序的方法,使用gap来表示整个间隔的大小,其预排序的思路与直接插入排序一致,不同的是,直接插入排序是将两个相邻的数进行比较,然后将较大者往后挪(在排升序的情况下,降序相反),而预排序的过程是间隔gap的两个数进行比较,较大者往后挪,当gap为1时,其就是直接插入排序,gap值越大,越大的数越快的到达尾端(在升序的情况下),但其越不接近有序,gap越小,其越接近有序,但较大的数越慢到达尾端,下图显示了不同gap值下,数组排序的不同状态。
希尔排序的代码如下:
void ShellSort(int* a, int n)
{
int gap = n;
while (gap > 1)
{
gap = gap / 3 + 1;
for (int i = 0;i < n - gap;++i)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = tmp;
}
}
}
希尔排序通过gap来控制预排序的过程,当gap值为1时,排序为直接插入排序,最终完成对数组的排序,希尔排序的时间复杂度在O(n^1.5)左右,其时间复杂度计算较为繁琐,这里就不展开讨论,后续继续分享其他几种排序的思路,并讨论其时间复杂度,与稳定性。
边栏推荐
- 到3mm;提供安全稳定的产品作的执行据发出方iid
- Uniapp learning
- [untitled]
- C语言经典习题之猴子吃桃问题
- 致-.-- -..- -
- Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind
- 激活函数和最常用的10个激活函数
- 黑色的的一站式运维管家 10条记录RO
- OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication
- Division of training set, verification set and test set in link prediction (take randomlinksplit of pyg as an example)
猜你喜欢

C语言经典习题之评委打分去掉最高最低求平均分

Journey of little black leetcode: 590. Post order traversal of n-ary tree

C语言经典习题之猴子吃桃问题

The second anniversary of opengauss' open source, cracking the pain point of database ecology

Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc

Basic learning notes of C language

Smart contract: release an erc20 token

数组力扣(持续更新)

Will your NFT disappear? Dfinity provides the best solution for NFT storage

Write a search box with search tips
随机推荐
到3mm;提供安全稳定的产品作的执行据发出方iid
由硬件确定(服务的服绍,可参看官方2 和
Particle Designer:粒子效果制作器,生成plist文件并在工程中正常使用
[JS] save the string as a file to the local (.Txt,.Json,.Md...)
[untitled]
uniapp学习
Label smoothing
Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc
Is it true to pay attention to official account and receive Xiaomi mobile power for free? Wechat circle of friends sends Xiaomi mobile power
Black one-stop operation and maintenance housekeeper 10 records ro
NFT insider 67: Barcelona Football Club launched its first NFT work, and Dubai launched the national metauniverse strategy
Division of training set, verification set and test set in link prediction (take randomlinksplit of pyg as an example)
Learn more about the new features of ES6 in grain mall of e-commerce project
Common cross domain problems
数组力扣(持续更新)
How to register and apply for free for Apple Developer account in order to enjoy the upgrade experience at the first time
Chery arizer 8 products are powerful, and "all excellent" is not for nothing
Smart people's game improvement: Chapter 3 Lesson 3 example: the secret of prime
打印1000年到2000年之间的闰年
There is not enough space on the disk to complete this operation when partitioning the computer