https://kotlinlang.org logo
Title
g

g4sarma

08/29/2018, 3:04 PM
Hello guys, what is the fastest way to sort an Int[] ?
d

diesieben07

08/29/2018, 3:09 PM
Just call
sort
😛
g

g4sarma

08/29/2018, 3:10 PM
I am aware about the sort() in kotlin. is that good enough to take care of the performance @evanchooly
d

diesieben07

08/29/2018, 3:11 PM
Yes.
You can usually trust the JDK to know what they are doing 🙂
g

g4sarma

08/29/2018, 3:13 PM
Cool, thanks for that link to the doc
e

evanchooly

08/29/2018, 3:14 PM
“do the simplest thing that can possibly work.” then optimize as necessary.
💯 2
👍 1
s

Shawn

08/29/2018, 3:14 PM
you can always just
ctrl/cmd+b
into the definition of the method in question…
many of the standard extensions provided on collection types are backed by stuff in the java stdlib anyhow
and the docs will be right there
j

Jeremy Rempel

08/29/2018, 4:00 PM
if its a known range ie, 1-100 you can do in o(n) time at cost of memory
but otherwise quicksort in nlogn
g

g4sarma

08/29/2018, 4:03 PM
you mean the dataset @Jeremy Rempel?
or the size
j

Jeremy Rempel

08/29/2018, 4:03 PM
the values, say all values in int[] were between 0-100
and you had a million of them
g

g4sarma

08/29/2018, 4:04 PM
Okay, sp it i had a larger range of values say -1000 to 1000
quicksort it is ?
j

Jeremy Rempel

08/29/2018, 4:04 PM
no, 0-100 is just a number. its just a defined range
g

g4sarma

08/29/2018, 4:05 PM
That's what i meant
j

Jeremy Rempel

08/29/2018, 4:05 PM
you'd loop through your array once and use a map to store map[i] = cnt
in O(n) time
then map[i] will represent the number of elements at i
then second loop from 0..range and copy values into a new array
O(n) time
you are creating a map of size n so its a tradeoff between time and memory