Merge sort process
Merge sort is a sorting algorithm based on the idea of divide and conquer. Its basic idea can be divided into three steps:
Step 1: Decomposition (Divide): Merge sort uses a recursive algorithm to implement the decomposition process. The specific implementation can be divided into the following steps:
If the length of the array to be sorted is 1, it is considered that the array is already sorted, and returns directly;
Divide the array to be sorted into two sub-arrays of equal length, and recursively sort the two sub-arrays respectively;
Merges two sorted subarrays into a single sorted array and returns this sorted array. Step 2: Merge (Merge): During the merge process, it is necessary to compare the elements of each sub-array and merge them into a new array in an orderly manner:
You can use two pointers i and j to point to the beginning of the two sub-arrays respectively, compare their element sizes, and insert the smaller elements into the new sorted array.
If one of the sub-arrays has been traversed, the remaining part of the other sub-array is directly inserted into the new sorted array.
Finally return this sorted array.
Step 3: The recursive termination condition of merge sort:
Merge sort uses a recursive algorithm to implement the decomposition process. When the length of the sub-array is 1, the sub-array is considered to be in order, and the recursion ends. Generally speaking, the basic idea of merge sorting is the divide and conquer method, which is divided into sub-problems to solve separately, and then the solutions of the sub-problems are combined into an overall solution.
Diagram of Merge Sort
merge sort code
// 定义函数mergeSort,参数是待排序数组arr
function mergeSort(arr: number[]): number[] {
// 如果数组长度小于等于1,则直接返回该数组
if (arr.length === 1) return arr
// 计算中间位置
let mid = Math.floor(arr.length / 2);
// 对左边的数组进行归并排序
let left = mergeSort(arr.slice(0,mid));
// 对右边的数组进行归并排序
let right = mergeSort(arr.slice(mid));/* */
// 合并两个排好序的数组
return merge(left,right);
}
// 定义函数merge,参数是两个排好序的数组left和right
function merge(left: number[], right: number[]): number[] {
let i = 0;
let j = 0;
let result = [];
// 比较两个数组的第一个元素,将较小的放入result数组
while(i<left.length&& j <right.length) {
if (left[i] < right[j]) {
result.push(left[i]);
i ++
} else {
result.push(right[j]);
j++
}
}
// 将没有比较完的剩余元素放入result数组
while(i < left.length) {
result.push(left[i]);
i++
}
while(j < right.length) {
result.push(right[j]);
j++
}
return result
}
// 测试数据
const testArr = [5, 2, 9, 1, 5, 6];
// 调用插入排序函数
const sortedArr = mergeSort(testArr);
// 打印结果
console.log(sortedArr);
Time complexity of merge sort
The analysis process of complexity:
Assuming that the length of the array is n, logn merge operations are required;
Each merge operation requires O(n) time complexity;
Therefore, the time complexity of merge sort is O(nlogn).
Best case: O(log n)
In the best case, the array to be sorted is already in order, so each sub-array only needs to be merged once, that is, only one merge operation is required.
Therefore, the time complexity at this point is O(log n).
Worst case: O(nlogn)
In the worst case, the array to be sorted is in reverse order, so each subarray needs to be merged multiple times.
Therefore, the time complexity at this time is O(nlogn).
Average case: O(nlogn)
On average, we assume that any two elements in the array to be sorted are equally likely to appear.
此时,可以证明归并排序的时间复杂度为 O(nlogn)。
总结
到这里,归并排序的研究就结束啦。在以后的实际场景中,归并排序很适合数据量大的排序,并且归并排序的算法实现也比较简单,归并排序是一种不需要过多研究的算法,适合于所有的排序场景。