Sorting Algorithm learning, python implementation

Source: Internet
Author: User

Several Basic sorting methods were implemented using python on Saturday and weekend. The code was directly written. This article does not focus on the basic knowledge, but only provides a rough description. If you are not clear about the concept, you can find your own materials. Insert sort directly: each time you extract the first element from the unordered table, insert it to the appropriate position of the ordered table, so that the ordered table is still ordered. 1 def insert (arr): 2 l = len (arr) 3 for I in range (1, l): 4 if arr [I] <arr [I-1]: 5 temp = arr [I] 6 j = I-1 7 arr [j + 1] = arr [j] 8 j = J-1 9 while j> = 0 and arr [j]> temp: 10 arr [j + 1] = arr [j] # element move-back 11 j = j-112 arr [j + 1] = temp13 return arr14 arra = [98,36,-9,0, 47,23, ,] 15 print insert (arra) Semi-insert sorting: semi-insert sorting is to directly insert the above into the sorting to search for ordered sub-tables using semi-query, after determining the position to be inserted, you can move the elements backward. 1 #-*-coding: cp936-*-2 def find_ff (arr, x): # Half lookup 3 size = len (arr) 4 low = 0 5 high = size-1 6 while low <= high: 7 mid = (low + high)/2 8 if arr [low] = x: 9 return low10 elif arr [high] = x: 11 return high12 elif arr [mid] = x: 13 return mid14 elif arr [mid]> x: 15 high = mid-116 else: 17 low = mid + 118 19 def insert_ff (arr): # Ascending 20 l = len (arr) 21 for I in range (1, l): 22 if arr [I] <arr [I-1]: 23 temp = arr [I] 24 j = I -125 low = 026 high = j27 while low <= high: 28 mid = (low + high)/229 print 'mid ', mid30 if arr [mid]> temp: 31 high = mid-132 else: 33 low = mid + 134 print 'high + 1', high + 135 print 'J ', j36 if j = 0: 37 arr [j + 1], arr [j] = arr [j], arr [j + 1] 38 else: 39 print 'Action 2' 40 for x in range (j, high,-1): # Move element 41 print 'x ', x42 arr [x + 1] = arr [x] 43 arr [high + 1] = temp44 print arr45 return arr46 47 arra = [98,36,-9,0, 47,23, 1,8, 10, 7] 4 8 print insert_ff (arra) the index value of the element must be clear when analyzing the semi-inserted sorting. Therefore, I added the print statement to the test, which can be ignored. Hill sort 1 def shell (arr): 2 l = len (arr) 3 h = l/2 4 while h> = 1: 5 for I in range (l ): 6 j = I 7 while j> = h and arr [j] <arr [j-h]: 8 arr [j], arr [j-h] = arr [j-h], arr [j] 9 h = h/2 10 return arr11 arra = [98,36,-9,0, 47,23, 1,8, 10, 7] 12 print shell (arra) Exchange sorting: bubble sorting and Quick Sort bubble sorting are relatively simple #-*-coding: cp936-*-def bubble (arr ): # descending order size = len (arr) for I in range (size-1): for j in range (I, size-1 ): if arr [j + 1]> arr [j]: arr [j + 1], Arr [j] = arr [j], arr [j + 1] return arr arra = [98,36,-9,0, 47,23,] print bubble (arra) quick sorting: an Improvement of Bubble Sorting is based on the principle of division and governance 1 #-*-coding: cp936-*-2 def partition (data, low, high ): 3 if low

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.