Guest User

Untitled

a guest
Feb 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. /**
  2. * Counting Sort assumes every element of input array was inside [0,k]
  3. */
  4. object CountingSort {
  5.  
  6. fun sort(input: Array<Int>, upperLimit: Int): Array<Int> {
  7. val C = IntArray(upperLimit + 1) { 0 }
  8. val output = Array<Int>(input.size) { 0 }
  9. input.forEach { element ->
  10. C[element]++
  11. }
  12. for (i in 1..upperLimit) {
  13. C[i] = C[i] + C[i - 1]
  14. }
  15. for (i in output.size - 1 downTo 0) {
  16. output[C[input[i]]-1] = input[i]
  17. C[input[i]]--
  18. }
  19. return output
  20. }
  21. }
Add Comment
Please, Sign In to add comment