Guest User

Untitled

a guest
Nov 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. func quicksort(_ arrayToSort: [Int]) -> [Int] {
  2.  
  3. if arrayToSort.count == 1 {
  4. return arrayToSort
  5. }
  6.  
  7. let pivot = arrayToSort[arrayToSort.count - 1]
  8.  
  9. let leftArray = arrayToSort.filter{ $0 < pivot }
  10. let rightArray = arrayToSort.filter { $0 > pivot }
  11.  
  12. let leftSortedArray: [Int]
  13. if leftArray.count > 1 {
  14. leftSortedArray = quicksort(leftArray)
  15. } else {
  16. leftSortedArray = leftArray
  17. }
  18.  
  19. let rightSortedArray: [Int]
  20. if rightArray.count > 1 {
  21. rightSortedArray = quicksort(rightArray)
  22. } else {
  23. rightSortedArray = rightArray
  24. }
  25.  
  26.  
  27. let result = concatenate(leftSortedArray, [pivot], rightSortedArray)
  28.  
  29. return result
  30.  
  31. }
  32.  
  33. func concatenate(_ arrays: [Int]...) -> [Int]{
  34. let result = arrays.flatMap{$0}
  35. return result
  36. }
  37.  
  38. let s = quicksort([4123,4,23123,3123134531,123123,123,124,12,312,312,3,123,72364823741,123,12,31298731,211028731923,123,123123,5,654,574,5,46,75,734,234234234])
  39. print(s)
Add Comment
Please, Sign In to add comment