-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselection_sort.py
More file actions
49 lines (35 loc) · 1.2 KB
/
selection_sort.py
File metadata and controls
49 lines (35 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Selection Sort Algorithm
Selection Sort finds the minimum element from the unsorted portion of the array
and places it at the beginning. This process is repeated for the remaining elements.
Time Complexity:
- Best Case: O(n²)
- Average Case: O(n²)
- Worst Case: O(n²)
Space Complexity: O(1) - in-place sorting
Stable: No (may change relative order of equal elements)
"""
def selection_sort(arr):
"""
Sorts an array using the Selection Sort algorithm.
Args:
arr: List of comparable elements to be sorted (modified in-place)
Returns:
None (sorts in-place)
"""
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Find the minimum element in remaining unsorted array
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
# Swap the found minimum element with the first element
arr[i], arr[min_idx] = arr[min_idx], arr[i]
# Example usage
if __name__ == "__main__":
numbers = [64, 34, 25, 12, 22, 11, 90]
print(f"Original array: {numbers}")
selection_sort(numbers)
print(f"Sorted array: {numbers}")