Six Classic sorting algorithms for python hands-on

Source: Internet
Author: User
Tags python list

After starting with python, I was shocked by its simple features and ease of use. However, there is a big difference between Python and C language. I am used to the C language and I am not used to using python.

It took about a week to get started with python. to familiarize yourself with and deepen your understanding of Python, I tried several classic sorting algorithms and strengthened my basic knowledge by the way. When I started writing the code, I found that I had a lot of problems with the code I wrote, and there were always various problems with the sorting results. It seems that I have not written these algorithms for a long time, I forgot some details. Where is my sweat...

 

Don't talk nonsense. Start to practice.

You need to specify a dataset before sorting. You can use a random number to generate the dataset:

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: randata. PY <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> import random <br/> ''' <br/> randomly generated 0 ~ Value Between 10000000 <br/> ''' <br/> def getrandata (Num ): <br/> A = [] <br/> I = 0 <br/> while I <num: <br/>. append (random. randint (0, 000) <br/> I + = 1 <br/> return a <br/> 

Bubble sort ):

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: bubble_sort.py <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> Import randata <br/> ''' <br/> algorithm idea: each time from the end to roll forward, the adjacent elements compared, small elements are switched to the front <br/> in the first round, the smallest elements are moved to the first position, and the second small element is moved to the second position, and so on <br/> ''' <br/> def bubblesort (a): <br/> L = Len () -2 <br/> I = 0 <br/> while I <L: <br/> J = L <br/> while j> = I: <br/> if (a [J + 1] <A [J]): <br/> A [J], A [J + 1] = A [J + 1], a [J] <br/> J-= 1 <br/> I + = 1 <br/> 

Insert sort ):

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: insert_sort.py <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> Import randata <br/> ''' <br/> the comment-out section is the normal Insertion Method of the C language array <br/> the uncommented section is improved using the insert and delete features of the python list <br/> ''' <br/> def insertsort (ARR): <br/> for I in range (1, Len (ARR )): <br/> ''' <br/> TMP = arr [I] <br/> J = I <br/> while j> 0 and TMP <arr [J-1]: <br/> arr [J] = arr [J-1] <br/> J-= 1 <br/> arr [J] = TMP <br/> ''' <br /> J = I <br/> while j> 0 and ARR [J-1]> arr [I]: <br/> J-= 1 <br/> arr. insert (J, arr [I]) <br/> arr. pop (I + 1) <br/> 

Shell sort ):

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: shell_sort.py <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> Import randata <br/> def shellsort (ARR): <br/> Dist = Len (ARR)/2 <br/> while Dist> 0: <br/> for I in range (Dist, Len (ARR )): <br/> TMP = arr [I] <br/> J = I <br/> while j> = DIST and TMP <arr [J-Dist]: <br/> arr [J] = arr [J-Dist] <br/> J-= DIST <br/> arr [J] = TMP <br/> Dist/= 2 <br/> 

The name of the hill sort originates from its inventor, Donald shell, which is one of the first algorithms to break through the secondary time barrier. However, it was not until several years after it was first discovered that it had been proved its subtemporal boundaries. It works by comparing elements with a certain interval. The distance used for comparison decreases with the algorithm until the last sorting of adjacent elements is compared.

 

