Common Algorithms (ii)-Advanced algorithms

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.