Skip to content

Commit 0e40407

Browse files
authored
Update README.md
1 parent 24bc0f5 commit 0e40407

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎src/recursive_sorting/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,34 @@ Let's think about the group photo example again. Everyone's lined up and the pho
3434
## Your Task
3535
- Implement the `quick_sort()` function in the Guided Project with your TA
3636

37+
## Implementation in Python
38+
```python
39+
def quick_sort( arr, low, high ):
40+
# base case
41+
if high-low <= 0:
42+
return arr
43+
44+
# partition
45+
pivot = low
46+
for i in range(low+1, high+1):
47+
if arr[i] < arr[pivot]:
48+
# move to LHS of pivot - 2 swaps
49+
temp = arr[i]
50+
arr[i] = arr[pivot+1]
51+
arr[pivot+1] = temp
52+
53+
temp = arr[pivot]
54+
arr[pivot] = arr[pivot+1]
55+
arr[pivot+1] = temp
56+
pivot += 1
57+
58+
# Quick Sort LHS, RHS
59+
arr = quick_sort(arr, low, pivot-1)
60+
arr = quick_sort(arr, pivot+1, high)
61+
62+
return arr
63+
```
64+
3765
### Real-World Applications
3866
While ***Quick Sort*** has "quick" in its name, it is typically not used as frequently as Merge Sort. Although it *is* quick in a best case scenario, worst case for ***Quick Sort*** is *very* bad. Because of this, it is not often chosen for production.
3967

0 commit comments

Comments
 (0)