Merge sort ):

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: merge_sort.py <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> Import randata <br/> ''' <br/> use the new allocated space to store the merged new list <br/> arr: original list (array) <br/> S: start point of the first space to be merged <br/> M: start point of the second space to be merged <br/> E: end Point of the second space to be merged <br/> ''' <br/> def mergewithnewspace (ARR, S, M, E): <br/> I, j = s, m <br/> T = 0 <br/> newarr = [] <br/> while I <m and j <= E: <br/> If (ARR [I] <arr [J]): <br/> newarr. append (ARR [I]) <br/> I + = 1 <br/> T + = 1 <br/> else: <br/> newarr. append (ARR [J]) <br/> J + = 1 <br/> T + = 1 <br/> If I> = m: <br/> T = 0 <br/> for I in range (S, j ): <br/> arr [I] = newarr [T] <br/> T + = 1 <br/> else: <br/> T = 0 <br/> for I in range (I, m): <br/> newarr. append (ARR [I]) <br/> for I in range (S, e + 1 ): <br/> arr [I] = newarr [T] <br/> T + = 1 <br/> Del newarr <br/> def mergepasswithnewspace (ARR, N, d): <br/> I = 0 <br/> while I <(n-d) And I <(n + 1-2 * D ): <br/> mergewithnewspace (ARR, I, I + D, I + 2 * D-1) <br/> I = I + 2 * D <br/> If I <n-d: <br/> mergewithnewspace (ARR, I, I + D, n-1) <br/> else: <br/> mergewithnewspace (ARR, I-2 * D, I, n-1) <br/> def mergesortwithnewspace (ARR ): <br/> d = 1 <br/> while D <Len (ARR): <br/> mergepasswithnewspace (ARR, Len (ARR), d) <br/> D * = 2 <br/> ''' <br/> the original list is used as the insert method instead of the merged list of storage space. <br/> storage <br/> arr: original list (array) <br/> S: start point of the first space to be merged <br/> M: start point of the second space to be merged <br/> E: the end point of the second part of the space to be merged <br/> is the normal Insertion Method of the C language array. <br/> The unannotated part is the insertion and delete feature Improvement <br/> ''' <br/> def mergewithoutnewspace (ARR, s, M, E): <br/> I, j = s, m <br/> while I <m and j <= E: <br/> If arr [I]> arr [J]: <br/> ''' <br/> TMP = arr [J] <br/> K = j <br/> while K> I: <br/> arr [k] = arr [k-1] <br/> K-= 1 <br/> arr [I] = TMP <br/> ''' <br /> arr. insert (I, arr [J]) <br/> arr. pop (J + 1) <br/> J + = 1 <br/> M + = 1 <br/> else: <br/> I + = 1 <br/> ''' <br/> arr: original list (array) <br/> N: array size <br/> D: interval size <br/> ''' <br/> def mergepasswithoutnewspace (ARR, N, d ): <br/> I = 0 <br/> while I <(n-d) And I <(n + 1-2 * D): <br/> mergewithoutnewspace (ARR, i, I + D, I + 2 * D-1) <br/> I = I + 2 * D <br/> If I <n-d: <br/> mergewithoutnewspace (ARR, I, I + D, n-1) <br/> else: <br/> mergewithoutnewspace (ARR, I-2 * D, I, n-1) <br/> def mergesortwithoutnewspace (ARR): <br/> d = 1 <br/> while D <Len (ARR): <br/> mergepasswithoutnewspace (ARR, len (ARR), d) <br/> D * = 2 <br/> 

In the Merge Sorting Algorithm, when merging two sorted tables, a new table whose size is equal to the sum of the two tables is created to store the results of the two tables, then copy the merged table back to the two consecutive tables. In addition, you can merge data by inserting and sorting data instead of allocating new storage results.

