1
1
def merge (array , left , mid , right ):
2
+ """
3
+ Merge two sorted subarrays of array:
4
+ The left subarray is array[left..mid]
5
+ The right subarray is array[mid+1..right]
6
+ This function is used in Merge Sort algorithm.
7
+
8
+ Args:
9
+ array: list of elements to be sorted
10
+ left: int, leftmost index of left subarray
11
+ mid: int, rightmost index of left subarray
12
+ right: int, rightmost index of right subarray
13
+
14
+ Yields:
15
+ A tuple of array and 4 integers representing the state of merge operation:
16
+ - left+i: the index of the next item from left subarray to be compared
17
+ - mid+j: the index of the next item from right subarray to be compared
18
+ - left: the leftmost index of current merge iteration
19
+ - right: the rightmost index of current merge iteration
20
+ """
21
+
2
22
L = array [left :mid + 1 ]
3
23
R = array [mid + 1 :right + 1 ]
4
24
i = 0
@@ -25,6 +45,21 @@ def merge(array, left, mid, right):
25
45
26
46
27
47
def helper (arr ,n ,start ):
48
+ """
49
+ A helper function used in Strand Sort algorithm for sorting a list in-place.
50
+
51
+ Args:
52
+ arr: list to be sorted
53
+ n: int, length of arr
54
+ start: int, index of the first element in the sublist being sorted
55
+
56
+ Yields:
57
+ A tuple of arr and 4 integers representing the state of strand sort iteration:
58
+ - i: the index of the current element in sublist being processed
59
+ - count-1: the last element in sublist already sorted
60
+ - start: the first index of current sublist
61
+ - n-1: the last index of current sublist
62
+ """
28
63
29
64
if start == n :
30
65
return
@@ -46,6 +81,27 @@ def helper(arr,n,start):
46
81
yield from helper (arr ,n ,count )
47
82
48
83
def strandSort (arr ,* args ):
84
+ """
85
+ Sorts a list in-place using Strand Sort Algorithm.
86
+
87
+ Strand sort is a sorting algorithm that sorts a list by repeatedly
88
+ pulling sorted sublists out of the original list and merging them together.
89
+ The algorithm works by repeatedly taking a sublist of elements from the original
90
+ list that are in increasing order, and removing these elements from the original list.
91
+ These sublists are then merged together in order to form a new, sorted list.
92
+ The process is repeated until the entire original list has been sorted.
93
+
94
+ Args:
95
+ arr: list to be sorted
96
+ *args: additional arguments that are not used in the function.
97
+
98
+ Yields:
99
+ A sequence of tuples representing the state of Strand Sort Algorithm:
100
+ Each tuple is a sequence of list arr and 4 integers, that represent the
101
+ state of an iteration in the helper function. The last tuple contains
102
+ the fully sorted list.
103
+ """
104
+
49
105
size = len (arr )
50
106
51
107
yield from helper (arr ,size ,0 )
0 commit comments