Bubble Select Insert Hill Merge fast sort Python implementation

Source: Internet
Author: User

Tag:exchange   incr   location    selection    art    pytho   list   cat   shel   

def bubble_sort (a_list): For Pass_num in range (len (a_list)-1, 0,-1): For I in Range (Pass_num): if A_list[i] > a_list[i + 1]: A_list[i], a_list[i + 1] = a_list[i + 1], a_list[i]def short_bubble_sort (a_li        ST): exchanges = True Pass_num = Len (a_list)-1 while pass_num > 0 and exchanges:exchanges = False                For I in Range (pass_num): if a_list[i] > a_list[i + 1]: exchanges = True A_list[i], a_list[i + 1] = a_list[i + 1], a_list[i] pass_num-= 1def Selection_sort (a_list): for Fill_sort in RA Nge (Len (a_list)-1, 0,-1): Pos_of_max = 0 for location in range (1, Fill_sort + 1): If A_list[lo                Cation] > A_list[pos_of_max]: Pos_of_max = Location if fill_sort! = Pos_of_max:  A_list[fill_sort], A_list[pos_of_max] = A_list[pos_of_max], a_list[fill_sort]def insertion_sort (a_list): For index in Range (1, Len (a_list)):       Current_value = A_list[index] Position = index while position > 0 and a_list[position-1] > CU Rrent_value:a_list[position] = a_list[position-1] Position-= 1 A_list[position] = current  _valuedef Shell_sort (a_list): Sublist_count = Len (a_list)//2 while Sublist_count > 0:for start_position In range (Sublist_count): Gap_insertion_sort (A_list, start_position, sublist_count) print ("After Increme NTS of size ", Sublist_count," The list is ", a_list) Sublist_count = sublist_count//2def Gap_insertion_sort (a_list        , start, GAP): For I in range (start + Gap, Len (a_list), gap): Current_value = a_list[i] Position = i  While position >= Gap and A_list[position-gap] > current_value:a_list[position] = a_list[position- Gap] Position = position-gap A_list[position] = current_valuedef merge_sort (a_list): Print ("Splitt ing ", a_list) If Len(a_list) > 1:mid = Len (a_list)//2 left_half = A_list[:mid] Right_half = a_list[mid:] Mer  Ge_sort (left_half) merge_sort (right_half) i = 0 J = 0 k = 0 While I < Len (left_half)                and J < Len (right_half): if Left_half[i] < Right_half[j]: a_list[k] = left_half[i] i + = 1 Else:a_list[k] = Right_half[j] J + = 1 K + = 1 W Hile i < Len (left_half): a_list[k] = left_half[i] K + = 1 i + = 1 while J < le N (right_half): a_list[k] = right_half[j] K + = 1 J + = 1 print ("Merging", A_list) de F Quick_sort (A_list): Quick_sort_helper (a_list, 0, Len (a_list)-1) def quick_sort_helper (A_list, First, last): If fi        RST < Last:split_point = Partition (A_list, first, last) Quick_sort_helper (a_list, first, Split_point) Quick_sort_helper (A_list, Split_point + 1, last) def partition (A_list, First, last): Pivot_value = A_list[first] Left_mark = first + 1  Right_mark = Last do = False while not done:while Left_mark <= Right_mark and A_list[left_mark] <=            Pivot_value:left_mark + = 1 while Right_mark >= Left_mark and A_list[right_mark] >= pivot_value: Right_mark-= 1 if right_mark < Left_mark:done = True Else:a_list[left _mark], A_list[right_mark] = A_list[right_mark], A_list[left_mark] a_list[first], a_list[right_mark] = A_list[right_ma RK], A_list[first] return right_marka_list = [Wu, si, 20]quick_sort, si, si, I, A, a_list) print (a_list)

  

Bubble Select Insert Hill Merge fast sort Python implementation

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.