Quick sorting (Quick sort)
Start by selecting one of the data (usually the first number of the array) as the key data, and then putting all the smaller numbers in front of it, all the larger numbers are placed behind it, this process is called a quick sort.
The array is split into two arrays and then the remaining two arrays are sorted, so the loop is done until one element is left.
Import Time,randomimport copydef Cal_time (func): #该装饰器用来测量函数运行的时间 def wrapper (*args,**kwargs): T1 = Tim E.time () res = func (*args,**kwargs) t2 = Time.time () print ("%s Running time:%s secs."% (func.__name __, T2-T1)) return res return wrapperdef Query_sort (data,left,right): if left < Right:mid = part Ition (data,left,right) query_sort (data,left,mid-1) Query_sort (data,mid+1,right) def partition (data,left,right ): temp = Data[left] While left < Right:while Left<right and Data[right] >= temp:right -=1 Data[left] = Data[right] While Left<right and Data[left] <= temp:left +=1 data[ Right] = Data[left] data[left] = temp return left@cal_timedef query_sort_h (data): Query_sort (Data,0,len (data)-1) @ Cal_timedef sort_fun (data): Data.sort () data = List (range (10000)) Random.shuffle (data) data1 = copy.deepcopy (data) print (data) query_sort_h (data) sorT_fun (data1) print (data)
Common Algorithms (ii)-Advanced algorithms