-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.py
More file actions
35 lines (29 loc) · 812 Bytes
/
selection_sort.py
File metadata and controls
35 lines (29 loc) · 812 Bytes
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
# coding=utf-8
from typing import List
class SelectionSort(object):
def sort(self, arr: List[int]):
"""
:param arr:
:return:
"""
if not arr:
return
for i in range(len(arr)):
min_idx = i
for j in range(i + 1, len(arr)):
if arr[j] < arr[min_idx]:
min_idx = j
arr[min_idx], arr[i] = arr[i], arr[min_idx]
def sort2(self, arr: List[int]):
"""
:param arr:
:return:
"""
if not arr:
return
for i in range(len(arr) - 1, 0, -1):
max_idx = i
for j in range(0, i):
if arr[j] > arr[max_idx]:
max_idx = j
arr[max_idx], arr[i] = arr[i], arr[max_idx]