Algorithm-python for fast sorting (the idea of the binary method) and python for the binary method

Source: Internet
Author: User

Algorithm-python for fast sorting (the idea of the binary method) and python for the binary method
Implementation

Store the required numbers in a list.

You will understand:

Implementation Code
1 # coding: UTF-8 2 # Fast sorting. Use the binary algorithm to implement 3 4 5 def quick_sort (list, left, right): 6 if left> right: 7 return 8 temp = list [left] 9 I = left10 j = right11 while I! = J: 12 # first search for 13 while list [j]> = temp and I from the right to the left <j: 14 j-= 115 # search for 16 while list [I] <= temp and I <j: 17 I + = 118 if I <j: 19 t = list [I] 20 list [I] = list [j] 21 list [j] = t22 # Replace the reference number 23 list [left] = list [I] 24 list [I] = temp25 # recursively call 26 quick_sort (list, left, I-1) 27 quick_sort (list, I + 1, right) 28 29 30 while True: 31 list = [] 32 try: 33 num = int (input ('How many do you want to compare? \ N') 34 TB t ValueError: 35 continue36 for k in range (num): 37 a = int (input ('Enter the nth '+ str (k + 1) + 'count: \ n') 38 list. append (a) 39 quick_sort (list, 0, num-1) 40 print ('sorting result: ') 41 for l in range (len (list )): 42 print (list [l], end = '') 43 print ()

Quick sorting is much more efficient than Bubble sorting ~

Related Article

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.