Python Exercise Example 37
**Problem:** Sort 10 numbers.
**Program analysis:** You can use the selection method: from the last 9 comparison processes, select the smallest and swap it with the first element; then repeat, comparing the second element with the remaining 8 and swapping.
Program source code:
Example
#!/usr/bin/python
-- coding: UTF-8 --
if
name
==
"
main
"
:
N
=
10
input data
(
'
Please enter 10 numbers:
\n
'
)
l
=
[
]
for
i
in
range
(
N
)
:
l
.
append
(
int
(
input
(
'
Enter a number:
\n
'
)
)
)
for
i
in
range
(
N
)
:
(
l
[
i
]
)
Sort 10 numbers
for
i
in
range
(
N
1
)
:
min
=
i
for
j
in
range
(
i
1
,
N
)
:
if
l
[
min
]
l
[
j
]
:
min
=
j
l
[
i
]
,
l
[
min
]
=
l
[
min
]
,
l
[
i
]
(
'
After sorting:
'
)
for
i
in
range
(
N
)
:
(
l
[
i
]
)
The output of the above example is:
Please enter 10 numbers:
Enter a number:
5
Enter a number:
3
Enter a number:
23
Enter a number:
67
Enter a number:
2
Enter a number:
56
Enter a number:
45
Enter a number:
98
Enter a number:
239
Enter a number:
9
5
3
23
67
2
56
45
98
239
9
After sorting:
2
3
5
9
23
45
56
67
98
239
Python 100 Examples