S12-20160514-day17
Pytho Automation Development day17date:2016.05.14
@南非波波
Course Outline:
Http://www.cnblogs.com/alex3714/articles/5474411.html
First, bubble sorting algorithm
#!/usr/local/env Python3 "author:@ South Africa bobo Blog:http://www.cnblogs.com/songqingbo/e-mail:[email protected]" Import random,time#m bubble sort def bubble_up1 (array): ' m bubble sort algorithm:p Aram array:: return:count:6190862 time:6.70638 3466720581 ' Count = 0 for I in range (len (array)): for J in range (Len (array)-1-i): If arr AY[J] > array[j + 1]: temp = array[j + 1] array[j + 1] = Array[j] array[j] = Temp Count + = 1 Print ("Count:", count) print ("Array:", array) def bubble_up2 (array): ' M bubble row Sequence algorithm:p Aram array:: return:count:5000 time:3.825218915939331 "Count = 0 for I in range (len (array)): For j in Range (Len (array)-1-i): Big_temp = J if array[big_temp] > array[j + 1]: Big_temp = j + 1 temp = array[big_temp] array[i] = array[big_temp] array[big_temp] = Temp Count + = 1 Print ("Count:", CounT) print ("Array:", array) if __name__ = = ' __main__ ': array = [] for i in range: Array.append (RANDOM.R Andrange (+)) Time_start = Time.time () bubble_up2 (array) time_end = Time.time () # Print (array[0:100]) Print ("Time:", Time_end-time_start)
Second, choose the sort
def select1(array): ‘‘‘ 选择排序算法v1 :param array: :return:count: 1996750 time: 3.2961885929107666 ‘‘‘ count = 0 for i in range(len(array)): for j in range(i,len(array)): if array[i] > array[j]: temp = array[j] array[j] = array[i] array[i] = temp count += 1 print("count:",count) print("array:",array)def select2(array): ‘‘‘ 选择排序算法v2 :param array: :return:count: 5000 time: 2.4801418781280518 ‘‘‘ count = 0 for i in range(len(array)): smallest_index = i for j in range(i,len(array)): if array[smallest_index] > array[j]: smallest_index = j temp = array[smallest_index] array[smallest_index] = array[i] array[i] = temp count += 1 print("count:",count) print("array:",array)
Three, direct insertion sorting algorithm
def insert1(array): ‘‘‘ 插入排序算法 :param array: :return: count: 4999 time: 3.685210704803467 ‘‘‘ count = 0 for index in range(1, len(array)): current_val = array[index] # 先记下来每次大循环走到的第几个元素的值 position = index while position > 0 and array[ position - 1] > current_val: # 当前元素的左边的紧靠的元素比它大,要把左边的元素一个一个的往右移一位,给当前这个值插入到左边挪一个位置出来 array[position] = array[position - 1] # 把左边的一个元素往右移一位 position -= 1 # 只一次左移只能把当前元素一个位置 ,还得继续左移只到此元素放到排序好的列表的适当位置 为止 array[position] = current_val # 已经找到了左边排序好的列表里不小于current_val的元素的位置,把current_val放在这里 count += 1 print("count:", count) print(array)
Iv. Quick Sort
def quick_sort(array,start,end): ‘‘‘ 快速排序算法 :param array: :param start: :param end: :return:time: 0.03600192070007324 ‘‘‘ if start >= end: return k = array[start] left_flag = start right_flag = end while left_flag < right_flag: while array[right_flag] > k: right_flag -= 1 temp = array[right_flag] array[left_flag] = array[right_flag] array[right_flag] = temp while array[left_flag] <= k: left_flag += 1 temp = array[left_flag] array[left_flag] = array[right_flag] array[right_flag] = temp quick_sort(array,start,left_flag - 1) quick_sort(array,left_flag + 1,end)
Python Development path day17-algorithm design (bubble sort, select sort, insert sort, binary tree)