Merge allocated space. The time complexity is O (nlogn). insert and merge is used. The time complexity is O (n ^ 2 ), the insert and delete mechanisms of the python list are used here, which is much more efficient than the insert method of the entire array in C language (see the comments section.

 

Heap sorting of classic algorithms:

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: heap_sort.py <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> ''' <br/> big root heap: In a full binary tree, for any node, arr [I]> = arr [2 * I], arr [I]> = arr [2 * I + 1] <br/> small root heap: in a Complete Binary Tree, arr [I] <= arr [2 * I] is suitable for any node. arr [I] <= arr [2 * I + 1] <br/> ''' <br/> Import randata <br/> ''' <br/> assume that outside the vertex at the start position, the binary tree with the start position as the root is a large root heap <br/> downward adjust the node at the start position to the appropriate position, yes, this tree is restored to a large root heap <br/> ''' <br/> def adjust (ARR, start, size ): <br/> TMP = arr [start] <br/> J = 2 * Start + 1 <br/> while j <size: <br/> If j <size-1 and ARR [J] <arr [J + 1]: <br/> J + = 1 <br/> If TMP> = arr [J]: <br/> Break <br/> arr [start] = arr [J] <br/> Start = j <br/> J = 2 * j + 1 <br/> arr [start] = TMP <br/> ''' <br/> create a large root heap from the list of unordered elements <br/> ''' <br/> def buildheap (ARR): <br/> size = Len (ARR) <br/> for I in range (size/2-1,-1,-1 ): <br/> adjust (ARR, I, size) <br/> def heapsort (ARR): <br/> size = Len (ARR) <br/> buildheap (ARR) <br/> ''' <br/> after a large root heap is created, the first element is the largest element in the list and is exchanged with the last element, list size-1 <br/> Reset the list to a large heap, repeat this operation until the last element <br/> ''' <br/> for I in range (size-1, 0,-1 ): <br/> arr [I], arr [0] = arr [0], arr [I] <br/> adjust (ARR, 0, I) <br/>

The idea of heap sorting is to build on the big root heap and the small root heap. For detailed steps, refer to the online explanation and the source code above.

 

Quick sorting of classic algorithms:

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: quick_sort.py <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> Import randata <br/> Import sys <br/> ''' <br/> This function is used, select an intermediate size value at the last and most intermediate positions, and place it in the first position of the interval <br/> to effectively eliminate the worst case of pre-sorting <br/> ''' <br/> def median (, start, end): <br/> center = (start + end)/2 <br/> If a [start]> A [center]: <br/> A [start], a [center] = A [center], a [start] <br/> If a [start]> A [end]: <br/> A [start], a [end] = A [end], a [start] <br/> If a [center]> A [end]: <br/> A [center], a [end] = A [end], a [center] <br/> A [start], A [center] = A [center], a [start] <br/> def doswap (A, start, end): <br/> If Start> = end: <br/> return <br/> I, j = start, end <br/> median (A, start, end) <br/> TMP = A [start] <br/> while (true): <br/> while (A [J]> TMP and I <j ): <br/> J-= 1 <br/> If I <J: <br/> A [I] = A [J] <br/> I + = 1 <br/> while (A [I] <TMP and I <j ): <br/> I + = 1 <br/> If I <J: <br/> A [J] = A [I] <br/> J-= 1 <br/> else: <br/> Break <br/> A [I] = TMP <br/> doswap (A, start, I-1) <br/> doswap (A, J + 1, end) <br/> def quicksort (a): <br/> # sets the recursive depth to 10000000. If the amount of data is too large, an exception occurs when the maximum recursive depth is exceeded. <br/> sys. setrecursionlimit (1000000) <br/> doswap (A, 0, Len (a)-1) <br/> 

Here, when selecting the reference value, the quick sort algorithm selects the median, that is, the first, last, and most intermediate elements in the range, select an intermediate size as the reference value, and move this element to the first position. This algorithm can effectively eliminate the worst time complexity in fast sorting.

 

When an algorithm is written, there is always something to verify. Write a separate part to execute the algorithm, output the time spent by the algorithm, and output their execution results to the file, used to check whether the execution result is correct.

#-*-Coding: UTF-8-*-<br/> #! /Usr/bin/python <br/> # filename: sort. PY <br/> # Author: Boyce <br/> # Email: boyce.ywr@gmail.com <br/> Import time <br/> Import randata <br/> Import bubble_sort <br/> Import quick_sort <br/> Import heap_sort <br/> Import shell_sort <br/> Import merge_sort <br/> Import insert_sort <br/> filename = 'sort. dat '<br/> size = 10000 <br/> Print'/nstart generate randam data... '<br/> arr = randata. getrandata (size) <br/> Print 'data generation finished. '<br/> Print 'data size is % d, result will be save to file % s' % (size, filename) <br/> F = file (filename, 'W') <br/> F. write ("/noriginal data:/N") <br/> F. write (STR (ARR) <br/> # Use the timsort sorting algorithm built in python <br/> A = arr [:] <br/> Print '/nstart internal sort... '<br/> T1 = time. clock () <br/>. sort () <br/> T2 = time. clock () <br/> Print 'internal sort finisehd. time used = % FS '% (t2-t1) <br/> F. write ('/n/ninternal sort [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> A = arr [:] <br/> Print '/nstart quick sort... '<br/> T1 = time. clock () <br/> quick_sort.quicksort (a) <br/> T2 = time. clock () <br/> Print 'quick sort finished. time used = % FS '% (t2-t1) <br/> F. write ('/n/nquick sort [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> A = arr [:] <br/> Print '/nstart Heap Sort... '<br/> T1 = time. clock () <br/> heap_sort.heapsort (a) <br/> T2 = time. clock () <br/> Print 'heap sort finished. time used = % FS '% (t2-t1) <br/> F. write ('/n/nheap sort [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> A = arr [:] <br/> Print '/nstart shell sort... '<br/> T1 = time. clock () <br/> shell_sort.shellsort (a) <br/> T2 = time. clock () <br/> Print 'Shell sort finished. time used = % FS '% (t2-t1) <br/> F. write ('/n/nshell sort [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> A = arr [:] <br/> Print '/nstart merge sort with new space... '<br/> T1 = time. clock () <br/> merge_sort.mergesortwithnewspace (a) <br/> T2 = time. clock () <br/> Print 'merge sort with new space finished. time used = % FS '% (t2-t1) <br/> F. write ('/n/nmerge sort with new space [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> A = arr [:] <br/> Print '/nstart merge sort without new space... '<br/> T1 = time. clock () <br/> merge_sort.mergesortwithoutnewspace (a) <br/> T2 = time. clock () <br/> Print 'merge sort without new space finished. time used = % FS '% (t2-t1) <br/> F. write ('/n/nmerge sort without new space [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> A = arr [:] <br/> Print '/nstart insert sort... '<br/> T1 = time. clock () <br/> insert_sort.insertsort (a) <br/> T2 = time. clock () <br/> Print 'insert sort finished. time used = % FS '% (t2-t1) <br/> F. write ('/n/ninsert sort [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> A = arr [:] <br/> Print '/nstart bubble sort... '<br/> T1 = time. clock () <br/> bubble_sort.bubblesort (a) <br/> T2 = time. clock () <br/> Print 'bubble sort finished. time used = % FS '% (t2-t1) <br/> F. write ('/n/nbubble sort [time used = % fs]/N' % (t2-t1) <br/> F. write (STR (A) <br/> Del a <br/> F. close () <br/> 

 

This is the result of a certain execution:

 

The output result is saved in the sort. dat file in the current directory. Open it in notepad and you will see it.

(Look at the timsort sorting algorithm built in Python. It's not an order of magnitude at all. It's so fast ...)

 

This shows the content of the output file:

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.