# python：选择排序（打擂台法）的实现

```            ```
List

=

[

1

,

5

,

7

,

4

,

9

]

for

i

in

range

(

0

,

5

)

:

min

=

i

for

j

in

range

(

i

,

5

)

:

if

List

[

min

]

>

List

[

j

]

:

min

=

j
List

[

i

]

,

List

[

min

]

=

List

[

min

]

,

List

[

i

]

print

(

List

)

#输出结果为[1, 4, 5, 7, 9]

```
```

```            ```

def

Order

(

self

)

:

for

i

in

range

(

0

,

n

-

1

)

:

min

=

i

for

j

in

range

(

i

+

1

,

n

)

:

if

List

[

min

]

>

List

[

j

]

:

min

=

j
List

[

i

]

,

List

[

min

]

=

List

[

min

]

,

List

[

i

]

print

(

List

)

List

=

list

(

input

(

)

)

n

=

len

(

List

)

Order

(

List

)

```
```

## 代码解析：

1、定义一个函数Order，然后调用它

```            ```

def

Order

(

)

:

pass

Order

(

)

```
```

2、获取用户输入，并将用户输入转化为列表；得到列表的长度

```            ```

def

Order

(

self

)

:

pass

List

=

list

(

input

(

)

)

n

=

len

(

List

)

Order

(

List

)

```
```

3、设置第一个遍历，使得：在列表的第一个位置存放此队列的最小值

```            ```

def

Order

(

self

)

:

for

j

in

range

(

1

,

n

)

:

if

List

[

0

]

>

List

[

j

]

:

min

=

j
List

[

0

]

,

List

[

min

]

=

List

[

min

]

,

List

[

0

]

print

(

List

)

List

=

list

(

input

(

)

)

n

=

len

(

List

)

Order

(

List

)

```
```

4、设置第二个遍历：从第二个数开始依次把第一步的逻辑走一遍

```            ```

def

Order

(

self

)

:

for

i

in

range

(

0

,

n

-

1

)

:

min

=

i

for

j

in

range

(

i

+

1

,

n

)

:

if

List

[

min

]

>

List

[

j

]

:

min

=

j
List

[

i

]

,

List

[

min

]

=

List

[

min

]

,

List

[

i

]

print

(

List

)

List

=

list

(

input

(

)

)

n

=

len

(

List

)

Order

(

List

)

```
```

QQ号联系： 360901061

【本文对您有帮助就好】