1. Estructura y algoritmo de datos:
1.1. Cree un método de clasificación por combinación:
public static void mergerSort(int[] arr, int left, int right, int[] temp) {
if(left < right) {
int mid = (left+right) / 2;
mergerSort(arr, left, mid, temp);
mergerSort(arr, mid+1, right, temp);
merger(arr, left, mid, right, temp);
}
}
1.2. Cree un método de combinación para la ordenación por combinación:
public static void merger(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];
t++;
i++;
}else{
temp[t] = arr[j];
t++;
j++;
}
}
while(i <= mid){
temp[t] = arr[i];
t++;
i++;
}
while(j <= right){
temp[t] = arr[j];
t++;
j++;
}
t=0;
int tempLeft = left;
while(tempLeft <= right){
arr[tempLeft] = temp[t];
t++;
tempLeft++;
}
}
1.3, método principal:
public static void main(String[] args) {
//随机创建8万 个数据
int arr[]= new int[80000];
int temp[] = new int[arr.length];
for(int i=0; i<arr.length; i++){
arr[i] = (int)(Math.random()*80000);
}
//计算 冒泡排序时间
double start = System.currentTimeMillis();
System.out.println("计时开始:");
mergerSort(arr,0, arr.length-1, temp);//此处写要测试的代码
double end = System.currentTimeMillis();
System.out.println("计时结束, 共耗时:"+ (double)((end-start)/1000) +"秒");
}
• Escrito por ChiKong_Tam el 10 de septiembre de 2020