Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Quick Sort Algorithm - Complexity Analysis"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "import numpy as np\n",
  17. "import random as rdm"
  18. ]
  19. },
  20. {
  21. "cell_type": "code",
  22. "execution_count": 2,
  23. "metadata": {},
  24. "outputs": [],
  25. "source": [
  26. "def generate_seq(n):\n",
  27. " seq = []\n",
  28. " for i in range(n):\n",
  29. " seq.append(rdm.randint(1,100))\n",
  30. " return seq\n",
  31. " "
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": 3,
  37. "metadata": {},
  38. "outputs": [
  39. {
  40. "data": {
  41. "text/plain": [
  42. "[48, 16, 17, 52, 67, 99, 32, 22, 62, 79]"
  43. ]
  44. },
  45. "execution_count": 3,
  46. "metadata": {},
  47. "output_type": "execute_result"
  48. }
  49. ],
  50. "source": [
  51. "generate_seq(10)"
  52. ]
  53. },
  54. {
  55. "cell_type": "code",
  56. "execution_count": 4,
  57. "metadata": {},
  58. "outputs": [],
  59. "source": [
  60. "def swap(a,b):\n",
  61. " temp = a\n",
  62. " a = b\n",
  63. " b = temp\n",
  64. " return a,b"
  65. ]
  66. },
  67. {
  68. "cell_type": "code",
  69. "execution_count": 5,
  70. "metadata": {},
  71. "outputs": [],
  72. "source": [
  73. "def swapint(x,y):\n",
  74. " assert (isinstance(x,int) == isinstance(y,int)),\"Arguments types mismatch, they must be of type int\"\n",
  75. " x +=y\n",
  76. " y = x - y\n",
  77. " x = x - y\n",
  78. " return x,y"
  79. ]
  80. },
  81. {
  82. "cell_type": "code",
  83. "execution_count": 6,
  84. "metadata": {},
  85. "outputs": [
  86. {
  87. "name": "stdout",
  88. "output_type": "stream",
  89. "text": [
  90. "10 15\n"
  91. ]
  92. }
  93. ],
  94. "source": [
  95. "a = 15\n",
  96. "b = 10\n",
  97. "a,b = swapint(a,b)\n",
  98. "print(a,b)"
  99. ]
  100. },
  101. {
  102. "cell_type": "markdown",
  103. "metadata": {},
  104. "source": [
  105. "#### Lomuto Partition Scheme"
  106. ]
  107. },
  108. {
  109. "cell_type": "markdown",
  110. "metadata": {},
  111. "source": [
  112. "This scheme is attributed to Nico Lomuto and popularized by Bentley in his book Programming Pearls and Cormen et al. in their book Introduction to Algorithms. This scheme chooses a pivot that is typically the last element in the array. The algorithm maintains index i as it scans the array using another index j such that the elements lo through i (inclusive) are less than or equal to the pivot, and the elements i+1 through j-1 (inclusive) are greater than the pivot. As this scheme is more compact and easy to understand, it is frequently used in introductory material, although it is less efficient than Hoare's original scheme. This scheme degrades to O(n2) when the array is already in order. There have been various variants proposed to boost performance including various ways to select pivot, deal with equal elements, use other sorting algorithms such as Insertion sort for small arrays and so on. "
  113. ]
  114. },
  115. {
  116. "cell_type": "code",
  117. "execution_count": 12,
  118. "metadata": {},
  119. "outputs": [],
  120. "source": [
  121. "def lomuto_partition(seq,low,high):\n",
  122. " i = low\n",
  123. " pivot = seq[high]\n",
  124. " for j in range(high):\n",
  125. " if seq[j] < pivot:\n",
  126. " seq[i],seq[j] = swap(seq[i],seq[j])\n",
  127. " i+=1\n",
  128. " seq[i],seq[high] = swap(seq[i],pivot)\n",
  129. " return i\n",
  130. " "
  131. ]
  132. },
  133. {
  134. "cell_type": "code",
  135. "execution_count": 13,
  136. "metadata": {},
  137. "outputs": [
  138. {
  139. "name": "stdout",
  140. "output_type": "stream",
  141. "text": [
  142. "seq = [40, 11, 71, 42, 15, 39, 92, 3, 84, 29, 16, 63, 46, 47, 55, 70, 94, 85, 42, 46]\n",
  143. "partitioned seq: [40, 11, 42, 15, 39, 3, 29, 16, 42, 46, 71, 63, 46, 47, 55, 70, 94, 85, 84, 92] , pivot=46\n"
  144. ]
  145. }
  146. ],
  147. "source": [
  148. "array = generate_seq(20)\n",
  149. "print(\"seq =\",array)\n",
  150. "p=lomuto_partition(array,0,len(array)-1)\n",
  151. "print(\"partitioned seq:\",array,\", pivot=%i\"%array[p])"
  152. ]
  153. },
  154. {
  155. "cell_type": "markdown",
  156. "metadata": {},
  157. "source": [
  158. "#### Hoare partition scheme"
  159. ]
  160. },
  161. {
  162. "cell_type": "markdown",
  163. "metadata": {},
  164. "source": [
  165. "The original partition scheme described by C.A.R. Hoare uses two indices that start at the ends of the array being partitioned, then move toward each other, until they detect an inversion: a pair of elements, one greater than or equal to the pivot, one lesser or equal, that are in the wrong order relative to each other. The inverted elements are then swapped. When the indices meet, the algorithm stops and returns the final index. There are many variants of this algorithm, for example, selecting pivot from A[hi] instead of A[lo]. Hoare's scheme is more efficient than Lomuto's partition scheme because it does three times fewer swaps on average, and it creates efficient partitions even when all values are equal. Like Lomuto's partition scheme, Hoare's partitioning also would cause Quicksort to degrade to O(n2) for already sorted input, if the pivot was chosen as the first or the last element. With the middle element as the pivot, however, sorted data results with (almost) no swaps in equally sized partitions leading to best case behavior of Quicksort, i.e. O(n log(n)). Like others, Hoare's partitioning doesn't produce a stable sort. Note that in this scheme, the pivot's final location is not necessarily at the index that was returned, and the next two segments that the main algorithm recurs on are (lo..p) and (p+1..hi) as opposed to (lo..p-1) and (p+1..hi) as in Lomuto's scheme. However, the partitioning algorithm guarantees lo ≤ p < hi which implies both resulting partitions are non-empty, hence there's no risk of infinite recursion."
  166. ]
  167. },
  168. {
  169. "cell_type": "code",
  170. "execution_count": 9,
  171. "metadata": {},
  172. "outputs": [],
  173. "source": [
  174. "def hoare_partition(seq,low,high):\n",
  175. " pass"
  176. ]
  177. },
  178. {
  179. "cell_type": "markdown",
  180. "metadata": {},
  181. "source": [
  182. "#### Quick Sort Algorithm with Lomuto Partition Scheme"
  183. ]
  184. },
  185. {
  186. "cell_type": "code",
  187. "execution_count": 10,
  188. "metadata": {},
  189. "outputs": [],
  190. "source": [
  191. "def quicksort_l(seq,low,high):\n",
  192. " if low < high:\n",
  193. " pivot = lomuto_partition(seq,low,high)\n",
  194. " quicksort_l(seq,low,pivot-1)\n",
  195. " quicksort_l(seq,pivot+1,high)"
  196. ]
  197. },
  198. {
  199. "cell_type": "code",
  200. "execution_count": 157,
  201. "metadata": {},
  202. "outputs": [
  203. {
  204. "name": "stdout",
  205. "output_type": "stream",
  206. "text": [
  207. "seq = [58, 27, 24, 90, 41, 78, 91]\n",
  208. "sorted seq: [24, 27, 41, 58, 78, 90, 91]\n"
  209. ]
  210. }
  211. ],
  212. "source": [
  213. "seq_1 = generate_seq(7)\n",
  214. "lo = 0 \n",
  215. "hi= len(seq_1)-1\n",
  216. "print(\"seq =\",seq_1)\n",
  217. "quicksort_l(seq_1,lo,hi)\n",
  218. "print(\"sorted seq:\",seq_1)"
  219. ]
  220. },
  221. {
  222. "cell_type": "markdown",
  223. "metadata": {},
  224. "source": [
  225. "#### Quick Sort Algorithm with Hoare Partition Scheme"
  226. ]
  227. },
  228. {
  229. "cell_type": "code",
  230. "execution_count": null,
  231. "metadata": {},
  232. "outputs": [],
  233. "source": [
  234. "def quicksort_h(seq,low,high):\n",
  235. " if low < high:\n",
  236. " p = hoare_partition(seq,low,high)\n",
  237. " quicksort_h(seq,low,pivot)\n",
  238. " quicksort_h(seq,pivot+1,high)"
  239. ]
  240. },
  241. {
  242. "cell_type": "code",
  243. "execution_count": null,
  244. "metadata": {},
  245. "outputs": [],
  246. "source": [
  247. "seq_2 = generate_seq(6)\n",
  248. "lo = 0 \n",
  249. "hi= len(seq_2)-1\n",
  250. "print(\"seq =\",seq_2)\n",
  251. "quicksort_l(seq_2,lo,hi)\n",
  252. "print(\"sorted seq:\",seq_2)"
  253. ]
  254. }
  255. ],
  256. "metadata": {
  257. "kernelspec": {
  258. "display_name": "Python 3",
  259. "language": "python",
  260. "name": "python3"
  261. },
  262. "language_info": {
  263. "codemirror_mode": {
  264. "name": "ipython",
  265. "version": 3
  266. },
  267. "file_extension": ".py",
  268. "mimetype": "text/x-python",
  269. "name": "python",
  270. "nbconvert_exporter": "python",
  271. "pygments_lexer": "ipython3",
  272. "version": "3.6.7"
  273. }
  274. },
  275. "nbformat": 4,
  276. "nbformat_minor": 2
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement