algorithm-base/animation-simulation/数据结构和算法/归并排序.md

185 lines
9.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### ****
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
23
4
PK PK,
PK:
PKPK,
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/武林大会.531pwa8nrk00.png)
****使
使
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/微信截图_20210202212227.2yaiv41e5ok0.png)
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/归并排序.5xyk55s6xjc0.gif)
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/合并.2gev4sm7ifbw.png)
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/微信截图_20210203150013.4zfufjynrq00.png)
```java
class Solution {
public int[] sortArray(int[] nums) {
mergeSort(nums,0,nums.length-1);
return nums;
}
public void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = left + ((right - left) >> 1);
mergeSort(arr,left,mid);
mergeSort(arr,mid+1,right);
merge(arr,left,mid,right);
}
}
//归并
public void merge(int[] arr,int left, int mid, int right) {
//第一步,定义一个新的临时数组
int[] temparr = new int[right -left + 1];
int temp1 = left, temp2 = mid + 1;
int index = 0;
//对应第二步,比较每个指针指向的值,小的存入大集合
while (temp1 <= mid && temp2 <= right) {
if (arr[temp1] <= arr[temp2]) {
temparr[index++] = arr[temp1++];
} else {
temparr[index++] = arr[temp2++];
}
}
//对应第三步,将某一小集合的剩余元素存到大集合中
if (temp1 <= mid) System.arraycopy(arr, temp1, temparr, index, mid - temp1 + 1);
if (temp2 <= right) System.arraycopy(arr, temp2, temparr, index, right -temp2 + 1); //将大集合的元素复制回原数组
System.arraycopy(temparr,0,arr,0+left,right-left+1);
}
}
```
****
O(n) logn, O(nlogn) O(nlogn) 广 O(1)
****
n , O(n).
****
merge arr[temp1] <= arr[temp2] arr[temp1]
| | | | | | |
| -------- | -------------- | -------------- | -------------- | ---------- | -------- |
| | O(nlogn) | O(nlogn) | O(nlogn) | O(n) | |
![](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/归并排序迭代.4zx9uezcky80.gif)
1,2,4,8,.....
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/微信截图_20210203205336.4j443ciyj7u0.png)
1 [3],[1][3],[1]
[4],[6] 2
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/微信截图_20210203210041.2t0e1gji8xy0.png)
2 [2,5],[1,3]
merge
```java
class Solution {
public int[] sortArray (int[] nums) {
//代表子集合大小124816.....
int k = 1;
int len = nums.length;
while (k < len) {
mergePass(nums,k,len);
k *= 2;
}
return nums;
}
public void mergePass (int[] array, int k, int len) {
int i;
for (i = 0; i < len-2*k; i += 2*k) {
//归并
merge(array,i,i+k-1,i+2*k-1);
}
//归并最后两个序列
if (i + k < len) {
merge(array,i,i+k-1,len-1);
}
}
public void merge (int[] arr,int left, int mid, int right) {
//第一步,定义一个新的临时数组
int[] temparr = new int[right -left + 1];
int temp1 = left, temp2 = mid + 1;
int index = 0;
//对应第二步,比较每个指针指向的值,小的存入大集合
while (temp1 <= mid && temp2 <= right) {
if (arr[temp1] <= arr[temp2]) {
temparr[index++] = arr[temp1++];
} else {
temparr[index++] = arr[temp2++];
}
}
//对应第三步,将某一小集合的剩余元素存到大集合中
if (temp1 <= mid) System.arraycopy(arr, temp1, temparr, index, mid - temp1 + 1);
if (temp2 <= right) System.arraycopy(arr, temp2, temparr, index, right -temp2 + 1);
//将大集合的元素复制回原数组
System.arraycopy(temparr,0,arr,0+left,right-left+1);
}
}
```