当前位置:网站首页>【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
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下标处
- 再遍历下标,把每个数一一取出
- 重复以上步骤,直到按最高位的也操作完就排完序了

四、桶排序
思想:
划分多个范围相同的区间,每个子区间自排序,最后合并。
图源网络
边栏推荐
- export default 导出的对象,不能解构问题,和module.exports的区别
- Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
- Summary of Aix storage management
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- gradle
- How can programmers better plan their career development?
- Common loss function of deep learning
- If the browser is accidentally closed, how does react cache the forms filled out by users?
- excel查找与引用函数
- 启牛商学院给的证券账户安不安全?哪里可以开户
猜你喜欢

Promise和模块块化编程

Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes

sso单点登录的实现。

B tree and b+tree of MySQL

【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器

使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)

Database -- sqlserver details

Leetcode96 different binary search trees

Leetcode skimming: stack and queue 02 (realizing stack with queue)
![[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom](/img/d4/9d27b29080ce83004aa875a499de9b.png)
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom
随机推荐
Leetcode skimming: stack and queue 06 (top k high-frequency elements)
gradle
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
JS common library CDN recommendation
@Valid parameter verification does not take effect
Data analysis methodology and previous experience summary [notes dry goods]
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
When installing mysql, there are two packages: Perl (data:: dumper) and Perl (JSON)
S32Kxxx bootloader之UDS bootloader
4. Object mapping Mapstercover
2022 safety officer-b certificate examination practice questions simulated examination platform operation
Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
[CTF] bjdctf 2020 Bar _ Bacystack2
From 20s to 500ms, I used these three methods
An intern's journey to cnosdb
【opencv】train&test HOG+SVM
Random avatar encyclopedia, multi category wechat applet source code with history_ Support traffic master
Mysql database driver (JDBC Driver) jar package download