MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / slowsort

Function slowsort

sorts/slowsort.py:15–55  ·  view source on GitHub ↗

Sorts sequence[start..end] (both inclusive) in-place. start defaults to 0 if not given. end defaults to len(sequence) - 1 if not given. It returns None. >>> seq = [1, 6, 2, 5, 3, 4, 4, 5]; slowsort(seq); seq [1, 2, 3, 4, 4, 5, 5, 6] >>> seq = []; slowsort(seq); seq [

(sequence: list, start: int | None = None, end: int | None = None)

Source from the content-addressed store, hash-verified

13
14
15def slowsort(sequence: list, start: int | None = None, end: int | None = None) -> None:
16 """
17 Sorts sequence[start..end] (both inclusive) in-place.
18 start defaults to 0 if not given.
19 end defaults to len(sequence) - 1 if not given.
20 It returns None.
21 >>> seq = [1, 6, 2, 5, 3, 4, 4, 5]; slowsort(seq); seq
22 [1, 2, 3, 4, 4, 5, 5, 6]
23 >>> seq = []; slowsort(seq); seq
24 []
25 >>> seq = [2]; slowsort(seq); seq
26 [2]
27 >>> seq = [1, 2, 3, 4]; slowsort(seq); seq
28 [1, 2, 3, 4]
29 >>> seq = [4, 3, 2, 1]; slowsort(seq); seq
30 [1, 2, 3, 4]
31 >>> seq = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; slowsort(seq, 2, 7); seq
32 [9, 8, 2, 3, 4, 5, 6, 7, 1, 0]
33 >>> seq = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; slowsort(seq, end = 4); seq
34 [5, 6, 7, 8, 9, 4, 3, 2, 1, 0]
35 >>> seq = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]; slowsort(seq, start = 5); seq
36 [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
37 """
38 if start is None:
39 start = 0
40
41 if end is None:
42 end = len(sequence) - 1
43
44 if start >= end:
45 return
46
47 mid = (start + end) // 2
48
49 slowsort(sequence, start, mid)
50 slowsort(sequence, mid + 1, end)
51
52 if sequence[end] < sequence[mid]:
53 sequence[end], sequence[mid] = sequence[mid], sequence[end]
54
55 slowsort(sequence, start, end - 1)
56
57
58if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected