Show HN: Design, data-structure, algorithm problems and their solutions
github.com
github.com
/ build the boolean result array
boolean[] result = new boolean[array.length];
for(int index = 0; index < array.length; index++) {
int num = array[index];
result[num] = true;
}
The result array is the same size as the original array.But when you do:
int num = array[index];
result[num] = true;
The index being referenced in the result array is dependent on the value of num. So if num is greater than or equal to the length of the array, it throws an ArrayOutOfBoundsException. And what happens when you try sorting an array with elements having the same value?Eg- try sorting {10,5,9}
Unrelated: HN needs to allow highlighting code inline.
boolean[] result = new boolean[Math.max(array)];
Consider sorting an array of 2 large integers {65000,30000}. To sort this array, the result array will be initialized with 65000 elements. Very inefficient.
You should look into bucketsort. That allows you to sort in finite universes in Theta(n+k) without allocating huge arrays for large integers.
The fastest time to sort an integer array is O(N)
It could be interpreted as generally applicable. The proposed method uses reverse-indexed flagging, an approach which isn't really feasible if element values are very large.
Generally, the lower bound for any comparison based sorting algorithm is Ω( n log( n )).
Sorting in θ(n+k) w.c. and a.c. (most of the time this is equal to O(n)) is possible for finite boundaries that allows for countingsort or bucketsort (radixsort uses countingsort internally).
Some implementations of those algorithms in Python: https://github.com/MauriceGit/Advanced_Algorithms
"Fastest sorting of bounded integers"
I so intend to write on some pieces of software architecture that I have understanding of. For micro-services piece, if you specific sample well-defined problems, please do let me know and I would like to cover it up.
Integer Sorting in 0(n sqrt (log log n)) Expected Time and Linear Space, Yijie Han and Mikkel Thorup, FOCS '02 Proceedings of the 43rd Symposium on Foundations of Computer Science Pages 135-144
http://dl.acm.org/citation.cfm?id=652131
... Just thought I would throw it out there.
I will go over the mentioned paper and also mention it as a reference.
But I still can't solve last years' AoC. Oh well. I guess I'll just have to keep working on it.