fork download
  1. fun bubblesort(arr: Array<Int>) : Array<Int> {
  2.  
  3. var finished: Int = 0
  4.  
  5. var swapped: Int
  6.  
  7. var size: Int = arr.size - 1
  8.  
  9. while( finished == 0 ) {
  10.  
  11. swapped = 0
  12.  
  13. for(i in 0..size-1) {
  14.  
  15. if(arr[i] > arr[i+1]) {
  16.  
  17. val holder: Int = arr[i]
  18.  
  19. arr[i] = arr[i+1]
  20.  
  21. arr[i+1] = holder
  22.  
  23. swapped = 1
  24. }
  25. }
  26.  
  27. if(swapped == 1) size = size - 1
  28.  
  29. else
  30. finished = 1
  31.  
  32. }
  33.  
  34. return arr
  35. }
  36.  
  37. fun main(args: Array<String>) {
  38.  
  39. var integers = arrayOf(9,8,7,6,5,4,3,2,1)
  40.  
  41. println(integers.joinToString(" "))
  42.  
  43. integers = bubblesort(integers)
  44.  
  45. println(integers.joinToString(" "))
  46. }
Success #stdin #stdout 0.12s 41120KB
stdin
Standard input is empty
stdout
9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9