package tao.leetcode;
/**
* Created by Tao on 2017/7/30.
*/
public class MyLeetcode {
public static void main(String[] args) {
int[] list = {4,1,2,3,5};
Solution so = new Solution();
so.myMergeSort(list);
System.out.println(" ");
}
}
package tao.leetcode;
public class Solution {
/**归并排序*************************/
public void myMergeSort(int[] list) {
int low = 0;
int high = list.length - 1;
mergeSort(list,low,high);
}
private void mergeSort(int[] list, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
mergeSort(list,low,mid);
mergeSort(list,mid + 1,high);
merge(list,low,mid,high);
}
}
private void merge(int[] list, int low, int mid, int high) {
int[] tmp = new int[high - low + 1];
int i = low, j = mid + 1;
int k = 0;
while (i <= mid && j <= high) {
if (list[i] <= list[j]) {
tmp[k++] = list[i++];
} else {
tmp[k++] = list[j++];
}
}
while (i <= mid) {
tmp[k++] = list[i++];
}
while (j <= high) {
tmp[k++] = list[j++];
}
for (int ind = 0; ind < tmp.length; ind++) {
list[low + ind] = tmp[ind];
}
}
}
package tao.leetcode;/** * Created by Tao on 2017/7/30. */public class MyLeetcode { public static void main(String[] args) { int[] list = {4,1,2,3,5}; Solution so = new Solutio...