Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Selection Sort

Python Selection Sort

Document Object Reference Manual Python3 Examples

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

]

print

(

"

Sorted array:

"

)

for

i

in

range

(

len

(

A

)

)

:

print

(

"

%d

"

%

A

[

i

]

)

,

The output of the code above is:


Sorted array:
11
12
22
25
64

Document Object Reference Manual Python3 Examples