Skip to content

Commit 65a5be2

Browse files
authored
Add files via upload
2025/7/18 selection_sort.py
1 parent 0ca9fcd commit 65a5be2

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Sorting/Selection_Sort.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def selection_sort(arr:list[int])->list[int]:
2+
n = len(arr)#get the length of the array
3+
for i in range(n):
4+
min_index = i #set minium index to current index
5+
for j in range(i+1, n):# 從未排序區域尋找最小值
6+
if arr[j] < arr[min_index]:#if found a smaller element
7+
min_index = j #update the index of the minimum element
8+
arr[i], arr[min_index] = arr[min_index], arr[i] #swap the found minimum element return arr
9+
return arr
10+
if __name__ == "__main__":
11+
arr = [64, 25, 12, 22, 11]
12+
print(f"Original array:{arr}")
13+
selection_sort(arr)
14+
print(f"Sorted array:{arr}")

0 commit comments

Comments
 (0)