Python quick sorting and python sorting

Source: Internet
Author: User

Python quick sorting and python sorting

Import randomdef rand (n): for I in range (n): yield random. randint () # create a random number list def createList (n): lists = [] for I in rand (n): lists. append (I) return lists # Fast sorting of def quick_sort (lists, l, r) by pitfall method: if (l <r): # x is the selected benchmark value, then, rank the data greater than the benchmark value to the right, and the data smaller than the benchmark value to the left I, j, x = l, r, lists [l] while (I <j ): # j traverse from the right, find the place where j <x, and exchange with lists [I], while (I <j and lists [j]> = x ): j-= 1 if (I <j): lists [I] = lists [j] I + = 1 # I traverse from the left and find the place where j> x, and exchange with lists [j] while (I <j and lists [I] <= x): I + = 1 if (I <j ): lists [j] = lists [I] j-= 1 # at this time, I is already greater than or equal to j, that is, the left side of I is less than x, and the right side is greater than x, so now I fill in x, then recursively sort lists [I] = x quick_sort (lists, l, I-1) quick_sort (lists, I + 1, r) lists = createList (1000) print listsquick_sort (lists, 0, len (lists)-1) print lists


Let's take a look at why the python code for fast sorting has been infinite loops? How to change

It is very simple to change the first if left> right to left> = right ..
When left = right, it should also be the exit condition. If you do not add it, it will fall into the while! = J ..
In this example, left = right = j, And I = left + 1 = j + 1, So I> j, and your judgment condition is only I <j, therefore, if both while and if are not satisfied, the endless loop will continue ..

How to Learn python?

Python 3.x and python 2.x are two development directions of Python. You only need to pay attention to one. After the selection, buy a book and start learning. Copy the above Code to run it. Ask questions in the book.
Start with the most basic questions, for example, how to use list, tuple, dict, set, String, etc,
Next is the practice of various statements, such as loops, branches, functions, namespaces, and exception Control.
The third step is the python standard library, which has a lot of content and is worth a long time to learn. For example, how to calculate the time, how to perform unit tests, how to perform high-precision floating-point operations, how to parse URLs, and how to perform regular matching.
Step 4 is about algorithms and data structures. You can find a book on the data structure and think about the data structures and algorithms (such as quick sorting) that can be implemented by C ++. How can you implement python.
Step 5 is system-related learning, such as how to create processes, threads, environment variables, file access permissions, and registry access.

After accumulating a certain amount of code (thousands of lines), you will be able to learn more. For example, you can select PyQt for gui learning. For network programming, you can select Django for learning. For database, you can select mysql-python for learning.

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.