Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ef7b366

Browse files
authoredOct 29, 2023
Update intro_sort.py
1 parent 2ddb584 commit ef7b366

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed
 

‎sorts/intro_sort.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Introspective Sort is hybrid sort (Quick Sort + Heap Sort + Insertion Sort)
2+
Introspective Sort is a hybrid sort (Quick Sort + Heap Sort + Insertion Sort)
33
if the size of the list is under 16, use insertion sort
44
https://en.wikipedia.org/wiki/Introsort
55
"""
@@ -9,7 +9,6 @@
99
def insertion_sort(array: list, start: int = 0, end: int = 0) -> list:
1010
"""
1111
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
12-
1312
>>> insertion_sort(array, 0, len(array))
1413
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
1514
"""
@@ -27,8 +26,7 @@ def insertion_sort(array: list, start: int = 0, end: int = 0) -> list:
2726
def heapify(array: list, index: int, heap_size: int) -> None: # Max Heap
2827
"""
2928
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
30-
31-
>>> heapify(array, len(array) // 2 ,len(array))
29+
>>> heapify(array, len(array) // 2 , len(array))
3230
"""
3331
largest = index
3432
left_index = 2 * index + 1 # Left Node
@@ -47,9 +45,7 @@ def heapify(array: list, index: int, heap_size: int) -> None: # Max Heap
4745

4846
def heap_sort(array: list) -> list:
4947
"""
50-
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
51-
52-
>>> heap_sort(array)
48+
>>> heap_sort([4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12])
5349
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
5450
"""
5551
n = len(array)
@@ -69,18 +65,13 @@ def median_of_3(
6965
) -> int:
7066
"""
7167
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
72-
73-
>>> median_of_3(array, 0, 0 + ((len(array) - 0) // 2) + 1, len(array) - 1)
68+
>>> median_of_3(array, 0, ((len(array) - 0) // 2) + 1, len(array) - 1)
7469
12
75-
7670
>>> array = [13, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
77-
78-
>>> median_of_3(array, 0, 0 + ((len(array) - 0) // 2) + 1, len(array) - 1)
71+
>>> median_of_3(array, 0, ((len(array) - 0) // 2) + 1, len(array) - 1)
7972
13
80-
8173
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 15, 14, 27, 79, 23, 45, 14, 16]
82-
83-
>>> median_of_3(array, 0, 0 + ((len(array) - 0) // 2) + 1, len(array) - 1)
74+
>>> median_of_3(array, 0, ((len(array) - 0) // 2) + 1, len(array) - 1)
8475
14
8576
"""
8677
if (array[first_index] > array[middle_index]) != (
@@ -98,7 +89,6 @@ def median_of_3(
9889
def partition(array: list, low: int, high: int, pivot: int) -> int:
9990
"""
10091
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
101-
10292
>>> partition(array, 0, len(array), 12)
10393
8
10494
"""
@@ -125,22 +115,16 @@ def sort(array: list) -> list:
125115
Examples:
126116
>>> sort([4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12])
127117
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
128-
129118
>>> sort([-1, -5, -3, -13, -44])
130119
[-44, -13, -5, -3, -1]
131-
132120
>>> sort([])
133121
[]
134-
135122
>>> sort([5])
136123
[5]
137-
138124
>>> sort([-3, 0, -7, 6, 23, -34])
139125
[-34, -7, -3, 0, 6, 23]
140-
141126
>>> sort([1.7, 1.0, 3.3, 2.1, 0.3 ])
142127
[0.3, 1.0, 1.7, 2.1, 3.3]
143-
144128
>>> sort(['d', 'a', 'b', 'e', 'c'])
145129
['a', 'b', 'c', 'd', 'e']
146130
"""
@@ -156,9 +140,7 @@ def intro_sort(
156140
) -> list:
157141
"""
158142
>>> array = [4, 2, 6, 8, 1, 7, 8, 22, 14, 56, 27, 79, 23, 45, 14, 12]
159-
160143
>>> max_depth = 2 * math.ceil(math.log2(len(array)))
161-
162144
>>> intro_sort(array, 0, len(array), 16, max_depth)
163145
[1, 2, 4, 6, 7, 8, 8, 12, 14, 14, 22, 23, 27, 45, 56, 79]
164146
"""
@@ -177,7 +159,6 @@ def intro_sort(
177159
import doctest
178160

179161
doctest.testmod()
180-
181162
user_input = input("Enter numbers separated by a comma : ").strip()
182163
unsorted = [float(item) for item in user_input.split(",")]
183-
print(sort(unsorted))
164+
print(f"{sort(unsorted) = }")

0 commit comments

Comments
 (0)
Please sign in to comment.