Java合并排序

来源:互联网 发布:python登录文件夹帐号 编辑:程序博客网 时间:2024/06/16 14:21
import java.lang.reflect.Array;import java.util.Arrays;public class Test {public static int[] sort(int[] nums,int low, int high){int mid = (low+high)/2;if(low < high){sort(nums,low,mid);sort(nums,mid+1,high);System.out.println("---"+low+"-----"+mid+"----"+high);merge(nums,low,mid,high);}return nums;}public static void merge(int[] nums,int low,int mid,int high){int[] temp = new int[high-low+1];int i = low;int j = mid + 1;int k = 0;while(i <= mid && j <= high){if(nums[i] < nums[j]){ temp[k++] = nums[i++];}else{temp[k++] = nums[j++];}}while(i <= mid){temp[k++] = nums[i++];}while(j <= high){temp[k++] = nums[j++];}for(int k2 = 0; k2 < temp.length; k2++){nums[k2+low] = temp[k2];}System.out.println("----"+Arrays.toString(nums));}public static void main(String[] args) {int[] nums = {2,3,7,8,1,6,9,0,5,4};sort(nums,0,nums.length-1);System.out.println(Arrays.toString(nums));}}

0 0
原创粉丝点击