10,000,000整数数组插入以Java格式排序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了10,000,000整数数组插入以Java格式排序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1797字,纯文字阅读大概需要3分钟。
内容图文
![10,000,000整数数组插入以Java格式排序](/upload/InfoBanner/zyjiaocheng/782/e84d11507e7243da9848703a29510a85.jpg)
所以我已经将插入排序代码正确编写到它将成功创建介于1,000和9,999之间的10,1,000,100,000和1,000,000个整数的数组,并完成插入排序算法.但是,当我尝试10,000,000个整数的最后一步时,会创建数组,但代码永远不会完全完成.我已经允许它有足够的时间来完成,超过4或5个小时,但无济于事.有人对这里的问题有什么看法吗?执行者是否有问题理解许多整数或问题源于什么?我已经包含了我编写的插入算法的副本.
public static void insertion(int[] a) {
int n = a.length;
for(int i = 1; i < n; i++) {
int j = i -1;
int temp = a[i];
while(j > 0 && temp < a[j]) {
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
}
解决方法:
Anybody have any ideas of what the issue may be here?
当您将阵列放大10倍时,您必须等待100倍,因为这是一个O(n ^ 2)算法.
Is the executer having issues comprehending that many integers or what could the issue stem from?
不,限制是2 ^ 31-1,你距离极限很远.
运行
interface A {
static void main(String[] a) {
for (int i = 25_000; i <= 10_000_000; i *= 2) {
Random r = new Random();
int[] arr = new int[i];
for (int j = 0; j < i; j++)
arr[j] = r.nextInt();
long start = System.currentTimeMillis();
insertion(arr);
long time = System.currentTimeMillis() - start;
System.out.printf("Insertion sort of %,d elements took %.3f seconds%n",
i, time / 1e3);
}
}
public static void insertion(int[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
int j = i - 1;
int temp = a[i];
while (j > 0 && temp < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
}
版画
Insertion sort of 25,000 elements took 0.049 seconds
Insertion sort of 50,000 elements took 0.245 seconds
Insertion sort of 100,000 elements took 1.198 seconds
Insertion sort of 200,000 elements took 4.343 seconds
Insertion sort of 400,000 elements took 19.212 seconds
Insertion sort of 800,000 elements took 71.297 seconds
所以我的机器可能需要大约4个小时,但是可能需要更长的时间,因为更大的数据集不适合L3缓存,而是更慢的主内存.
内容总结
以上是互联网集市为您收集整理的10,000,000整数数组插入以Java格式排序全部内容,希望文章能够帮你解决10,000,000整数数组插入以Java格式排序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。