Merge 33dae6dc891b26d53381623dd5906082e7dda0f6 into d38ec3fe06b95a6bf1abe7ac4aa3fd8622d1c375
This commit is contained in:
commit
9dd97530e5
50
notes/算法.md
50
notes/算法.md
@ -421,6 +421,56 @@ public class Shell<T extends Comparable<T>> extends Sort<T> {
|
|||||||
|
|
||||||
### 1. 归并方法
|
### 1. 归并方法
|
||||||
|
|
||||||
|
package sortdemo;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by chengxiao on 2016/12/8.
|
||||||
|
*/
|
||||||
|
public class MergeSort {
|
||||||
|
public static void main(String []args){
|
||||||
|
int []arr = {9,8,7,6,5,4,3,2,1};
|
||||||
|
sort(arr);
|
||||||
|
System.out.println(Arrays.toString(arr));
|
||||||
|
}
|
||||||
|
public static void sort(int []arr){
|
||||||
|
int []temp = new int[arr.length];//在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
|
||||||
|
sort(arr,0,arr.length-1,temp);
|
||||||
|
}
|
||||||
|
private static void sort(int[] arr,int left,int right,int []temp){
|
||||||
|
if(left<right){
|
||||||
|
int mid = (left+right)/2;
|
||||||
|
sort(arr,left,mid,temp);//左边归并排序,使得左子序列有序
|
||||||
|
sort(arr,mid+1,right,temp);//右边归并排序,使得右子序列有序
|
||||||
|
merge(arr,left,mid,right,temp);//将两个有序子数组合并操作
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private static void merge(int[] arr,int left,int mid,int right,int[] temp){
|
||||||
|
int i = left;//左序列指针
|
||||||
|
int j = mid+1;//右序列指针
|
||||||
|
int t = 0;//临时数组指针
|
||||||
|
while (i<=mid && j<=right){
|
||||||
|
if(arr[i]<=arr[j]){
|
||||||
|
temp[t++] = arr[i++];
|
||||||
|
}else {
|
||||||
|
temp[t++] = arr[j++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(i<=mid){//将左边剩余元素填充进temp中
|
||||||
|
temp[t++] = arr[i++];
|
||||||
|
}
|
||||||
|
while(j<=right){//将右序列剩余元素填充进temp中
|
||||||
|
temp[t++] = arr[j++];
|
||||||
|
}
|
||||||
|
t = 0;
|
||||||
|
//将temp中的元素全部拷贝到原数组中
|
||||||
|
while(left <= right){
|
||||||
|
arr[left++] = temp[t++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
归并方法将数组中两个已经排序的部分归并成一个。
|
归并方法将数组中两个已经排序的部分归并成一个。
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
Loading…
x
Reference in New Issue
Block a user