0%

Sort Algorithm 2 - Quick Sort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def solution(nums):
low, high = 0, len(nums)-1
quickSort(nums,low,high)
return nums

def quickSort(nums,low,high):
if low < high:
pivotpos = partition(nums,low,high)
quickSort(nums,low,pivotpos-1)
quickSort(nums,pivotpos+1,high)

def partition(nums,low,high):
pivot = nums[low]
while low < high:
while low < high and nums[high] >= pivot:
high -= 1
nums[low] = nums[high]
while low < high and nums[low] <= pivot:
low += 1
nums[high] = nums[low]
nums[low] = pivot
return low


if __name__ == "__main__":
nums = [10, 17, 50, 7, 30, 24, 30, 45, 15, 5, 36, 21]
print(solution(nums))

(1) avarage time cost: T(n) = O(nlogn)
(2) avarage time cost: S(n) = O(logn)
(3) in-place sort
(4) unstable
(5) best T(n)= O(nlogn), worst T(n) = O(nlogn)

-------------End of blogThanks for your reading-------------