当前位置:网站首页>Sort: merge sort and quick sort
Sort: merge sort and quick sort
2022-07-26 07:28:00 【Li xiaoenzi】
Power button leetcode:912. Sort array , Can be used to practice sorting algorithm
subject : Give you an array of integers nums, Please arrange the array in ascending order .
Catalog
Both merging and fast sorting use the idea of recursion and divide and conquer . Merger : The following traversal of binary tree ; Quick line up : Preorder traversal of two tree .
Why don't you say so ? The core code framework of sorting :
Merge sort
public void sort(int[] nums,int low,int high){
if(low == high) return;
int mid = low + (high - low)/2;
// Sort left half array ,nums[low...mid]
sort(nums,low,mid);
// Sort the right half of the array ,nums[mid+1...high]
sort(nums,mid+1,high);
// Subsequent positions of binary tree traversal , Merge nums[low...mid] and nums[mid+1...high]
merge(nums,low,mid,high);
}First sort the left and right arrays , Re merger .
Quick sort
public void sort(int[] nums,int low,int high){
if(low >= high) return;
// The preorder position of a binary tree , division
int p = partition(nums,low,high);
// To the left half of the partition nums[low...p-1] Sort
sort(nums,low,p - 1);
// Right half of the division nums[p+1...high] Sort
sort(nums,p + 1,high);
} First construct the boundary point , Then go to the dividing point of the left and right subtree groups .
Merge sort
The process :
- First, divide the array to be sorted into two parts Left and Right
- Yes Left Partial sorting
- Yes Right Partial sorting
- Merge Left and Right Two parts , Merge into an ordered array
Code :
class Solution {
int[] temp; // Auxiliary array of merge sort
public int[] sortArray(int[] nums) {
// Merger
temp = new int[nums.length];
sort(nums,0,nums.length-1);
return nums;
}
public void sort(int[] nums,int low,int high){
if(low == high) return;
// Effectively prevent overflow , Actually sum (low + high)/2 Equivalent
int mid = low + (high - low)/2;
// Sort left half array
sort(nums,low,mid);
// Sort the right half of the array
sort(nums,mid+1,high);
merge(nums,low,mid,high);
}
public void merge(int[] nums,int low,int mid,int high){
// First the nums[low...high] The value of is stored in the auxiliary array temp in
for(int i = low;i <= high;i++){
temp[i] = nums[i];
}
// Double pointer merge
int i = low,j = mid + 1;
for(int m = low; m <= high; m++){
if(i == mid + 1) nums[m] = temp[j++];
else if( j == high + 1) nums[m] = temp[i++];
else if(temp[i] > temp[j]) nums[m] = temp[j++];
else nums[m] = temp[i++];
}
}
}Quick sort
Quick sort is to sort an element first , Then put the remaining elements in order . The core function is partition function :
The process :
- Is in nums[low...high] Find a dividing point in p
- Exchange elements so that nums[low...p-1] All elements of are less than or equal to nums[p]
- nums[p+1...high] All of the elements are greater than nums[p]
partition It's just a way of nums[p] This element is in order

Code :
class Solution {
public int[] sortArray(int[] nums) {
// Quick line up
sort(nums,0,nums.length - 1);
return nums;
}
public void sort(int[] nums,int low,int high){
if(low >= high) return;
// Yes nums[low...high] division
// bring nums[low...p-1] <= nums[p] < nums[p+1...high]
int p = partition(nums,low,high);
sort(nums,low,p - 1);
sort(nums,p + 1,high);
}
public int partition(int[] nums,int low,int high){
int pivot = nums[low];
int i = low + 1,j = high;
while(i <= j){ // Continuous exchange , Until the left side is satisfied, it is less than , The right side is larger than
while(i < high && nums[i] <= pivot) i++; // The while At the end num[i] > pivot
while(j > low && nums[j] > pivot) j--; // The while At the end num[j] <= pivot
if(i >= j) break;
swap(nums,i,j); // Exchange these two
}
// take pivot Put it in the right place ,pivot The element on the left is smaller than it ,pivot The element on the right is bigger than it
swap(nums,low,j);
return j;
}
public void swap(int[] nums,int a,int b){
int temp = nums[b];
nums[b] = nums[a];
nums[a] = temp;
}
}
边栏推荐
- 7月消息,Glassnode数据显示,Deribit上ETH永续期货合约未平仓头寸刚刚达到一个月高点237,959,827美元。
- Learning Efficient Convolutional Networks Through Network Slimming
- To do list application vikunja
- Apache dolphin scheduler 2.x nanny level source code analysis, China Mobile engineers uncover the whole process of service scheduling and start
- Simulation of transfer function step response output of botu PLC first-order lag system (SCL)
- 「论文笔记」Next-item Recommendations in Short Sessions
- OAuth2.0系列博客教程汇总
- 2021全球机器学习大会演讲稿
- 时间序列分析预测实战之ARIMA模型
- 【Keras入门日志(3)】Keras中的序贯(Sequential)模型与函数式(Functional)模型
猜你喜欢

Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程

Crawler data analysis

PostgreSQL sequence create alter nextval Curval numerical interval gap

WCF 入门教程二

C# 使用Log4Net记录日志(基础篇)

3.0.0 alpha blockbuster release! Nine new functions and new UI unlock new capabilities of dispatching system

What is bloom filter in redis series?

NFT digital collection development: digital collections help enterprise development

Upgrade ecological proposition: what has Alibaba cloud brought to thousands of businesses?

达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程【三】
随机推荐
MySQL syntax (2) (pure syntax)
OVSDB
[daily question 1] 919. Complete binary tree inserter
China Unicom transformed the Apache dolphin scheduler resource center to realize the one-stop access of cross cluster call and data script of billing environment
5. Multi table query
Hcip--- BGP comprehensive experiment
Hcip--- MPLS detailed explanation and BGP route filtering
NFT数字藏品系统开发:NFT数藏 的最佳数字营销策略有哪些
Configure flask
0 dynamic programming leetcode1567. Length of the longest subarray with positive product
mysql语法(二)(纯语法)
NFT digital collection development: Six differences between digital collections and NFT
Open source management system based on ThinkPHP
NFT digital collection system development: the collision of literature + Digital Collections
NFT数字藏品系统开发:激活数字文化遗产
5、多表查询
机器学习相关比赛网站
Compose text and icon splicing to realize drawableleft or drawableright
4、数据的完整性
Uncover the mystery of cloud native data management: operation level