- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用不同的算法对数组进行排序。当我使用少量数组运行应用程序时,它可以工作并产生结果。当数组大于:131072时,它将停止响应。
可能是什么原因?这段代码有什么问题吗?
这是我的功能:
boolean runTest(String[] text, int[] number, String url, String out, Sort sort) {
PrintWriter filename;
boolean tobeReturned = true;
String beforeSorting = "";
String afterSorting = "";
long startTime;
double timeTaken;
try {
filename = createWriter(out);
for(File directory : new File(url).listFiles()){
File[] listOfFiles = directory.listFiles();
filename.println("Number of Records: \t" + directory.getName());
for (File file : listOfFiles) {
text = loadStrings(file);
number = int(text);
if (isSorted(number)) { beforeSorting = "Sorted";} else { beforeSorting = "NOT Sorted"; };
startTime = startTime();
sort.sortInteger(number);
timeTaken = stopTime(startTime);
if (isSorted(number)) { afterSorting = "Sorted"; } else { afterSorting = "NOT Sorted"; };
filename.println("File Set " + file.getName() + ": \t\t" + beforeSorting + ": \t" + afterSorting + ": \t" + timeTaken);
timeTaken = 0;
}
filename.println("\n");
}
filename.flush();
filename.close();
}
catch (Exception e) {
tobeReturned = false;
}
return tobeReturned;
}
interface Sort{
public int[] sortInteger(int[] input);
}
class Insertion implements Sort{
Insertion() {
}
int[] sortInteger(int[] input) {
int i, j, tobeSorted;
for (i = 1; i < input.length; i++) {
tobeSorted = input[i];
j = i;
while (j > 0 && input[j - 1] > tobeSorted) {
input[j] = input[j - 1];
j--;
}
input[j] = tobeSorted;
}
//println(input);
return input;
}
}
import java.util.*;
long a = 9; // total loop, 9 means = 65536, 10 means = 131072 ...
long b = 2; // multiplier, 2 means = 512,1024,2048...
long c = 512; // starting number
long d = 5; // times random text file
String url;
Insertion insertion;
Merge merge;
Bubble bubble;
Shell shell;
QuickSort quickSort;
void setup() {
url = sketchPath("_numbers/");
insertion = new Insertion();
merge = new Merge();
bubble = new Bubble();
shell = new Shell();
quickSort = new QuickSort();
generateFiles(a,b,c,d);
boolean runSortTest = false;
runSortTest = runTest(url, "_results/a_insertion.txt", insertion);
runSortTest = runTest(url, "_results/b_merge.txt", merge);
runSortTest = runTest(url, "_results/c_bubble.txt", bubble);
runSortTest = runTest(url, "_results/d_shell.txt", shell);
runSortTest = runTest(url, "_results/e_quickSort.txt", quickSort);
if(runSortTest){
println("Done");
}else{
println("Error");
}
}
boolean generateFiles(long totalLoop, long multiplier, long power, long fileNumber){
PrintWriter pw;
//int orderCount = 1;
int count = 1;
//boolean tobeReturned = true;
try {
for (int i = 1; i < totalLoop; i = i+1) {
for (int j = 1; j < fileNumber+1; j = j+1) {
pw = createWriter("/_numbers/" + power + "/" + count + ".txt");
for (int k = 0; k < power; k = k+1) {
pw.println(randomNumber(0, power));
//pw.write(int(randomNumber(0, power)) + "\t");
}
count++;
pw.flush(); // Writes the remaining data to the file
pw.close(); // Finishes the file
}
count = 1;
//orderCount++;
power *= multiplier;
}
//orderCount = 1;
return true;
} catch (Exception e) {
return false;
}
}
long randomNumber(long min, long max){
long randomN = (long)random(min,(max + 1));
return randomN;
}
//####################################################################################################
//## Runs the test and produces a log file for each algorithms
//####################################################################################################
boolean runTest(String url, String out, Sort sort) {
PrintWriter filename;
boolean tobeReturned = true;
String beforeSorting = "";
String afterSorting = "";
long startTime;
double timeTaken;
try {
filename = createWriter(out);
for(File directory : new File(url).listFiles()){
File[] listOfFiles = directory.listFiles();
filename.println("Number of Records: \t" + directory.getName());
for (File file : listOfFiles) {
String[] text; int[] number;
text = loadStrings(file);
number = int(text);
if (isSorted(number)) { beforeSorting = "Sorted";} else { beforeSorting = "NOT Sorted"; };
startTime = startTime();
sort.sortInteger(number);
timeTaken = stopTime(startTime);
if (isSorted(number)) { afterSorting = "Sorted"; } else { afterSorting = "NOT Sorted"; };
filename.println("File Set " + file.getName() + ": \t\t" + beforeSorting + ": \t" + afterSorting + ": \t" + timeTaken);
timeTaken = 0;
Arrays.fill(text, null);
number = null;
}
filename.println("\n");
}
filename.flush();
filename.close();
}
catch (Exception e) {
e.printStackTrace();
tobeReturned = false;
}
return tobeReturned;
}
boolean isSorted(int[] array) {
for (int i = 0; i < array.length-1; i ++) {
if (array[i] > array[i+1]) {
return false;
}
}
return true;
}
//####################################################################################################
//## Time comparison
//####################################################################################################
long startTime() {
return System.nanoTime();
}
double stopTime(long startTime) {
double finalTime = (System.nanoTime() - startTime)/1000000000.0;
return finalTime;
}
/*
Interface
# Last update: 20 October 2015
*/
interface Sort{
public int[] sortInteger(int[] input);
}
/*
Insertion class, implements Sort interface
# Last update: 25 October 2015
*/
class Insertion implements Sort{
Insertion() {
}
int[] sortInteger(int[] input) {
int i, j, tobeSorted;
for (i = 1; i < input.length; i++) {
tobeSorted = input[i];
j = i;
while (j > 0 && input[j - 1] > tobeSorted) {
input[j] = input[j - 1];
j--;
}
input[j] = tobeSorted;
}
//println(input);
return input;
}
}
/*
Merge class, implements Sort interface
# Last update: 25 October 2015
*/
class Merge implements Sort{
Merge() {
}
int[] sortInteger(int[] input) {
if (input.length > 1) {
// split array into two halves
int[] left = leftHalf(input);
int[] right = rightHalf(input);
// recursively sort the two halves
sortInteger(left);
sortInteger(right);
// merge the sorted halves into a sorted whole
merge(input, left, right);
}
return input;
}
// Returns the first half of the given array.
int[] leftHalf(int[] array) {
int size1 = array.length / 2;
int[] left = new int[size1];
for (int i = 0; i < size1; i++) {
left[i] = array[i];
}
return left;
}
// Returns the second half of the given array.
int[] rightHalf(int[] array) {
int size1 = array.length / 2;
int size2 = array.length - size1;
int[] right = new int[size2];
for (int i = 0; i < size2; i++) {
right[i] = array[i + size1];
}
return right;
}
void merge(int[] result, int[] left, int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
for (int i = 0; i < result.length; i++) {
if (i2 >= right.length || (i1 < left.length &&
left[i1] <= right[i2])) {
result[i] = left[i1]; // take from left
i1++;
} else {
result[i] = right[i2]; // take from right
i2++;
}
}
}
}
/*
Bubble class, implements Sort interface
# Last update: 25 October 2015
*/
class Bubble implements Sort {
Bubble() {
}
int[] sortInteger(int[] input) {
boolean swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < input.length - j; i++) {
if (input[i] > input[i + 1]) {
tmp = input[i];
input[i] = input[i + 1];
input[i + 1] = tmp;
swapped = true;
}
}
}
return input;
}
}
/*
Shell class, implements Sort interface
# Last update: 25 October 2015
*/
class Shell implements Sort {
Shell() {
}
int[] sequence = {59724292, 26544130, 11797391, 5243258, 2330349, 1035711, 460316, 204585, 90927, 40412, 17961, 7983, 3548, 1577, 701, 301, 132, 57, 23, 10, 4, 1};
/*
int number = 701;
for(int i=0; i<15; i++){
int newN = int(number*2.25);
println(number);
number = newN;
}
*/
int[] sortInteger (int[] input) {
int size = input.length;
int i, j, temp;
for ( int s : sequence ) {
i = s;
while ( i < size ) {
temp = input[i];
j = i-s;
while ( j >= 0 && input[j] > temp ) {
input[j + s] = input[j];
j -= s;
}
input[j + s] = temp;
i ++;
}
}
return input;
}
}
/*
QuickSort class, implements Sort interface
# Last update: 26 October 2015
*/
class QuickSort implements Sort {
QuickSort() {
}
int[] sortInteger(int[] input) {
quickSort(input, 0, input.length-1) ;
return input;
}
public void quickSort(int arr[], int low, int high)
{
int i = low, j = high;
int temp;
int pivot = arr[(low + high) / 2];
/** partition **/
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{
/** swap **/
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
/** recursively sort lower half **/
if (low < j)
quickSort(arr, low, j);
/** recursively sort upper half **/
if (i < high)
quickSort(arr, i, high);
}
}
最佳答案
解决这个问题的第一步是找出问题所在。这就是我们要求MCVE时的意思。您必须弄清楚问题出在哪里。一种简单的方法是添加println()
语句:
boolean runSortTest = false;
println("1");
runSortTest = runTest(url, "_results/a_insertion.txt", insertion);
println("2");
runSortTest = runTest(url, "_results/b_merge.txt", merge);
println("3");
runSortTest = runTest(url, "_results/c_bubble.txt", bubble);
println("4");
runSortTest = runTest(url, "_results/d_shell.txt", shell);
println("5");
runSortTest = runTest(url, "_results/e_quickSort.txt", quickSort);
println("6");
boolean runSortTest = false;
println("1");
runSortTest = runTest(url, "_results/a_insertion.txt", insertion);
println("2");
runSortTest = runTest(url, "_results/b_merge.txt", merge);
println("3");
//runSortTest = runTest(url, "_results/c_bubble.txt", bubble);
println("4");
runSortTest = runTest(url, "_results/d_shell.txt", shell);
println("5");
runSortTest = runTest(url, "_results/e_quickSort.txt", quickSort);
println("6");
import java.util.*;
long a = 9; // total loop, 9 means = 65536, 10 means = 131072 ...
long b = 2; // multiplier, 2 means = 512,1024,2048...
long c = 512; // starting number
long d = 5; // times random text file
String url;
Bubble bubble;
void setup() {
url = sketchPath("_numbers/");
bubble = new Bubble();
generateFiles(a, b, c, d);
boolean runSortTest = false;
runSortTest = runTest(url, "_results/c_bubble.txt", bubble);
if (runSortTest) {
println("Done");
} else {
println("Error");
}
}
boolean generateFiles(long totalLoop, long multiplier, long power, long fileNumber) {
PrintWriter pw;
//int orderCount = 1;
int count = 1;
//boolean tobeReturned = true;
try {
for (int i = 1; i < totalLoop; i = i+1) {
for (int j = 1; j < fileNumber+1; j = j+1) {
pw = createWriter("/_numbers/" + power + "/" + count + ".txt");
for (int k = 0; k < power; k = k+1) {
pw.println(randomNumber(0, power));
//pw.write(int(randomNumber(0, power)) + "\t");
}
count++;
pw.flush(); // Writes the remaining data to the file
pw.close(); // Finishes the file
}
count = 1;
//orderCount++;
power *= multiplier;
}
//orderCount = 1;
return true;
}
catch (Exception e) {
return false;
}
}
long randomNumber(long min, long max) {
long randomN = (long)random(min, (max + 1));
return randomN;
}
//####################################################################################################
//## Runs the test and produces a log file for each algorithms
//####################################################################################################
boolean runTest(String url, String out, Sort sort) {
PrintWriter filename;
boolean tobeReturned = true;
String beforeSorting = "";
String afterSorting = "";
long startTime;
double timeTaken;
try {
filename = createWriter(out);
for (File directory : new File(url).listFiles()) {
File[] listOfFiles = directory.listFiles();
filename.println("Number of Records: \t" + directory.getName());
for (File file : listOfFiles) {
String[] text;
int[] number;
text = loadStrings(file);
number = int(text);
if (isSorted(number)) {
beforeSorting = "Sorted";
} else {
beforeSorting = "NOT Sorted";
};
startTime = startTime();
sort.sortInteger(number);
timeTaken = stopTime(startTime);
if (isSorted(number)) {
afterSorting = "Sorted";
} else {
afterSorting = "NOT Sorted";
};
filename.println("File Set " + file.getName() + ": \t\t" + beforeSorting + ": \t" + afterSorting + ": \t" + timeTaken);
timeTaken = 0;
Arrays.fill(text, null);
number = null;
}
filename.println("\n");
}
filename.flush();
filename.close();
}
catch (Exception e) {
e.printStackTrace();
tobeReturned = false;
}
return tobeReturned;
}
boolean isSorted(int[] array) {
for (int i = 0; i < array.length-1; i ++) {
if (array[i] > array[i+1]) {
return false;
}
}
return true;
}
//####################################################################################################
//## Time comparison
//####################################################################################################
long startTime() {
return System.nanoTime();
}
double stopTime(long startTime) {
double finalTime = (System.nanoTime() - startTime)/1000000000.0;
return finalTime;
}
/*
Interface
# Last update: 20 October 2015
*/
interface Sort {
public int[] sortInteger(int[] input);
}
/*
Bubble class, implements Sort interface
# Last update: 25 October 2015
*/
class Bubble implements Sort {
Bubble() {
}
int[] sortInteger(int[] input) {
boolean swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < input.length - j; i++) {
if (input[i] > input[i + 1]) {
tmp = input[i];
input[i] = input[i + 1];
input[i + 1] = tmp;
swapped = true;
}
}
}
return input;
}
}
println()
语句是您最好的朋友:
int[] sortInteger(int[] input) {
println("input length: " + input.length);
input length: 1024
input length: 1024
input length: 1024
input length: 1024
input length: 1024
input length: 16384
input length: 16384
input length: 16384
input length: 16384
input length: 16384
input length: 2048
input length: 2048
input length: 2048
input length: 2048
input length: 2048
input length: 32768
input length: 32768
input length: 32768
input length: 32768
input length: 32768
input length: 4096
input length: 4096
input length: 4096
input length: 4096
input length: 4096
input length: 512
input length: 512
input length: 512
input length: 512
input length: 512
input length: 65536
input length: 65536
input length: 65536
input length: 65536
input length: 65536
input length: 8192
input length: 8192
input length: 8192
input length: 8192
input length: 8192
Done
c_bubble.txt
中得到的输出:
File Set 1.txt: NOT Sorted: Sorted: 0.009598049335181713
File Set 2.txt: NOT Sorted: Sorted: 0.0011289309477433562
File Set 3.txt: NOT Sorted: Sorted: 0.0026859149802476168
File Set 4.txt: NOT Sorted: Sorted: 0.002715847920626402
File Set 5.txt: NOT Sorted: Sorted: 0.0015163590433076024
Number of Records: 16384
File Set 1.txt: NOT Sorted: Sorted: 0.38029658794403076
File Set 2.txt: NOT Sorted: Sorted: 0.37928950786590576
File Set 3.txt: NOT Sorted: Sorted: 0.3931492865085602
File Set 4.txt: NOT Sorted: Sorted: 0.3918215036392212
File Set 5.txt: NOT Sorted: Sorted: 0.38513386249542236
Number of Records: 2048
File Set 1.txt: NOT Sorted: Sorted: 0.0040453351102769375
File Set 2.txt: NOT Sorted: Sorted: 0.0041389851830899715
File Set 3.txt: NOT Sorted: Sorted: 0.004071420058608055
File Set 4.txt: NOT Sorted: Sorted: 0.004472961183637381
File Set 5.txt: NOT Sorted: Sorted: 0.0041146110743284225
Number of Records: 32768
File Set 1.txt: NOT Sorted: Sorted: 1.6499463319778442
File Set 2.txt: NOT Sorted: Sorted: 1.6393990516662598
File Set 3.txt: NOT Sorted: Sorted: 1.628066897392273
File Set 4.txt: NOT Sorted: Sorted: 1.6488127708435059
File Set 5.txt: NOT Sorted: Sorted: 1.6586071252822876
Number of Records: 4096
File Set 1.txt: NOT Sorted: Sorted: 0.0184687077999115
File Set 2.txt: NOT Sorted: Sorted: 0.018312623724341393
File Set 3.txt: NOT Sorted: Sorted: 0.018741531297564507
File Set 4.txt: NOT Sorted: Sorted: 0.01845288649201393
File Set 5.txt: NOT Sorted: Sorted: 0.018356671556830406
Number of Records: 512
File Set 1.txt: NOT Sorted: Sorted: 3.81015008315444E-4
File Set 2.txt: NOT Sorted: Sorted: 3.322649863548577E-4
File Set 3.txt: NOT Sorted: Sorted: 3.3953398815356195E-4
File Set 4.txt: NOT Sorted: Sorted: 3.314100031275302E-4
File Set 5.txt: NOT Sorted: Sorted: 3.42526996973902E-4
Number of Records: 65536
File Set 1.txt: NOT Sorted: Sorted: 6.721750736236572
File Set 2.txt: NOT Sorted: Sorted: 6.752647399902344
File Set 3.txt: NOT Sorted: Sorted: 6.7578630447387695
File Set 4.txt: NOT Sorted: Sorted: 6.696788787841797
File Set 5.txt: NOT Sorted: Sorted: 6.759160995483398
Number of Records: 8192
File Set 1.txt: NOT Sorted: Sorted: 0.08915259689092636
File Set 2.txt: NOT Sorted: Sorted: 0.09149085730314255
File Set 3.txt: NOT Sorted: Sorted: 0.0899868980050087
File Set 4.txt: NOT Sorted: Sorted: 0.08892638236284256
File Set 5.txt: NOT Sorted: Sorted: 0.08631659299135208
draw()
函数,因此只会看到一个灰色框。如果不希望弹出窗口,请尝试在
exit()
函数末尾添加对
setup()
的调用。
draw()
函数,因此您的程序没有任何视觉效果,因此它所做的只是显示一个灰色框。这并不意味着您的程序没有响应!这只是意味着您没有告诉它做任何事情。考虑在
exit()
函数的末尾添加对
setup()
的调用。
int[] sortInteger(int[] input) {
println("input length: " + input.length);
println()
语句是您最好的朋友。尝试添加一个
println()
语句,该语句在插入排序循环中向您显示
i
的值:
int i, j, tobeSorted;
for (i = 1; i < input.length; i++) {
tobeSorted = input[i];
j = i;
while (j > 0 && input[j - 1] > tobeSorted) {
input[j] = input[j - 1];
j--;
}
input[j] = tobeSorted;
println("i: " + i);
}
void setup() {
int[] input = getInput(1000);
int start = millis();
bubbleSort(input);
int elapsed = millis() - start;
println("Elapsed: " + elapsed);
exit();
}
int[] getInput(int n) {
int[] input = new int[n];
for (int i = 0; i < n; i++) {
input[i] = (int)random(n);
}
return input;
}
int[] bubbleSort(int[] input) {
boolean swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < input.length - j; i++) {
if (input[i] > input[i + 1]) {
tmp = input[i];
input[i] = input[i + 1];
input[i + 1] = tmp;
swapped = true;
}
}
}
return input;
}
getInput()
函数的值,就可以看到它如何影响算法完成的时间。
N: 1,000 Time: 7
N: 10,000 Time: 145
N: 100,000 Time: 14600
N: 1,000,000 Time: 1467533
N: 1,000 Time: 1
N: 10,000 Time: 5
N: 100,000 Time: 38
N: 1,000,000 Time: 221
N: 10,000,000 Time: 2202
println()
语句以查看程序在哪里花费时间。这就是您解决问题的方式。
println()
语句是您最好的朋友。
关于java - 不同的排序算法在大型阵列上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33387339/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!