7 ms·
arrayToSort[sum] = 1; This is just blatant programmer error. The code is attempting to assign a value to a slot in an array of a fixed size, which does not exi
by iCannotEven 8y ago
arrayToSort[sum] = 1;
This is just blatant programmer error. The code is attempting to assign a value to a slot in an array of a fixed size, which does not exist.
Use:
Integer[] arrayToSort = new Integer[2000000000];
No error.
- toxik 8y agoThe equivalent Python code works fine, though. >>> a=[0]*sum(rls) >>> sum=-1 >>> for i in rls: ... sum += i ... a[sum] = 1 ... >>> a.sort() >>> Takes a real good while, too.
- iCannotEven 8y agoThat's because the array to be sorted is packed and inflated, so as to be the worst possible input for that kind of sort. Worst-case complexity. Also, I needed to bump my JVM heap up to 16GB (not 9GB as recommended), just to run it.
- sdinsn 8y agoYou are incorrect. Look at the error message more closely: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 49 at java.util.ComparableTimSort.pushRun(Unknown Source) at java.util.ComparableTimSort.sort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at Test.main(Test.java:80) The error occurs inside TimSort, not at arrayToSort[sum] = 1; It's a bug with TimSort going out of bounds (which obviously shouldn't happen ever), not the Test.