Skip to content

Commit 8cf1077

Browse files
author
Joseph Luce
authored
Update 912_sort_an_array.md
1 parent 0cf0029 commit 8cf1077

File tree

1 file changed

+2
-31
lines changed

1 file changed

+2
-31
lines changed

leetcode/medium/912_sort_an_array.md

+2-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 912. Sort an Array
22

3-
## Quick Sort
3+
## Quick Sort In-Place
44

55
- Runtime: O(Nlog(N)), worst case O(N^2)
66
- Space: O(log(N))
@@ -17,38 +17,9 @@ The pivot is now in the correct place.
1717
We then partition the array in two halves and call a recursion on each half, they will be figuring out where the correct placement of the next pivot should be.
1818
This is where the log(N) comes into play, since each time we call the recursion, the array is half of what it use to be.
1919

20-
If the pivot is always selected to be the smallest number of the array, we can have a worst case scenario of O(N^2).
20+
If the pivot is always selected to be the largest number of the array, we can have a worst case scenario of O(N^2).
2121
Hence, never creating two partitions.
2222

23-
```
24-
class Solution(object):
25-
def sortArray(self, nums):
26-
27-
def quick_sort(nums):
28-
if len(nums) == 0:
29-
return []
30-
pivot = nums[-1]
31-
last_unsorted_idx = 0
32-
for idx, n in enumerate(nums[:-1]):
33-
if n <= pivot:
34-
nums[last_unsorted_idx], nums[idx] = nums[idx], nums[last_unsorted_idx]
35-
last_unsorted_idx += 1
36-
nums[last_unsorted_idx], nums[-1] = nums[-1], nums[last_unsorted_idx]
37-
left_sorted = quick_sort(nums[:last_unsorted_idx])
38-
right_sorted = quick_sort(nums[last_unsorted_idx+1:])
39-
return left_sorted + [nums[last_unsorted_idx]] + right_sorted
40-
41-
return quick_sort(nums)
42-
```
43-
44-
## Quick Sort In-Place
45-
46-
- Runtime: O(Nlog(N)), worst case O(N^2)
47-
- Space: O(log(N))
48-
- N = Number of elements in list
49-
50-
To do an in-place quicksort with python, we have to avoid using string slicing.
51-
5223
Note: There is an unstable version of in-place quick sort that has an O(1) space complexity.
5324

5425
```

0 commit comments

Comments
 (0)