Fast sorting algorithm

Source: Internet
Author: User

Fast sorting algorithm

Quick Line Idea:

Take an element p (the first element) so that the element P is returned to the position;

The list is divided by P into two parts, the left side is smaller than p, the right side is larger than p;

Recursion completes the sort.

The complexity of time is:

O (NLOGN)

ImportRandomImport TimedefCal_time (func):"""test time Adorner:p Aram Func: Receives a function: return:"""    defWrapper (*args, * *Kwargs): Ti=time.time () x= Func (*args, * *Kwargs) Ti2=time.time ()Print("Time Cost :", Func.__name__, Ti2-ti)returnxreturnwrapperdefquick_sort_x (data, left, right):ifLeft <Right :#Place the number in the left position, return the digitMID =partition (data, left, right)#Recursive OperationQuick_sort_x (data, left, mid-1) quick_sort_x (data, mid+ 1, right)defpartition (data, left, right):"""To implement a bitwise operation:P Aram data: Unordered list:p Aram Left: List the subscript:p Aram Right:len (data)-1:return: Sorted list"""tmp=Data[left] whileLeft <Right : whileLeft < Right andData[right] >=Tmp:right-= 1Data[left]=Data[right] whileLeft < Right andData[left] <=Tmp:left+ = 1Data[right]=Data[left] Data[left]=tmpreturnRight@cal_timedefquick_sort (data):returnquick_sort_x (data, 0, Len (data)-1) da= List (Range (10000) ) random.shuffle (DA) quick_sort (DA)Print(DA)

Fast sorting algorithm

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.