Python daily delicious (30)-Fast sorting of Python data structures and algorithms

Source: Internet
Author: User
ArticleDirectory
    • 1. Quick sorting
    • 4. Results

The principle of quick sorting is to extract the first number and divide the entire array into two waves. One dial is greater than this number, and the other wave is smaller than this number, then recursively process the first and second digits in the same way. All of them are "quick sorting", and the efficiency is certainly better than other general sorting.AlgorithmHigh. Let's verify the performance difference between the so-called "Quick Sort" and "bubble sort.

1. Quick sorting Def Quicksort ( Data , Low = 0 , High = None ):
If High = None :
High = Len ( Data ) - 1
If Low < High :
S , I , J = Data [ Low ], Low , High
While I < J :
While I < J And Data [ J ] > = S :
J = J - 1
If I < J :
Data [ I ] = Data [ J ]
I = I + 1
While I < J And Data [ I ] <= S :
I = I + 1
If I < J :
Data [ J ] = Data [ I ]
J = J - 1
Data [ I ] = S
Quicksort ( Data , Low , I - 1 )
Quicksort ( Data , I + 1 , High ) 2. Bubble Sorting
Def Bubblesort ( Data ):
For I In Range ( Len ( Data ) - 1 , 0 , - 1 ):
For J In Range ( 0 , I ):
If Data [ J ] > Data [ J + 1 ]:
Data [ J ], Data [ J + 1 ] = Data [ J + 1 ], Data [ J ]

 

3. Performance Comparison

It seems that the Bubble Sorting only requires five rows, Which is concise enough, but what is the performance? To compare:

Import Random
Import Datetime
Import Copy

Def Sort_perfmon ( Sortfunc , Data ):
Sort_data = Copy . Deepcopy ( Data )
T1 = Datetime . Datetime . Now ()
Sortfunc ( Sort_data )
T2 = Datetime . Datetime . Now ()
Print Sortfunc . _ Name __ , T2 - T1
# Print sort_data

Data = [ Random . Randint ( 0 , 65536 ) For I In Range ( 2000 )]
# Print data
Sort_perfmon ( Quicksort , Data )
Sort_perfmon ( Bubblesort , Data )

 

4. Results

By sorting 2000 random numbers, the following results show that the advantage of fast sorting is very large.

Quicksort 0:00:00. 062000
Bubblesort 0:00:03. 563000

 

5.CodeDownload

Http://files.cnblogs.com/coderzh/Code/sorttest.rar

 

 

Python daily delicious series (total)

Python daily delicious (28)-urlopen

Python daily delicious (29)-call the dynamic link library (DLL) of VC ++)

Python daily delicious (30)-Fast sorting of Python data structures and algorithms

Python daily delicious (31)-insert sorting of Python data structures and algorithms

Python daily delicious (32)-Python data structure and algorithm heap 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.