当前位置:网站首页>【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
2022-07-02 00:43:00 【Living_Amethyst】
目录
一、归并排序
归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。

若将两个有序表合并成一个有序表,称为二路归并。 归并排序核心步骤:
- 分解
- 合并

代码(递归)
//归并排序
public static void merge(int[] array,int low, int mid ,int high){
int s1 = low;
int e1 = mid;
int s2 = mid+1;
int e2 = high;
int[] tmpArr = new int[high-low+1];
int k = 0;
//证明两个段都有数据
while(s1 <= e1 && s2 <= e2){
if(array[s1] < array[s2]){
tmpArr[k++] = array[s1++];
}else {
tmpArr[k++] = array[s2++];
}
}
while (s1 <= e1){
tmpArr[k++] = array[s1++];
}
while (s2 <= e2){
tmpArr[k++] = array[s2++];
}
for(int i = 0; i < k; i++){
array[i+low] = tmpArr[i];
}
}
public static void mergeSortInternal(int[] array,int low, int high){
if(low >= high) return; //递归结束条件
int mid = low + ((high-low) >>> 1) ;
mergeSortInternal(array,low,mid);
mergeSortInternal(array,mid+1,high);
merge(array,low,mid,high);
}
public static void mergeSort(int[] array){
mergeSortInternal(array,0,array.length-1);
}
代码(非递归)
public static void merge(int[] array,int low, int mid ,int high){
int s1 = low;
int e1 = mid;
int s2 = mid+1;
int e2 = high;
int[] tmpArr = new int[high-low+1];
int k = 0;
//证明两个段都有数据
while(s1 <= e1 && s2 <= e2){
if(array[s1] < array[s2]){
tmpArr[k++] = array[s1++];
}else {
tmpArr[k++] = array[s2++];
}
}
while (s1 <= e1){
tmpArr[k++] = array[s1++];
}
while (s2 <= e2){
tmpArr[k++] = array[s2++];
}
for(int i = 0; i < k; i++){
array[i+low] = tmpArr[i];
}
}
//归并排序(非递归)
public static void mergeSortNor(int[] array){
int gap = 1;
while(gap < array.length){
for(int i = 0; i < array.length; i += 2*gap){
int left = i;
int mid = left+gap-1;
//修正mid,防止越界
if(mid >= array.length){
mid = array.length-1;
}
int right = mid+gap;
//修正right
if(right >= array.length){
right = array.length-1;
}
merge(array,left,mid,right);
}
}
}
归并排序总结
- 归并的缺点在于需要O(N)的空间复杂度,归并排序的思考更多的是解决在磁盘中的外排序问题。
- 时间复杂度:O(N*logN)
- 空间复杂度:O(N)
- 稳定性:稳定
二、计数排序
思想:计数排序又称为鸽巢原理,是对哈希直接定址法的变形应用。 操作步骤:
1. 统计相同元素出现次数
2. 根据统计的结果将序列回收到原来的序列中
算法的步骤如下:
- (1)找出待排序的数组中最大和最小的元素
- (2)统计数组中每个值为i的元素出现的次数,存入数组C的第i项
- (3)对所有的计数累加(从C中的第一个元素开始,每一项和前一项相加)
- (4)反向填充目标数组:将每个元素i放在新数组的第C(i)项,每放一个元素就将C(i)减去1

代码
public static void countSort(int[] array){
//1.获取最大值和最小值
int maxVal = array[0];
int minVal = array[0];
for(int i = 1; i < array.length;i++){
if(maxVal < array[i]){
maxVal = array[i];
}
if(minVal > array[i]){
minVal = array[i];
}
}
//2.开始计数
int range = maxVal-minVal+1;
int[] count = new int[range];
for (int i = 0; i < array.length; i++) {
count[array[i] - minVal]++;
}
//3.遍历这个计数的数组,把数据放回array
int index = 0;
for (int i = 0; i < count.length; i++) {
while(count[i]>0) {
array[index++] = i + minVal;
count[i]--;
}
}
} 【计数排序的特性总结】
1. 计数排序在数据范围集中时,效率很高,但是适用范围及场景有限。
2. 时间复杂度:O(MAX(N,范围))
3. 空间复杂度:O(范围)
4. 稳定性:稳定
三、基数排序
基数排序是按照低位先排序,然后收集;再按照高位排序,然后再收集;依次类推,直到最高位。有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序。最后的次序就是高优先级高的在前,高优先级相同的低优先级高的在前。
算法描述
- 取得数组中的最大数,并取得位数;
- 先按个位,个位为0的放在0下标处,个位为1放在1下标处,个位为n放在n下标处
- 再遍历下标,把每个数一一取出
- 再按十位,十位为0的放在0下标处,十位为1放在1下标处,十位为n放在n下标处
- 再遍历下标,把每个数一一取出
- 重复以上步骤,直到按最高位的也操作完就排完序了

四、桶排序
思想:
划分多个范围相同的区间,每个子区间自排序,最后合并。
图源网络
边栏推荐
- Leetcode96 different binary search trees
- Heketi record
- 【会议资源】2022年第三届自动化科学与工程国际会议(JCASE 2022)
- Basis of deep learning neural network
- [wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login
- SQL数据分析之流程控制语句【if,case...when详解】
- EMC circuit protection device for surge and impulse current protection
- Advanced skills of testers: a guide to the application of unit test reports
- JS——图片转base码 、base转File对象
- Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master
猜你喜欢

It's nothing to be utilitarian!

Slf4j print abnormal stack information
![Data analysis methodology and previous experience summary [notes dry goods]](/img/00/e4c4cf37f1ca9134546f970d800226.png)
Data analysis methodology and previous experience summary [notes dry goods]

SQL Server 安装指南

AIX存储管理之卷组属性的查看和修改(二)

智能运维实战:银行业务流程及单笔交易追踪

2023款雷克萨斯ES产品公布,这回进步很有感

测试员8年工资变动,令网友羡慕不已:你一个月顶我一年工资

How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03

2022 safety officer-a certificate examination questions and online simulation examination
随机推荐
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
Common loss function of deep learning
B tree and b+tree of MySQL
Dongge cashes in and the boss retires?
Leetcode skimming: binary tree 03 (post order traversal of binary tree)
[CTF] bjdctf 2020 Bar _ Bacystack2
sso单点登录的实现。
How to improve data quality
Schrodinger's Japanese learning applet source code
excel查找与引用函数
Is the securities account given by qiniu business school safe? Where can I open an account
I want to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
You probably haven't noticed the very important testing strategy in your work
BPR (Bayesian personalized sorting)
数据分析方法论与前人经验总结【笔记干货】
cookie、session、tooken
AIX存储管理之总结篇
What is ThreadLocal memory leak and how to solve it
Using multithreaded callable to query Oracle Database
JS -- image to base code, base to file object