Advertisement
Guest User

Untitled

a guest
Aug 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. func merge<T: Comparable>(_ array: inout [T], _ left: Int, _ middle: Int, _ right: Int) {
  2. let leftSubarray = Array(array[left...middle])
  3. let rightSubarray = Array(array[(middle+1)...right])
  4.  
  5. var index = left
  6. var leftIndex = 0
  7. var rightIndex = 0
  8.  
  9. while (leftIndex < leftSubarray.count) && (rightIndex < rightSubarray.count) {
  10. if leftSubarray[leftIndex] < rightSubarray[rightIndex] {
  11. array[index] = leftSubarray[leftIndex]
  12. leftIndex += 1
  13. }
  14. else {
  15. array[index] = rightSubarray[rightIndex]
  16. rightIndex += 1
  17. }
  18. index += 1
  19. }
  20.  
  21. while leftIndex < leftSubarray.count {
  22. array[index] = leftSubarray[leftIndex]
  23. leftIndex += 1
  24. index += 1
  25. }
  26.  
  27. while rightIndex < rightSubarray.count {
  28. array[index] = rightSubarray[rightIndex]
  29. rightIndex += 1
  30. index += 1
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement