import random def bubble_sort(li): for i in range(len(li)-1): exch_flag = False for j in range(len(li)-i-1): if li[j+1] < li[j]: li[j],li[j+1] = li[j+1],li[j] exch_flag = True if not exch_flag: break def insert_sort(li): for i in range(1,len(li)): tmp = li[i] j = i-1 while j>=0 and tmp < li[j]: li[j+1] = li[j] j -= 1 li[j+1]=tmp def select_sort(li): for i in range(len(li)-1): min_loc = i for j in range(i+1,len(li)): print(i) if li[j] < li[min_loc]: min_loc = j if min_loc != i: li[i],li[min_loc] = li[min_loc],li[i] def _partition(li,left,right): temp = li[left] while left < right: while left < right and temp < li[right]: right -= 1 li[left] = li[right] while left < right and temp > li[left]: left += 1 li[right] = li[left] li[left] = temp return left def _quick_sort(li,left,right): if left < right: mid = _partition(li,left,right) _quick_sort(li,left,mid) _quick_sort(li,mid+1,right) def quick_sort(li): left = 0 right = len(li)-1 _quick_sort(li,left,right) def sift(li,low,high): i = low j = i * 2 + 1 tmp = li[i] while j <= high: if j < high and li[j] < li[j+1]: j += 1 if tmp < li[j]: li[i] = li[j] i = j j = 2 * i + 1 else: break li[i] = tmp def heap_sort(li): n = len(li) for i in range(n//2-1,-1,-1): sift(li,i,n-1) for j in range(n-1,-1,-1): li[0],li[j] = li[j],li[0] sift(li,0,j-1) def merge(li,left,mid,right): i = left j = mid+1 ltmp = [] while i<=mid and j<=right: if li[i] <= li[j]: ltmp.append(li[i]) i += 1 else: ltmp.append(li[j]) j += 1 while i<= mid: ltmp.append(li[i]) i += 1 while j <= right: ltmp.append(li[j]) j+= 1 li[left:right+1] = ltmp def _merge_sort(li,left,right): if left < right: mid = (left+right)//2 _merge_sort(li,left,mid) _merge_sort(li,mid+1,right) merge(li,left,mid,right) def merge_sort(li): left = 0 right = len(li)-1 _merge_sort(li,left,right) def sort_test(): li = [] for i in range(100): li.append(i) random.shuffle(li) select_sort(li) print(li) sort_test()