fork download
  1. fun quickSort(array: MutableList<Int>, left: Int, right: Int) {
  2. if (right <= left) {
  3. return
  4. }
  5.  
  6. val pivot = array[(left + right) / 2]
  7. var i = left
  8. var j = right
  9.  
  10. while (i <= j) {
  11. while (array[i] < pivot) {
  12. i += 1
  13. }
  14.  
  15. while (array[j] > pivot) {
  16. j -= 1
  17. }
  18.  
  19. if (i > j) {
  20. break
  21. }
  22.  
  23. val tmp = array[i]
  24. array[i] = array[j]
  25. array[j] = tmp
  26.  
  27. i++
  28. j--
  29. }
  30.  
  31. quickSort(array, left, j)
  32. quickSort(array, i, right)
  33. }
  34.  
  35. fun main() {
  36. val array = mutableListOf(7, 3, 0, 1, 5, 2, 5, 19, 10, 5)
  37.  
  38. quickSort(array, 0, array.count() - 1)
  39.  
  40. println(array)
  41. }
Success #stdin #stdout 0.09s 39400KB
stdin
Standard input is empty
stdout
[0, 1, 2, 3, 5, 5, 5, 7, 10, 19]