|
1 | | -def binary_search(the_array, item, start, end): |
| 1 | +def binary_search(lst, item, start, end): |
2 | 2 | if start == end: |
3 | | - if the_array[start] > item: |
| 3 | + if lst[start] > item: |
4 | 4 | return start |
5 | 5 | else: |
6 | 6 | return start + 1 |
7 | 7 | if start > end: |
8 | 8 | return start |
9 | 9 |
|
10 | | - mid = (start + end)/ 2 |
11 | | - if the_array[mid] < item: |
12 | | - return binary_search(the_array, item, mid + 1, end) |
13 | | - elif the_array[mid] > item: |
14 | | - return binary_search(the_array, item, start, mid - 1) |
| 10 | + mid = (start + end) // 2 |
| 11 | + if lst[mid] < item: |
| 12 | + return binary_search(lst, item, mid + 1, end) |
| 13 | + elif lst[mid] > item: |
| 14 | + return binary_search(lst, item, start, mid - 1) |
15 | 15 | else: |
16 | 16 | return mid |
17 | 17 |
|
18 | 18 |
|
19 | | -""" |
20 | | -Insertion sort that the heap sort uses if the array size is small or if |
21 | | -the size of the "run" is small |
22 | | -""" |
23 | | -def insertion_sort(the_array): |
24 | | - l = len(the_array) |
25 | | - for index in range(1, l): |
26 | | - value = the_array[index] |
27 | | - pos = binary_search(the_array, value, 0, index - 1) |
28 | | - the_array = the_array[:pos] + [value] + the_array[pos:index] + the_array[index+1:] |
29 | | - return the_array |
| 19 | +def insertion_sort(lst): |
| 20 | + length = len(lst) |
| 21 | + |
| 22 | + for index in range(1, length): |
| 23 | + value = lst[index] |
| 24 | + pos = binary_search(lst, value, 0, index - 1) |
| 25 | + lst = lst[:pos] + [value] + lst[pos:index] + lst[index+1:] |
| 26 | + |
| 27 | + return lst |
| 28 | + |
30 | 29 |
|
31 | 30 | def merge(left, right): |
32 | | - """Takes two sorted lists and returns a single sorted list by comparing the |
33 | | - elements one at a time. |
34 | | - [1, 2, 3, 4, 5, 6] |
35 | | - """ |
36 | 31 | if not left: |
37 | 32 | return right |
| 33 | + |
38 | 34 | if not right: |
39 | 35 | return left |
| 36 | + |
40 | 37 | if left[0] < right[0]: |
41 | 38 | return [left[0]] + merge(left[1:], right) |
| 39 | + |
42 | 40 | return [right[0]] + merge(left, right[1:]) |
43 | 41 |
|
44 | 42 |
|
45 | | -def timsort(the_array): |
| 43 | +def timsort(lst): |
46 | 44 | runs, sorted_runs = [], [] |
47 | | - l = len(the_array) |
48 | | - new_run = [the_array[0]] |
49 | | - for i in range(1, l): |
50 | | - if i == l-1: |
51 | | - new_run.append(the_array[i]) |
| 45 | + length = len(lst) |
| 46 | + new_run = [lst[0]] |
| 47 | + sorted_array = [] |
| 48 | + |
| 49 | + for i in range(1, length): |
| 50 | + if i == length - 1: |
| 51 | + new_run.append(lst[i]) |
52 | 52 | runs.append(new_run) |
53 | 53 | break |
54 | | - if the_array[i] < the_array[i-1]: |
| 54 | + |
| 55 | + if lst[i] < lst[i - 1]: |
55 | 56 | if not new_run: |
56 | | - runs.append([the_array[i-1]]) |
57 | | - new_run.append(the_array[i]) |
| 57 | + runs.append([lst[i - 1]]) |
| 58 | + new_run.append(lst[i]) |
58 | 59 | else: |
59 | 60 | runs.append(new_run) |
60 | 61 | new_run = [] |
61 | 62 | else: |
62 | | - new_run.append(the_array[i]) |
63 | | - for each in runs: |
64 | | - sorted_runs.append(insertion_sort(each)) |
65 | | - sorted_array = [] |
| 63 | + new_run.append(lst[i]) |
| 64 | + |
| 65 | + for run in runs: |
| 66 | + sorted_runs.append(insertion_sort(run)) |
| 67 | + |
66 | 68 | for run in sorted_runs: |
67 | 69 | sorted_array = merge(sorted_array, run) |
68 | | -print(sorted_array) |
| 70 | + |
| 71 | + return sorted_array |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + |
| 76 | + lst = [5,9,10,3,-4,5,178,92,46,-18,0,7] |
| 77 | + sorted_lst = timsort(lst) |
| 78 | + print(sorted_lst) |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + main() |
0 commit comments