Python Selection Sort
Selection sort is a simple and intuitive sorting algorithm. It works as follows: first find the smallest (or largest) element in the unsorted sequence and put it at the beginning of the sorted sequence; then keep looking for the smallest (or largest) element among the remaining unsorted elements and put it at the end of the sorted sequence; and so on until all elements are sorted.
Example
import
sys
A
=
[
64
,
25
,
12
,
22
,
11
]
for
i
in
range
(
len
(
A
)
)
:
min_idx
=
i
for
j
in
range
(
i
1
,
len
(
A
)
)
:
if
A
[
min_idx
]
A
[
j
]
:
min_idx
=
j
A
[
i
]
,
A
[
min_idx
]
=
A
[
min_idx
]
,
A
[
i
]
(
"
Sorted array:
"
)
for
i
in
range
(
len
(
A
)
)
:
(
"
%d
"
%
A
[
i
]
)
,
The output of the code above is:
Sorted array:
11
12
22
25
64
Python3 Examples