Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
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.

186 lines
4.3 KiB

  1. # Insertion Sort
  2. ## Functional Notation
  3. $$
  4. s([]) = []\\
  5. s(x::xs) = i(x, s(xs))
  6. $$
  7. $$
  8. i(x, []) = [x]\\
  9. i(x,y::ys) = x::y::ys, if x \leq y\\
  10. i(x,y::yx) = y::i(x, ys) otherwise
  11. $$
  12. ```Python
  13. def insertionSort(alist):
  14. for index in range(1,len(alist)):
  15. currentvalue = alist[index]
  16. position = index
  17. while position > 0 and alist[position-1] > currentvalue:
  18. alist[position] = alist[position-1]
  19. position = position-1
  20. alist[position] = currentvalue
  21. return alist
  22. ```
  23. # Merge Sort
  24. ## Functional Notation
  25. $$
  26. d([]) = ([],[])\\
  27. d([x]) = ([x],[])\\
  28. d(x_1::x_2::xs) = let (b_1, b_2) = d(xs) in (x_1::b_1, x_2::b_2)\\
  29. $$
  30. $$
  31. mSort([]) = []\\
  32. mSort([x]) = [x]\\
  33. mSort(xs) = let(b_1, b_2) = d(xs) in mSort(b_1) combine mSort(b_2)\\
  34. $$
  35. ## Python Implementation
  36. ```Python
  37. def merge_sort(a):
  38. if len(a) < 2:
  39. return a
  40. l = a[0:len(a)//2]
  41. r = a[len(a)//2:]
  42. return merge(merge_sort(l), merge_sort(r))
  43. def merge(a, b):
  44. out = []
  45. i = 0
  46. j = 0
  47. while i < len(a) or j < len(b):
  48. if i >= len(a):
  49. out.append(b[j])
  50. j += 1
  51. elif j >= len(b):
  52. out.append(a[i])
  53. i += 1
  54. else:
  55. if a[i] <= b[j]:
  56. out.append(a[i])
  57. i += 1
  58. else:
  59. out.append(b[j])
  60. j += 1
  61. return out
  62. ```
  63. # Quick Sort
  64. ## Functional Notation
  65. $$
  66. qSort([]) = []\\
  67. qSort(x::xs) = qSort([y \in xs | y < x]) + [y \in x:xs | y = x] + qSort([y \in xs | y > x])\\
  68. $$
  69. ## Memory Greedy Solution
  70. ```
  71. def quickSortNormal(data):
  72. """
  73. This is the traditional implementation of quick sort
  74. where there are two recursive calls.
  75. """
  76. if len(data) == 0:
  77. return []
  78. else:
  79. less, equal, greater = partition(data)
  80. return quickSortNormal(less) + equal + quickSortNormal(greater)
  81. ```
  82. ## Accumulation Solution
  83. ```
  84. def quick_sort_accumulation(data, a):
  85. """
  86. Implementation of quickSort which forces tail recursion
  87. by wrapping the second recursive in the tail positioned
  88. recursive call and added an accumulation variable.
  89. """
  90. if len(data) == 0:
  91. return a
  92. less, equal, greater = partition(data)
  93. return quick_sort_accumulation(less,
  94. equal + quick_sort_accumulation(greater, a))
  95. def quicksort(data):
  96. """
  97. Wrapper function for quick sort accumulation.
  98. """
  99. return quick_sort_accumulation(data, [])
  100. ```
  101. ## In-Place Sorting Implementation
  102. ```
  103. def iterative_partition(data, left, right):
  104. """
  105. Function which partitions the data into two segments,
  106. the left which is less than the pivot and the right
  107. which is greater than the pivot. The pivot for this
  108. algo is the right most index. This function returns
  109. the ending index of the pivot.
  110. :param data: array to be sorted
  111. :param left: left most portion of array to look at
  112. :param right: right most portion of the array to look at
  113. """
  114. x = data[right]
  115. i = left - 1
  116. j = left
  117. while j < right:
  118. if data[j] <= x:
  119. i = i + 1
  120. data[i], data[j] = data[j], data[i]
  121. j = j+1
  122. data[i + 1], data[right] = data[right], data[i + 1]
  123. return i + 1
  124. def iterative_quick_sort(data):
  125. """
  126. In place implementation of quick sort
  127. Wrapper function for iterative_quick_sort_helper which
  128. initializes, left, right to be the extrema of the array.
  129. """
  130. iterative_quick_sort_helper(data, 0, len(data) -1)
  131. return data
  132. def iterative_quick_sort_helper(data, left, right):
  133. """
  134. Uses the divide and conquer algo to sort an array
  135. :param data: array of data
  136. :param left: left index bound for sorting
  137. :param right: right bound for sorting
  138. """
  139. if left < right:
  140. pivot = iterative_partition(data, left, right)
  141. iterative_quick_sort_helper(data, left, pivot -1)
  142. iterative_quick_sort_helper(data, pivot+1, right)
  143. ```
  144. # Time Complexities Overview
  145. | Algorithm &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Worst &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Average &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; | Best &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; |
  146. |--- |:--- |:--- |:--- |
  147. | Insertion | $0(n^2)$ | $0(n^2)$ | $0(n^2)$ |
  148. | Merge | $0(nlog(n))$ | $0(nlog(n))$ | $0(nlog(n))$ |
  149. | Quick | $0(nlog(n))$ | $0(nlog(n))$ | $0(n^2)$ |