-
Notifications
You must be signed in to change notification settings - Fork 1
/
DAA6.py
75 lines (60 loc) · 1.89 KB
/
DAA6.py
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
64
65
66
67
68
69
70
71
72
73
74
75
# Python implementation of Quick Sort Using Deterministic Method
# Function to find the partition position
def partition(arr,start,stop):
pivot = start # pivot
# a variable to memorize where the
i = start + 1
# partition in the array starts from.
for j in range(start + 1, stop + 1):
if arr[j] <= arr[pivot]:
arr[i] , arr[j] = arr[j] , arr[i]
i = i + 1
arr[pivot] , arr[i - 1] = arr[i - 1] , arr[pivot]
pivot = i - 1
return (pivot)
# function to perform quicksort
def quickSort(array, start, stop):
if start < stop:
pi = partition(array, start, stop)
# Recursive call on the left of pivot
quickSort(array, start, pi - 1)
# Recursive call on the right of pivot
quickSort(array, pi + 1, stop)
# Driver Code
if __name__ == "__main__":
array = [10, 7, 8, 9, 1, 5]
quickSort(array, 0, len(array) - 1)
print(array)
# Python implementation of Quick Sort using Randomization Method
import random
# Function to find random pivot
def partitionrand(arr , start, stop):
randpi = random.randrange(start, stop)
# swapping starting element with random pivot
arr[start], arr[randpi] = arr[randpi], arr[start]
return partition(arr, start, stop)
# Function to find the partition position
def partition(arr,start,stop):
pivot = start # pivot
# a variable to memorize where the
i = start + 1
# partition in the array starts from.
for j in range(start + 1, stop + 1):
if arr[j] <= arr[pivot]:
arr[i] , arr[j] = arr[j] , arr[i]
i = i + 1
arr[pivot] , arr[i - 1] = arr[i - 1] , arr[pivot]
pivot = i - 1
return (pivot)
def quickSort(arr, start , stop):
if(start < stop):
pi = partitionrand(array, start, stop)
# Recursive call on the left of pivot
quickSort(array, start, pi - 1)
# Recursive call on the right of pivot
quickSort(array, pi + 1, stop)
# Driver Code
if __name__ == "__main__":
array = [10, 7, 8, 9, 1, 5]
quickSort(array, 0, len(array) - 1)
print(array)