Java quick sort, reading from a user input file into any array (to be sorted)
Date : March 29 2020, 07:55 AM
seems to work fine Some errors: public testScan1(String filename) actually doesn't have any return type and it's also called from a static context but it's not static. It should be changed to public static void testScan1(String filename). what is the purpose of file.hasNext()? Of course it doesn't exist because it doesn't mean anything. I think you meant scan.hasNext(). array cannot be found because it is defined inside a try/catch block so it's present only inside that scope. Move the definition before try.
|
Why does the Insertion Sort algorithm take up constant memory(if we disregard the input array)?
Date : March 29 2020, 07:55 AM
wish help you to fix your issue We are not declaring any auxillary data, other than some CONSTANT amount of variables (i,j,temp, maybe some more for invoking the function itself, still constants though), each of constant size. So we can bound the extra space we declare by a constant C=4*maxSize{i,j,temp}, and by definition of big O notaiton, it gives us O(1)
|
How can I sort each column in a user input 3x3 2d array from smallest to largest? in JAVA language
Date : March 29 2020, 07:55 AM
Hope that helps Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 You're getting this error because at the end of your for loops when you do ct++ you will eventually get ct=3. Since array indexing starts at 0, your 3x3 column indices will go from 0-2. So when ct=3 and you do matrix[ct].length you will get an array out of bounds error.
|
Is it possible to implement an Insertion Sort algorithm with a given user input without knowing it's size in Java?
Date : March 29 2020, 07:55 AM
To fix this issue Assuming the user inputs numbers in the form "1,2,3,7,79,9", you can convert that into an array of the proper length using String[] stringArr = userInput.split(",");
int[] nums = new int[stringArr.length];
for (int i = 0; i < nums.length; i++) {
nums[i] = Integer.parseInt(stringArr[i]);
}
|
10,000,000 Integer Array Insertion Sort in Java
Tag : java , By : user152423
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further So I have written the insertion sort code properly to where it will successfully create arrays of 10, 1,000, 100,000 and 1,000,000 integers between 1,000 and 9,999 and complete the insertion sort algorithm just fine. However, when I attempt the last step of 10,000,000 integers, the array is created, but the code never fully completes. I have allowed it plenty of time to complete, upwards of 4 or 5 hours, to no avail. Anybody have any ideas of what the issue may be here? Is the executer having issues comprehending that many integers or what could the issue stem from? I have included a copy of the insertion algorithm that I have written. , Anybody have any ideas of what the issue may be here? 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
|