-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathQuickSortImple.py
More file actions
63 lines (52 loc) · 2.06 KB
/
QuickSortImple.py
File metadata and controls
63 lines (52 loc) · 2.06 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Quick Sort Implementation
# Author: Pradeep K. Pant, https://pradeeppant.com
#- The quick sort uses divide and conquer to gain the same advantages as the merge sort,
# while not using additional storage also known as "partition exchange sort" .
# - As a trade-off, however, it is possible that the list may not be divided in half.
# - When this happens, we will see that performance is diminished.
#- A quick sort first selects a value, which is called the "pivot" value.
#- The role of the pivot value is to assist with splitting the list.
#- The actual position where the pivot value belongs in the final sorted list, commonly
# called the "split" point, will be used to divide the list for subsequent calls to the quick sort.
#- Performance:
# - Worst case: O(n square)
# - Best case: O(nlog n)
# - Average case: O(nlog n)
# Reference material: http://interactivepython.org/runestone/static/pythonds/SortSearch/TheSelectionSort.html
def quick_sort(arr):
quick_sort_help(arr,0,len(arr)-1)
def quick_sort_help(arr,first,last):
# do till this condition is satisfied..
if first<last:
# Split the list
splitpoint = partition(arr,first,last)
quick_sort_help(arr,first,splitpoint-1)
quick_sort_help(arr,splitpoint+1,last)
def partition(arr,first,last):
# use first entry as pivot val
pivotvalue = arr[first]
leftmark = first+1
rightmark = last
done = False
# Looping starts
while not done:
while leftmark <= rightmark and arr[leftmark] <= pivotvalue:
leftmark = leftmark + 1
while arr[rightmark] >= pivotvalue and rightmark >= leftmark:
rightmark = rightmark -1
if rightmark < leftmark:
done = True
else:
temp = arr[leftmark]
arr[leftmark] = arr[rightmark]
arr[rightmark] = temp
# swapping
temp = arr[first]
arr[first] = arr[rightmark]
arr[rightmark] = temp
return rightmark
# Test
arr = [2,7,1,8,5,9,11,35,25]
quick_sort(arr)
print (arr)
#[1, 2, 5, 7, 8, 11, 25, 35]