#!/usr/bin/env python #-*- coding:utf-8 -*- def binary_search(source_data,find_nu): mid = len(source_data)/2 if len(source_data) >= 1: if source_data[mid] > find_nu: print 'data in left of %s' % source_data[mid] binary_search(source_data[:mid],find_nu) elif source_data[mid] < find_nu: print 'data in right of %s' % source_data[mid] binary_search(source_data[mid:],find_nu) else: print 'found_nu is',source_data[mid] else: print 'find_nu not found' if __name__ == '__main__': data = range(1,6000000) binary_search(data,0)
冒泡排序:
data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6] print("before sort:",data) previous = data[0] for j in range(len(data)): tmp = 0 for i in range(len(data)-1): if data[i] > data[i+1]: tmp=data[i] data[i] = data[i+1] data[i+1] = tmp print(data) print("after sort:",data)