Python Sorting Algorithm: Fast sorting and Bubble sorting, python Bubble Sorting

Source: Internet
Author: User

Python Sorting Algorithm: Fast sorting and Bubble sorting, python Bubble Sorting

Python Sorting Algorithm: Fast sorting and Bubble Sorting

Reprinted please indicate Source Address: http://www.cnblogs.com/funnyzpc/p/7828610.html

This line has been in the trap (IT for short) for some years, but I have hardly written the Sorting Algorithm since my teacher introduced the sorting algorithm. Of course, this is no problem, in actual sorting, most data is sorted in the database before it is obtained from the database. Of course, this sorting is in the SQL field, if you really need to sort in the Code, there is also a corresponding tool class for processing, for example, there are JavaArray. sort ()Array (Array type), although there are restrictions on the function, but it also saves the time of the escape code, well, the good thing you don't need to write is to reduce the development time, of course, it also increases the time for coffee and online shopping ~

 

 

The following code is easy to understand:

(FromWikipedia, Including the Bubble Sorting mentioned in this section.Wikipedia)

Well, it's cool time. Let me give you a rough idea.Quick sorting:

A> first, take A number (usually the first number) as the reference value.

B> divide the array to be sorted into two sides, pointing to both sides of the array from left to right

C> the left and right sides are moved to the center, and the value is greater than the reference value to the right of the reference value (if the value on the right is moved to the position, the value does not move ), the same is true on the left.

D> sort the values on both sides of the benchmark position (typically recursive call)

Well, the above is probably half the process of fast sorting. If you do not understand it, we suggest following the code to speculate on the whole process of fast sorting, which is not difficult:

1 #! /Usr/bin/env Python 2 # Quick sorting 3 def quickSort (arr, _ l, _ r): 4 l, r, t = _ l, _ r, 0 5 if l <= r: 6 t = arr [_ l] # benchmark parameter 7 while (l <r): 8 # When the value on the right is greater than the benchmark parameter, the right value moves one to the left (position and value) 9 while l <r and arr [r]> = t: 10 r = r-111 arr [l] = arr [r] # position of the right value to the left value 12 # When the left value is smaller than the reference parameter, the right value moves one to the left (position and value) 13 while l <r and arr [l] <= t: 14 l = l + 115 arr [r] = arr [l] # Place the left value in the position of the right value 16 ''' 17. After the current round of sorting, place the benchmark parameter in 18. values on the left and right sides are sorted (recursively called) 19 ''' 20 arr [r] = t # normalization of the benchmark parameter 21 22 quickSort (arr, _ l L-1) # sorting of 23 quickSort (arr, l + 1, _ r) # Sort data 24 25 I = [11,100, 11.1, 21] 26 print ("Before sorting") 27 print (I) 28 print ("after sorting") 29 quickSort (I, 0, len (I)-1) 30 print (I) 31 32 ============= below is the console output ============= 33 34 Before sorting 35 [23, 1, 6, 77, 8,-11,100, 11.1, 99, 24, 21] After 36 sorting, 37 [-11, 1, 6, 8, 11.1, 21, 23, 24, 77, 99,100]

 

Hmm ~, After the quick sorting is completed, the dynamic chart of the Bubble Sorting is displayed first, and those with dense phobias do not enter:

 

Combined with graphs,Bubble SortingThe process is roughly like this:

A> compare A value (usually the first value) in the array to be sorted as the reference value with all other values.

B> directly skipping the value above the benchmark value, and exchanging the location with the benchmark value smaller than the benchmark value

Amount ~, It's better to use code to speak ~

1 #! /Usr/bin/env Python 2 3 # bubble sort method 4 def bubleSort (arr): 5 for k in range (len (arr )): 6 temp = arr [k] # temporary value for exchanging 7 for j in range (k + 1, len (arr )): 8 ''' 9 if the value is smaller than the benchmark value, the benchmark value is exchanged with the current value for location 10''' 11 if arr [j] <arr [k]: 12 temp = arr [k] 13 arr [k] = arr [j] 14 arr [j] = temp15 continue; # No 16 17 I = [, 8, -11,100, 11.1, 21] 18 19 print ("Before sorting") 20 print (I) 21 print ("after sorting") 22 bubleSort (I) 23 print (I) 24 25 =========== console output =============== 26 Before sorting 27 [23, 1, 6, 77, 8,-11,100, 11.1, 99, 24, 21] 28 after sorting 29 [-11, 1, 6, 8, 11.1, 21, 23, 24, 77, 99,100]

 

Since it is Python, of course, Python also has a built-in one-click Sorting Algorithm for Arrays:

1 ii = [11,100, 11.1, 8,-, 21] 2 ii.Sort ()# Array built-in sort method sorting 3 print ("ii", ii) 4 ij = [11,100, 11.1, 8,-, 21] 5 ij =Sorted (ij)# Sort external methods (equivalent to the tool class) 6 print ("ij", ij)

Since Python already provides the sorting method, it's not sad to be busy.

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.