冒泡選擇插入希爾歸併快速排序等python實現

來源:互聯網
上載者:User

標籤:exchange   incr   location   選擇   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_list):    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 range(len(a_list) - 1, 0, -1):        pos_of_max = 0        for location in range(1, fill_sort + 1):            if a_list[location] > 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] > current_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 increments 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("Splitting ", a_list)    if len(a_list) > 1:        mid = len(a_list) // 2        left_half = a_list[:mid]        right_half = a_list[mid:]        merge_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        while i < len(left_half):            a_list[k] = left_half[i]            k += 1            i += 1        while j < len(right_half):            a_list[k] = right_half[j]            k += 1            j += 1        print("Merging ", a_list)def quick_sort(a_list):    quick_sort_helper(a_list, 0, len(a_list) - 1)def quick_sort_helper(a_list, first, last):    if first < 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    done = 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_mark], a_list[first]    return right_marka_list = [54, 26, 93, 17, 77, 31, 44, 55, 20]quick_sort(a_list)print(a_list)

  

冒泡選擇插入希爾歸併快速排序等python實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.