Repository where I mostly put random python scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9 KiB

  1. """
  2. Jeffery Russell
  3. 10-6-18
  4. File Containing Variations of Quick Sort
  5. """
  6. def partition(data):
  7. """
  8. Partitions a list of data into three sections
  9. which are lower, equal, and equal to the pivot
  10. which is selected to be the last element in the
  11. list.
  12. """
  13. pivot = len(data) -1
  14. lower, equal, upper = [], [], []
  15. for i in range(0, len(data), 1):
  16. if data[i] > data[pivot]:
  17. upper.append(data[i])
  18. elif data[i] == data[pivot]:
  19. equal.append(data[i])
  20. else:
  21. lower.append(data[i])
  22. return lower, equal, upper
  23. def quickSortNormal(data):
  24. """
  25. This is the traditional implementation of quick sort
  26. where there are two recursive calls.
  27. """
  28. if len(data) == 0:
  29. return []
  30. else:
  31. less, equal, greater = partition(data)
  32. return quickSortNormal(less) + equal + quickSortNormal(greater)
  33. def quick_sort_accumulation(data, a):
  34. """
  35. Implementation of quickSort which forces tail recursion
  36. by wrapping the second recursive in the tail positioned
  37. recursive call and added an accumulation variable.
  38. """
  39. if len(data) == 0:
  40. return a
  41. less, equal, greater = partition(data)
  42. return quick_sort_accumulation(less,
  43. equal + quick_sort_accumulation(greater, a))
  44. def quicksort(data):
  45. """
  46. Wrapper function for quick sort accumulation.
  47. """
  48. return quick_sort_accumulation(data, [])
  49. def iterative_partition(data, left, right):
  50. """
  51. Function which partitions the data into two segments,
  52. the left which is less than the pivot and the right
  53. which is greater than the pivot. The pivot for this
  54. algo is the right most index. This function returns
  55. the ending index of the pivot.
  56. :param data: array to be sorted
  57. :param left: left most portion of array to look at
  58. :param right: right most portion of the array to look at
  59. """
  60. x = data[right]
  61. i = left - 1
  62. j = left
  63. while j < right:
  64. if data[j] <= x:
  65. i = i + 1
  66. data[i], data[j] = data[j], data[i]
  67. j = j+1
  68. data[i + 1], data[right] = data[right], data[i + 1]
  69. return i + 1
  70. def iterative_quick_sort(data):
  71. """
  72. In place implementation of quick sort
  73. Wrapper function for iterative_quick_sort_helper which
  74. initalizes, left, right to be the extrema of the array.
  75. """
  76. iterative_quick_sort_helper(data, 0, len(data) -1)
  77. return data
  78. def iterative_quick_sort_helper(data, left, right):
  79. """
  80. Uses the divide and conquer algo to sort an array
  81. :param data: array of data
  82. :param left: left index bound for sorting
  83. :param right: right bound for sorting
  84. """
  85. if left < right:
  86. pivot = iterative_partition(data, left, right)
  87. iterative_quick_sort_helper(data, left, pivot -1)
  88. iterative_quick_sort_helper(data, pivot+1, right)
  89. print iterative_quick_sort([1,3,1,5,7,9,2,3,5,5,6])