Python bubble sort and quick sort encountered errors and problems

Source: Internet
Author: User

Today I saw the brother even in the PHP bubble sort and quick sort, think of the next should be able to be implemented in Python.

Bubble sort function:

def mysort (x):    len1 = Len (x) for    I in range (len1-1,0,-1): for        J in Range (0,i):            if x[j]>x[j+1]:                x[ J],X[J+1]=X[J+1],X[J]    return x

The third line of code, is to let I the value 9 to 1, because the bubble sort is a large number to go back, when the second loop, the largest number is already at the end, so do not need to compare at once.

Similarly, the third time, just let it compare to Len1-2, the fourth time, compared to len1-1.

So the number of cycles can be reduced by half.

Python supports the direct exchange of list values, which is also convenient.

Quick Sort function:

def qsort (x):
if (x = = []):
return []
Len1 = Len (x)
left = []
right = []
Key = X[0]
For I in Range (1,LEN1):
if (X[i]<=key):
Left.append (X[i])
Else
Right.append (X[i])
left = Qsort (left)
right = Qsort (right)
return left + [key] + right

The quick sort first has a comparison value key, which takes the first value in the list. Let the other values in the list compare them.

If it is less than it is placed in the right list,

If it is greater than it is placed in the left list.

and then recursion. (Not very understanding of recursion.) Only know that the function itself calls itself. )

Finally, left, compare value key (need to convert to list type), right is connected together.

An error has occurred:

Runtimeerror:maximum recursion depth exceeded while calling a Python object

Inquiry to know:

Originally in Python, the maximum depth of the recursive function is 999. Exceeding this depth will cause an error.

We just need to precede the code with

Import Syssys.setrecursionlimit (1000000)

Set to 1000000 to resolve.

The final code and test efficiency:

#!usr/bin/env python#!coding=utf-8__author__ = ' zhengjim ' Import timeimport randomimport syssys.setrecursionlimit ( 1000000) def mysort (x):    len1 = Len (x) for    I in range (len1-1,0,-1): for        J in Range (0,i):            if x[j]>x[j+1] :                X[j],x[j+1]=x[j+1],x[j]    return xdef qsort (x):    if (x = = []):        return []    len1 = Len (x) Left    = []< C10/>right = []    key = x[0] for    i in range (1,LEN1):        if (x[i]<=key):            left.append (X[i])        else:            right.append (X[i]) Left    = Qsort (left) Right    = Qsort (right)    return left + [key] + rightif __name__ = = ' __main__ ':    x=[] for    i in range (1000000):        j = random.randint (1,10000)        X.append (j)    start = Time.clock ()    qsort (x)       # change function, compare efficiency    end =time.clock ()    print '%f '% (End-start)

 

Defines a 1000000 chaotic sequence table.

Experimental results:

# bubble sort ran for more than 5 minutes
# Quick Sort 12.017942
#系统函数 0.428260

Python bubble sort and quick sort encountered errors and problems

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.