Python for fast sorting and random quick sorting

Source: Internet
Author: User

Directly on the code:

#Quick Sort#Coding:utf-8defquicksort (a,left,right):if(left<Right ): Mid=partition (A,left,right) quicksort (A,left,mid-1) quicksort (A,mid+1, right)defpartition (A,left,right): x=A[right] I= Left-1#Initial I points to an empty, guaranteeing 0 to I are less than or equal to x     forJinchRange (Left,right):#J is used to look for smaller than X, to find and i+1 exchange, to ensure that I was less than or equal to x        if(a[j]<=x): I= I+1A[i],a[j]=A[j],a[i] A[i+1],a[right] = a[right],a[i+1]#0 to I are less than or equal to X, so the final position of X is i+1    returnI+1 while(True):Try: S= Input ("enter the array to be sorted: \ n")#array to be queuedL =S.split () a= [Int (t) forTinchl] Quicksort (A,0,len (a)-1)        Print("after sorting:")         forIteminchA:Print(item,end=' ')        Print("\ n")    except:         Break

Random Quick sort:

#Random Quick Sort#Coding:utf-8ImportRandomdefRandom_quicksort (a,left,right):if(left<Right ): Mid=random_partition (a,left,right) random_quicksort (A,left,mid-1) Random_quicksort (A,mid+1, right)defrandom_partition (a,left,right): t= Random.randint (left,right)#generate a random number between [Left,right]A[t],a[right] =A[right],a[t] x=A[right] I= Left-1#Initial I points to an empty, guaranteeing 0 to I are less than or equal to x     forJinchRange (Left,right):#J is used to look for smaller than X, to find and i+1 exchange, to ensure that I was less than or equal to x        if(a[j]<=x): I= I+1A[i],a[j]=A[j],a[i] A[i+1],a[right] = a[right],a[i+1]#0 to I are less than or equal to X, so the final position of X is i+1    returnI+1 while(True):Try: S= Input ("enter the array to be sorted: \ n")#array to be queuedL =S.split () a= [Int (t) forTinchl] Random_quicksort (A,0,len (a)-1)        Print("after sorting:")         forIteminchA:Print(item,end=' ')        Print("\ n")    except:         Break

Python for fast sorting and random quick sorting

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.