|
1 |
| -from insertion_sort import sort |
2 |
| -# iterative Timsort function to sort the |
3 |
| -# array[0...n-1] (similar to merge sort) |
4 |
| -def tim_sort(arr, n): |
5 |
| - |
6 |
| - # Sort individual subarrays of size RUN |
7 |
| - for i in range(0, n, RUN): |
8 |
| - sort(arr, i, min((i+31), (n-1))) |
9 |
| - |
10 |
| - # start merging from size RUN (or 32). It will merge |
11 |
| - # to form size 64, then 128, 256 and so on .... |
12 |
| - size = RUN |
13 |
| - while size < n: |
14 |
| - |
15 |
| - # pick starting point of left sub array. We |
16 |
| - # are going to merge arr[left..left+size-1] |
17 |
| - # and arr[left+size, left+2*size-1] |
18 |
| - # After every merge, we increase left by 2*size |
19 |
| - for left in range(0, n, 2*size): |
20 |
| - |
21 |
| - # find ending point of left sub array |
22 |
| - # mid+1 is starting point of right sub array |
23 |
| - mid = left + size - 1 |
24 |
| - right = min((left + 2*size - 1), (n-1)) |
25 |
| - |
26 |
| - # merge sub array arr[left.....mid] & |
27 |
| - # arr[mid+1....right] |
28 |
| - merge(arr, left, mid, right) |
29 |
| - |
30 |
| - size = 2*size |
31 |
| - |
32 |
| -def merge(arr, l, m, r): |
33 |
| - |
34 |
| - # original array is broken in two parts |
35 |
| - # left and right array |
36 |
| - len1, len2 = m - l + 1, r - m |
37 |
| - left, right = [], [] |
38 |
| - for i in range(0, len1): |
39 |
| - left.append(arr[l + i]) |
40 |
| - for i in range(0, len2): |
41 |
| - right.append(arr[m + 1 + i]) |
42 |
| - |
43 |
| - i, j, k = 0, 0, l |
44 |
| - # after comparing, we merge those two array |
45 |
| - # in larger sub array |
46 |
| - while i < len1 and j < len2: |
47 |
| - |
48 |
| - if left[i] <= right[j]: |
49 |
| - arr[k] = left[i] |
50 |
| - i += 1 |
51 |
| - |
52 |
| - else: |
53 |
| - arr[k] = right[j] |
54 |
| - j += 1 |
55 |
| - |
56 |
| - k += 1 |
57 |
| - |
58 |
| - # copy remaining elements of left, if any |
59 |
| - while i < len1: |
60 |
| - |
61 |
| - arr[k] = left[i] |
62 |
| - k += 1 |
63 |
| - i += 1 |
64 |
| - |
65 |
| - # copy remaining element of right, if any |
66 |
| - while j < len2: |
67 |
| - arr[k] = right[j] |
68 |
| - k += 1 |
69 |
| - j += 1 |
| 1 | +def inplace_insertion_sort(arr, start_ind, end_ind): |
| 2 | + """ |
| 3 | + Performs an in-place insertion sort over a continuous slice of an |
| 4 | + array. A natural way to avoid this would be to use numpy arrays, |
| 5 | + where slicing does not copy. |
| 6 | +
|
| 7 | + This is in-place and has no result. |
| 8 | +
|
| 9 | + :param arr: the array to sort |
| 10 | + :param start_ind: the index to begin sorting at |
| 11 | + :param end_ind: the index to end sorting at. This index is excluded |
| 12 | + from the sort (i.e., len(arr) is ok) |
| 13 | + """ |
| 14 | + for i in range(start_ind + 1, end_ind): |
| 15 | + current_number = arr[i] |
| 16 | + |
| 17 | + for j in range(i - 1, start_ind - 1, -1): |
| 18 | + if arr[j] > current_number: |
| 19 | + arr[j], arr[j + 1] = arr[j + 1], arr[j] |
| 20 | + else: |
| 21 | + arr[j + 1] = current_number |
| 22 | + break |
| 23 | + |
| 24 | + |
| 25 | +# iterative Timsort function to sort the |
| 26 | +# array[0...n-1] (similar to merge sort) |
| 27 | +def tim_sort(arr, run=32): |
| 28 | + """ |
| 29 | + Tim sort algorithm. See https://en.wikipedia.org/wiki/Timsort. |
| 30 | + This is performed in-place. |
| 31 | +
|
| 32 | + :param arr: list of values to sort |
| 33 | + :param run: the largest array that is sorted with an insertion sort. |
| 34 | + :return: the sorted array |
| 35 | + """ |
| 36 | + |
| 37 | + # Sort individual subarrays of size run |
| 38 | + |
| 39 | + for i in range(0, len(arr), run): |
| 40 | + inplace_insertion_sort(arr, i, min(i + run, len(arr))) |
| 41 | + |
| 42 | + # start merging from size RUN (or 32). It will merge |
| 43 | + # to form size 64, then 128, 256 and so on .... |
| 44 | + size = run |
| 45 | + while size < len(arr): |
| 46 | + # pick starting point of left sub array. We |
| 47 | + # are going to merge arr[left..left+size-1] |
| 48 | + # and arr[left+size, left+2*size-1] |
| 49 | + # After every merge, we increase left by 2*size |
| 50 | + for left in range(0, len(arr), 2 * size): |
| 51 | + # find ending point of left sub array |
| 52 | + # mid+1 is starting point of right sub array |
| 53 | + mid = left + size |
| 54 | + right = min(left + (2 * size), len(arr)) |
| 55 | + |
| 56 | + # merge sub array arr[left.....mid] & |
| 57 | + # arr[mid+1....right] |
| 58 | + merge(arr, left, mid, right) |
| 59 | + |
| 60 | + size = 2 * size |
| 61 | + return arr |
| 62 | + |
| 63 | +def merge(arr, left, mid, right): |
| 64 | + """ |
| 65 | + Merge of two sections of array, both of which are individually |
| 66 | + sorted. The result is that the entire chunk is sorted. Note that right |
| 67 | + edges are exclusive (like slicing). |
| 68 | +
|
| 69 | + This modifies the passed array, but requires a complete copy of the array. |
| 70 | +
|
| 71 | + .. code:: python |
| 72 | +
|
| 73 | + merge([0, -1, 1, 3, 2, 4], 2, 4, 6) # [0, -1, 1, 2, 3, 4] |
| 74 | +
|
| 75 | + :param arr: the array which should have a portion sorted in-place |
| 76 | + :param left: the left-most index which is included in the merge |
| 77 | + :param mid: the first index that belongs to the second section |
| 78 | + :param right: the right-edge in the merge, which is not included in the sort. |
| 79 | + """ |
| 80 | + # original array is broken in two parts |
| 81 | + # left and right array |
| 82 | + left_arr = arr[left:mid] |
| 83 | + right_arr = arr[mid:right] |
| 84 | + |
| 85 | + left_pos = 0 |
| 86 | + right_pos = 0 |
| 87 | + arr_ind = left |
| 88 | + # after comparing, we merge those two array |
| 89 | + # in larger sub array |
| 90 | + while left_pos < len(left_arr) and right_pos < len(right_arr): |
| 91 | + if left_arr[left_pos] <= right_arr[right_pos]: |
| 92 | + arr[arr_ind] = left_arr[left_pos] |
| 93 | + left_pos += 1 |
| 94 | + else: |
| 95 | + arr[arr_ind] = right_arr[right_pos] |
| 96 | + right_pos += 1 |
| 97 | + |
| 98 | + arr_ind += 1 |
| 99 | + |
| 100 | + # copy remaining elements of left, if any |
| 101 | + for i in range(left_pos, len(left_arr)): |
| 102 | + arr[arr_ind] = left_arr[i] |
| 103 | + arr_ind += 1 |
| 104 | + |
| 105 | + # copy remaining element of right, if any |
| 106 | + for i in range(right_pos, len(right_arr)): |
| 107 | + arr[arr_ind] = right_arr[i] |
| 108 | + arr_ind += 1 |
0 commit comments