排序
09_bubble_sort
// An highlighted block
def bubble_sort(alist):
'''冒泡排序'''
n = len(alist)
for j in range(n-1):
count = 0
for i in range(0,n-1):
#班长从头走到尾
if alist[i] > alist[i+1]:
alist[i], alist[i+1] = alist[i+1], alist[i]
count += 1
if 0 == count:
return
#i 0~n-2 range(0,n-1) j = 0
#i 0~n-3 range(0,n-1-1) j = 1
#i 0~n-4 range(0,n-1-2) j=2
#j=n i range(0,n-1-j)
if __name__ == "__main__":
li = [54,26,93,17,77,31,44,55,20]
print(li)
bubble_sort(li)
print(li)
10_select_sort
// An highlighted block
def select_sort(alist):
'''选择排序'''
n = len(alist)
for j in range(n-1): #j:0~n-2
min_index = j
for i in range(j+1,n):
if alist[min_index]>alist[i]:
min_index = i
alist[j],alist[min_index] = alist[min_index],alist[j]
if __name__ == "__main__":
li = [54,26,93,17,77,32,44,55,20]
print(li)
select_sort(li)
print(li)
11_insert_sort
// An highlighted block
def insert_sort(alist):
'''插入排序'''
n = len(alist)
#从右边的无序序列中取出多少个元素执行这样的过程
for j in range(1,n):
# i代表内层循环起始值
i = j
#执行从右边的无序序列中取出第一个元素,即i 位置的元素,然后将其插入到前面的正确位置中
while i>0:
if alist[i]<alist[i-1]:
alist[i],alist[i-1] = alist[i-1],alist[i]
i-=1
else:
break
if __name__ =="__main__":
li = [54,26,93]
print(li)
insert_sort(li)
print(li)
12_shell_sort
// An highlighted block
def shell_sort(alist):
'''希尔排序'''
n = len(alist)
gap = n//2
while gap >0:
#插入算法,与普通的插入算法的不同是gap的步长
for j in range(gap,n):
i=j
while i>0:
if alist[i]<alist[i-gap]:
alist[i],alist[i-gap] = alist[i-gap],alist[i]
i-=gap
else:
break
gap//=2
13_quick_sort
// An highlighted block
def quick_sort(alist,first, last):
'''快速排序'''
if first == last:
return
# n = len(alist)
mid_value = alist[first]
# low = 0
# high = n-1
low = first
high =last
while low < high:
#high 左移
while low < high and alist[high] >= mid_value:
high -= 1
alist[low] = alist[high]
while low < high and alist[low] < mid_value:
low += 1
alist[high] = alist[low]
#从循环退出时,low==high
alist[low] = mid_value
#对low左边的列表执行快速排序
quick_sort(alist,first,low-1)
#对low右边的列表排序
quick_sort(alist,low+1,last)
if __name__ == "__main__":
li =[54,26,93,17,77,31,44,55,20]
print(li)
quick_sort(li,0,len(li)-1)
print(li)
14_merge_sort
// An highlighted block
def merge_sort(alist):
'''归并排序'''
n = len(alist)
if n <= 1:
return alist
mid = n // 2
#left采用归并排序后形成的有序的新的列表
left_li = merge_sort(alist[:mid])
#right采用归并排序后形成的有序的新的列表
right_li = merge_sort(alist[mid:])
#将两个有序的子序列合并为一个新的整体
#merge(left,right)
left_pointer,right_pointer = 0,0
result = []
while left_pointer < len(left_li) and right_pointer < len(right_li):
if left_li[left_pointer] < right_li[right_pointer]:
result.append(left_li[left_pointer])
left_pointer += 1
else:
result.append(right_li[right_pointer])
right_pointer += 1
result += left_li[left_pointer:]
result += right_li[right_pointer:]
return result
if __name__ == "__main__":
li = [54,26,93,17,77,31,44,55,20]
print(li)
sorted_li = merge_sort(li)
print(li)
print(sorted_li)
15_binary_sort
// An highlighted block
# def binary_search(alist,item):
# '''二分查找,递归'''
# n = len(alist)
# if n > 0:
# mid = n//2
# if alist[mid]==item:
# return True
# elif item < alist[mid]:
# return binary_search(alist[:mid],item)
# else:
# return binary_search(alist[mid+1:],item)
# return False
def binary_search_2(alist,item):
'''二分查找,有序表,非递归'''
n = len(alist)
first = 0
last = n-1
while first <= last:
mid = (first+last)//2
if alist[mid] == item:
return True
elif item < alist[mid]:
last = mid + 1
return False
if __name__ =='__main__':
li = [17,20,26,31,44,54]
# print(binary_search(li,17))
# print(binary_search(li,54))
print(binary_search_2(li, 17))
print(binary_search_2(li, 54))