forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro_sort.py
More file actions
Latest commit
173 lines (135 loc) · 4.73 KB
/
Copy pathintro_sort.py
File metadata and controls
173 lines (135 loc) · 4.73 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
Introspective Sort is hybrid sort (Quick Sort + Heap Sort + Insertion Sort)
if the size of the list is under 16, use insertion sort
https://en.wikipedia.org/wiki/Introsort
"""
importmath
definsertion_sort(array: list, start: int=0, end: int=0) ->list:
"""
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
>>> insertion_sort(array, 0, len(array))
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
"""
end=endorlen(array)
foriinrange(start, end):
temp_index=i
temp_index_value=array[i]
whiletemp_index!=startandtemp_index_value<array[temp_index-1]:
array[temp_index] =array[temp_index-1]
temp_index-=1
array[temp_index] =temp_index_value
returnarray
defheapify(array: list, index: int, heap_size: int) ->None: # Max Heap
"""
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
>>> heapify(array, len(array) // 2 ,len(array))
"""
largest=index
left_index=2*index+1# Left Node
right_index=2*index+2# Right Node
ifleft_index<heap_sizeandarray[largest] <array[left_index]:
largest=left_index
ifright_index<heap_sizeandarray[largest] <array[right_index]:
largest=right_index
iflargest!=index:
array[index], array[largest] =array[largest], array[index]
heapify(array, largest, heap_size)
defheap_sort(array: list) ->list:
"""
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
>>> heap_sort(array)
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
"""
n=len(array)
foriinrange(n//2, -1, -1):
heapify(array, i, n)
foriinrange(n-1, 0, -1):
array[i], array[0] =array[0], array[i]
heapify(array, 0, i)
returnarray
defmedian_of_3(
array: list, first_index: int, middle_index: int, last_index: int
) ->int:
"""
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
>>> median_of_3(array, 0, 0 + ((len(array) - 0) // 2) + 1, len(array) - 1)
12
"""
if (array[first_index] >array[middle_index]) != (
array[first_index] >array[last_index]
):
returnarray[first_index]
elif (array[middle_index] >array[first_index]) != (
array[middle_index] >array[last_index]
):
returnarray[middle_index]
else:
returnarray[last_index]
defpartition(array: list, low: int, high: int, pivot: int) ->int:
"""
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
>>> partition(array, 0, len(array), 12)
8
"""
i=low
j=high
whileTrue:
whilearray[i] <pivot:
i+=1
j-=1
whilepivot<array[j]:
j-=1
ifi>=j:
returni
array[i], array[j] =array[j], array[i]
i+=1
defsort(array: list) ->list:
"""
:param collection: some mutable ordered collection with heterogeneous
comparable items inside
:return: the same collection ordered by ascending
Examples:
>>> sort([4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12])
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
>>> sort([-1, -5, -3, -13, -44])
[-44, -13, -5, -3, -1]
>>> sort([])
[]
>>> sort([5])
[5]
>>> sort([-3, 0, -7, 6, 23, -34])
[-34, -7, -3, 0, 6, 23]
>>> sort([1.7, 1.0, 3.3, 2.1, 0.3 ])
[0.3, 1.0, 1.7, 2.1, 3.3]
>>> sort(['d', 'a', 'b', 'e', 'c'])
['a', 'b', 'c', 'd', 'e']
"""
iflen(array) ==0:
returnarray
max_depth=2*math.ceil(math.log2(len(array)))
size_threshold=16
returnintro_sort(array, 0, len(array), size_threshold, max_depth)
defintro_sort(
array: list, start: int, end: int, size_threshold: int, max_depth: int
) ->list:
"""
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
>>> max_depth = 2 * math.ceil(math.log2(len(array)))
>>> intro_sort(array, 0, len(array), 16, max_depth)
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
"""
whileend-start>size_threshold:
ifmax_depth==0:
returnheap_sort(array)
max_depth-=1
pivot=median_of_3(array, start, start+ ((end-start) //2) +1, end-1)
p=partition(array, start, end, pivot)
intro_sort(array, p, end, size_threshold, max_depth)
end=p
returninsertion_sort(array, start, end)
if__name__=="__main__":
importdoctest
doctest.testmod()
user_input=input("Enter numbers separated by a comma : ").strip()
unsorted= [float(item) foriteminuser_input.split(",")]
print(sort(unsorted))