forked from EINDEX/Python-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.py
More file actions
29 lines (24 loc) · 777 Bytes
/
bubble_sort.py
File metadata and controls
29 lines (24 loc) · 777 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""冒泡排序 """
def bubble_sort(arr):
length = len(arr)
for index in range(length):
for j in range(1, length - index):
if arr[j - 1] > arr[j]:
# 交换两者数据,这里没用temp是因为python 特性元组。
arr[j - 1], arr[j] = arr[j], arr[j - 1]
return arr
def bubble_sort_flag(arr):
length = len(arr)
for index in range(length):
# 标志位
flag = True
for j in range(1, length - index):
if arr[j - 1] > arr[j]:
arr[j - 1], arr[j] = arr[j], arr[j - 1]
flag = False
if flag:
# 没有发生交换,直接返回list
return arr
return arr