现在我在10000个randoms数字的数组上运行插入排序。当我运行时,我没有得到任何结果/输出。但是如果我将数组的大小减小到100我得到结果。有什么想法?代码如下:
import java.util.Random;
public class Insertion {
public static void main(String a[]) {
int i;
int[] arr = new int[10000];
Random randomGenerator = new Random();
for (int idx = 0; idx < 10000; ++idx) {
arr[idx] = randomGenerator.nextInt(100000);
}
System.out.println("\n\n Straight Isertion sort\n\n");
System.out.println(" Result Sort\n\n");
System.out.println("Values Before the sort:\n");
for (i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
insertion_srt(arr, arr.length);
System.out.print("Values after the sort:\n");
for (i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
System.out.println("PAUSE");
}
public static void insertion_srt(int arr[], int n) {
for (int i = 1; i < n; i++) {
int j = i;
int B = arr[i];
while ((j > 0) && (arr[j - 1] > B)) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = B;
}
}
}
输出: