Skip to content

Commit b8eb3a7

Browse files
authoredFeb 22, 2023
Add docstrings for strandSort
1 parent dda26d5 commit b8eb3a7

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
 

‎src/algorithms/strandSort.py

+56
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
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+
222
L = array[left:mid+1]
323
R = array[mid+1:right+1]
424
i = 0
@@ -25,6 +45,21 @@ def merge(array, left, mid, right):
2545

2646

2747
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+
"""
2863

2964
if start==n:
3065
return
@@ -46,6 +81,27 @@ def helper(arr,n,start):
4681
yield from helper(arr,n,count)
4782

4883
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+
49105
size = len(arr)
50106

51107
yield from helper(arr,size,0)

0 commit comments

Comments
 (0)
Failed to load comments.