MCPcopy Create free account
hub / github.com/geekcomputers/Python / selection_sort

Function selection_sort

sorting_algos.py:4–48  ·  view source on GitHub ↗

Sorts a list in ascending order using the selection sort algorithm. Args: arr: List of comparable elements (e.g., integers, strings). Returns: The sorted list (in-place modification). Examples: >>> selection_sort([]) [] >>> selection_sort([1])

(arr: list)

Source from the content-addressed store, hash-verified

2
3
4def selection_sort(arr: list) -> list:
5 """Sorts a list in ascending order using the selection sort algorithm.
6
7 Args:
8 arr: List of comparable elements (e.g., integers, strings).
9
10 Returns:
11 The sorted list (in-place modification).
12
13 Examples:
14 >>> selection_sort([])
15 []
16 >>> selection_sort([1])
17 [1]
18 >>> selection_sort([1, 2, 3, 4])
19 [1, 2, 3, 4]
20 >>> selection_sort([4, 3, 2, 1])
21 [1, 2, 3, 4]
22 >>> selection_sort([3, 1, 3, 2])
23 [1, 2, 3, 3]
24 >>> selection_sort([5, 5, 5, 5])
25 [5, 5, 5, 5]
26 >>> selection_sort([2, 4, 1, 3])
27 [1, 2, 3, 4]
28 >>> selection_sort([-1, -3, 0, 2])
29 [-3, -1, 0, 2]
30 >>> selection_sort([0, -5, 3, -2, 1])
31 [-5, -2, 0, 1, 3]
32 >>> selection_sort(["banana", "apple", "cherry"])
33 ['apple', 'banana', 'cherry']
34 >>> selection_sort(["Apple", "banana", "Cherry"])
35 ['Apple', 'Cherry', 'banana']
36 >>> selection_sort([2147483647, -2147483648, 0])
37 [-2147483648, 0, 2147483647]
38 """
39
40 """TC : O(n^2)
41 SC : O(1)"""
42
43 n = len(arr)
44 for i in range(n):
45 for j in range(i + 1, n):
46 if arr[i] > arr[j]:
47 arr[i], arr[j] = arr[j], arr[i]
48 return arr
49
50
51def bubble_sort(arr: list) -> list:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected