归并排序Java实现
归并排序是时间复杂度最低的排序算法,效率十分高,(其他排序,可见我的其他博文)
/**
* 归并排序
* 算法思想:
*用分治的思想,不断把原来的数组分割为原来的二分之一,直至只剩下一个元素,
*即为已经排好序的单元素数组,然后开始合并排序,最后形成排好序的数组
* 算法复杂度为:O(nlogn)
*/
public class MergeSort{
//归并排序的入口,这个算法体主要进行数组拆分和调用合并函数,进而完成整个合并排序算法。
public static void mergeSort(int arr[]){
if(arr.length > 1){
//分为左右两部分,然后进行递归,
//直到递归的数组长度为1的时候,开始一步一步向上合并
int[] firstHalf = new int[arr.length/2];
System.arraycopy(arr,0,firstHalf,0,arr.length/2);
mergeSort(firstHalf);
int[] secondHalf = new int[arr.length - firstHalf.length];
System.arraycopy(arr,arr.length/2,secondHalf,0,secondHalf.length);
mergeSort(secondHalf);
merge(firstHalf,secondHalf,arr);
}
}
//合并函数,把拆分的数组合并起来
public static void merge(int[] firstHalf,int[] secondHalf,int[] temp){
int current1 = 0;
int current2 = 0;
int current3 = 0;
while(current1 < firstHalf.length && current2 < secondHalf.length){
if (firstHalf[current1] < secondHalf[current2]){
temp[current3++] = firstHalf[current1++];
}else{
temp[current3++] = secondHalf[current2++];
}
}
while(current1 < firstHalf.length){
temp[current3++] = firstHalf[current1++];
}
while(current2 < secondHalf.length){
temp[current3++] = secondHalf[current2++];
}
}
}