-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.py
67 lines (54 loc) · 1.63 KB
/
quick_sort.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
import time
start = time.time()
count = 0
# Python program for implementation of Quicksort Sort
# This function takes last element as pivot, places
# the pivot element at its correct position in sorted
# array, and places all smaller (smaller than pivot)
# to left of pivot and all greater elements to right
# of pivot
def partition(arr, low, high):
global count
i = (low - 1) # index of smaller element
pivot = arr[high] # pivot
for j in range(low, high):
count += 1
# If current element is smaller than or
# equal to pivot
if arr[j] <= pivot:
# increment index of smaller element
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return (i + 1)
# The main function that implements QuickSort
# arr[] --> Array to be sorted,
# low --> Starting index,
# high --> Ending index
# Function to do Quick sort
def quickSort(arr, low, high):
if low < high:
# pi is partitioning index, arr[p] is now
# at right place
pi = partition(arr, low, high)
# Separately sort elements before
# partition and after partition
quickSort(arr, low, pi - 1)
quickSort(arr, pi + 1, high)
# Driver code to test above
fp = open("case") # Open file on read mode
arr = fp.read().split(" ") # Create a list containing all lines
fp.close()
list2 = []
for i in range(len(arr)):
t = int(arr[i])
list2.append(t)
n = len(list2)
quickSort(list2, 0, n - 1)
print("Sorted array is:")
for i in range(n):
print("%d" % list2[i])
print("")
print(count)
end = time.time()
print(end - start)