-
Notifications
You must be signed in to change notification settings - Fork 1k
/
IntroSort.py
95 lines (81 loc) · 2.77 KB
/
IntroSort.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''
INTRO SORT
Introsort sort is a sorting algorithm that initially uses quicksort,
but switches to heap sort if the depth of the recursion is too deep,
and uses insertion sort for small cases.
Time Complexity:
Worst-case performance: O(nlogn)
Average-case performance: O(nlogn)
'''
#A function that takes a list as argument.
def introsort(alist):
#maxdepth is chosen equal to 2 times floor of log base 2 of the length of the list.
maxdepth = (len(alist).bit_length() - 1 ) * 2
# Call introsort_helper with start = 0 and end = len(blist).
introsort_helper(alist, 0, len(alist), maxdepth)
#The function sorts the list from indexes start to end.
def introsort_helper(alist, start, end, maxdepth):
if end - start <= 1:
return
elif maxdepth == 0:
heapsort(blist, start, end)
else:
p = partition(alist, start, end)
introsort_helper(alist, start, p + 1, maxdepth - 1)
introsort_helper(alist, p + 1, end, maxdepth - 1)
#Partition function uses Hoare partition scheme to partition the list.
def partition(alist, start, end):
pivot = alist[start]
i = start - 1
j = end
while True:
i = i + 1
while alist[i] < pivot:
i = i + 1
j = j - 1
while alist[j] > pivot:
j = j - 1
if i >= j:
return j
swap(alist, i, j)
#Function for swapping particular elements of a list
def swap(alist, i, j):
alist[i], alist[j] = alist[j], alist[i]
#Heapsort from indexes start to end - 1
def heapsort(blist, start, end):
build_max_heap(alist, start, end)
for i in range(end - 1, start, -1):
swap(blist, start, i)
max_heapify(alist, index = 0, start = start, end = i)
#Rearrange the list into a list representation of a heap.
def build_max_heap(alist, start, end):
def parent(i):
return (i - 1) // 2
length = end - start
index = parent(length - 1)
while index >= 0:
max_heapify(alist, index, start, end)
index = index - 1
#Modifies the heap structure at and below the node at given index to make it satisfy the heap property.
def max_heapify(alist, index, start, end):
def left(i):
return 2 * i + 1
def right(i):
return 2 * i + 2
size = end - start
l = left(index)
r = right(index)
if (l < size and alist[start + l] > alist[start + index]):
largest = l
else:
largest = index
if (r < size and alist[start + r] > alist[start + largest]):
largest = r
if largest != index:
swap(blist, start + largest, start + index)
max_heapify(blist, largest, start, end)
alist = input('Enter the list of numbers: ').split()
alist = [int(n) for n in alist]
introsort(alist)
print('Sorted list: ', end = '')
print(alist)