Skip to content

Commit b038e43

Browse files
authored
Revised QuickSort code, in Jupyter nb
1 parent 1d0b24e commit b038e43

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Python QuickSort Algorithm"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"[5, 9, 1, 2, 4, 8, 6, 3, 7]\n",
20+
"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
21+
]
22+
}
23+
],
24+
"source": [
25+
"#---------------------------------------\n",
26+
"# Quick Sort\n",
27+
"#---------------------------------------\n",
28+
"def quick_sort(A):\n",
29+
" quick_sort2(A, 0, len(A)-1)\n",
30+
" \n",
31+
"def quick_sort2(A, low, hi):\n",
32+
" if hi-low < 1 and low < hi:\n",
33+
" quick_selection(A, low, hi)\n",
34+
" elif low < hi:\n",
35+
" p = partition(A, low, hi)\n",
36+
" quick_sort2(A, low, p - 1)\n",
37+
" quick_sort2(A, p + 1, hi)\n",
38+
" \n",
39+
"def get_pivot(A, low, hi):\n",
40+
" mid = (hi + low) // 2\n",
41+
" s = sorted([A[low], A[mid], A[hi]])\n",
42+
" if s[1] == A[low]:\n",
43+
" return low\n",
44+
" elif s[1] == A[mid]:\n",
45+
" return mid\n",
46+
" return hi\n",
47+
" \n",
48+
"def partition(A, low, hi):\n",
49+
" pivotIndex = get_pivot(A, low, hi)\n",
50+
" pivotValue = A[pivotIndex]\n",
51+
" A[pivotIndex], A[low] = A[low], A[pivotIndex]\n",
52+
" border = low\n",
53+
"\n",
54+
" for i in range(low, hi+1):\n",
55+
" if A[i] < pivotValue:\n",
56+
" border += 1\n",
57+
" A[i], A[border] = A[border], A[i]\n",
58+
" A[low], A[border] = A[border], A[low]\n",
59+
"\n",
60+
" return (border)\n",
61+
" \n",
62+
"def quick_selection(x, first, last):\n",
63+
" for i in range (first, last):\n",
64+
" minIndex = i\n",
65+
" for j in range (i+1, last+1):\n",
66+
" if x[j] < x[minIndex]:\n",
67+
" minIndex = j\n",
68+
" if minIndex != i:\n",
69+
" x[i], x[minIndex] = x[minIndex], x[i]\n",
70+
" \n",
71+
"A = [5,9,1,2,4,8,6,3,7]\n",
72+
"print(A)\n",
73+
"quick_sort(A)\n",
74+
"print(A)"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"### Nice simple version written by Mr. UncleChu in comments\n",
82+
"Slick code, but does not sort in place, so uses a lot more memory. Do not use for large lists or you'll get stackoverflow."
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 5,
88+
"metadata": {},
89+
"outputs": [
90+
{
91+
"name": "stdout",
92+
"output_type": "stream",
93+
"text": [
94+
"[5, 9, 1, 2, 4, 8, 6, 3, 7]\n",
95+
"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
96+
]
97+
}
98+
],
99+
"source": [
100+
"def quick_sort_chu(a_list):\n",
101+
" if len(a_list) < 2: return a_list\n",
102+
" lesser = quick_sort([x for x in a_list[1:] if x <= a_list[0]])\n",
103+
" bigger = quick_sort([x for x in a_list[1:] if x > a_list[0]])\n",
104+
" return sum([lesser, [a_list[0]], bigger], [])\n",
105+
"A = [5,9,1,2,4,8,6,3,7]\n",
106+
"print(A)\n",
107+
"B = quick_sort_chu(A)\n",
108+
"print(B)"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"metadata": {},
115+
"outputs": [],
116+
"source": []
117+
}
118+
],
119+
"metadata": {
120+
"kernelspec": {
121+
"display_name": "Python 3",
122+
"language": "python",
123+
"name": "python3"
124+
},
125+
"language_info": {
126+
"codemirror_mode": {
127+
"name": "ipython",
128+
"version": 3
129+
},
130+
"file_extension": ".py",
131+
"mimetype": "text/x-python",
132+
"name": "python",
133+
"nbconvert_exporter": "python",
134+
"pygments_lexer": "ipython3",
135+
"version": "3.7.0"
136+
}
137+
},
138+
"nbformat": 4,
139+
"nbformat_minor": 2
140+
}

0 commit comments

Comments
 (0)