class Program { static void Main(string[] args) { int[] arr = arrInsert(100000); merge_sort(arr,0,arr.Length-1); } static int[] arrInsert(int count) { int[] arr=new int[count]; Random r1=new Random(); int i=0; while(i<count) { int num=r1.Next(0,10000); arr[i]=num; i++; } return arr; } static void merge(int[] arr, int L, int M, int R) { int left_size = M-L; //左半部分数组的长度 int right_size = R-M+1; //右半部分数组的长度 int[] L_arr = new int[left_size]; //声明数组 int[] R_arr = new int[right_size]; int i = 0, j = 0, k = 0; for ( i = L; i < M; i++) //数组内容 { L_arr[i - L] = arr[i]; } for ( i = M; i <= R; i++) { R_arr[i - M] = arr[i]; } i = 0; j = 0; k = L; while (i < left_size && j < right_size) { if (L_arr[i] < R_arr[j]) { arr[k++] = L_arr[i++]; } else { arr[k++] = R_arr[j++]; } } while (i < left_size) { arr[k++] = L_arr[i++]; } while (j < right_size) { arr[k++] = R_arr[j++]; } } static void merge_sort(int[]arr, int L, int R) { if(L == R) { return; } else { int M = (L+R)/2; merge_sort(arr, L, M); merge_sort(arr, M+1, R); merge(arr, L, M+1, R); } } }