日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

排序算法 - 6种 - 超炫的动画演示 - Python实现

發布時間:2024/7/5 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 排序算法 - 6种 - 超炫的动画演示 - Python实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.冒泡排序

思路:遍歷列表,每一輪每次比較相鄰兩項,將無序的兩項交換,下一輪遍歷比前一輪比較次數減1。

  • def bubble_sort(a_list):
  • for passnum in range(len(a_list)-1, 0, -1):
  • for i in range(passnum):
  • 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
  • passnum = len(a_list) - 1
  • while passnum > 0 and exchanges:
  • exchanges = False
  • for i in range(passnum):
  • if a_list[i] > a_list[i+1]:
  • exchanges = True
  • a_list[i], a_list[i+1] = a_list[i+1], a_list[i]
  • passnum = passnum - 1
  • 動畫演示:

    2.選擇排序

    思路:遍歷列表,每次遍歷選出最大的放在合適的位置。

  • def selection_sort(a_list):
  • for fillslot in range(len(a_list)-1, 0, -1):
  • position_max=0
  • for location in range(1, fillslot+1):
  • if a_list[location] > a_list[position_max]:
  • position_max = location
  • a_list[fillslot], a_list[position_max] = a_list[position_max], a_list[fillslot]
  • 動畫演示:

    3.插入排序

    思路:每一步都將待插入的數據按大小插入到已排序的數據中的合適位置。

  • def insertion_sort(a_list):
  • for index in range(1, len(a_list)):
  • cur_value = a_list[index]
  • pos = index
  • while pos > 0 and a_list[pos-1] > cur_value:
  • a_list[pos] = a_list[pos-1]
  • pos = pos - 1
  • a_list[pos] = cur_value
  • 動畫演示:

    4.希爾排序

    思路:分解為多個子列表進行插入排序,不是連續分,而是通過增量。

  • def shell_sort(a_list):
  • sublist_count = len(a_list)
  • while sublist_count > 0:
  • for start_position in range(sublist_count):
  • gap_insertion_sort(a_list, start_position, sublist_count)
  • sublistcount = sublistcount // 2
  • def 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_value
  • 5.歸并排序

    思路:分而治之,不斷拆分為一半,直至項數為0或1,然后排序合并。需要額外空間。

  • def merge_sort(a_list):
  • if len(a_list) > 1:
  • mid = len(a_list) // 2
  • lefthalf = a_list[:mid]
  • righthalf = a_list[mid:]
  • merge_sort(lefthalf)
  • merge_sort(righthalf)
  • i=0
  • j=0
  • k=0
  • while i < len(lefthalf) and j < len(righthalf):
  • if lefthalf[i] < righthalf[j]:
  • a_list[k] = lefthalf[i]
  • i = i + 1
  • else:
  • a_list[k] = righthalf[j]
  • j = j + 1
  • k=k+1
  • while i < len(lefthalf):
  • a_list[k] = lefthalf[i]
  • i = i + 1
  • k = k + 1
  • while j < len(righthalf):
  • a_list[k] = righthalf[j]
  • j = j + 1
  • k = k + 1
  • 動畫演示:

    6.快速排序

    思路:與歸并一樣使用分而治之,不使用額外內存,特點是樞軸值。

  • 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:
  • splitpoint = partition(a_list, first, last)
  • quick_sort_helper(a_list, first, splitpoint-1)
  • quick_sort_helper(a_list, splitpoint+1, last)
  • def partition(a_list, first, last):
  • pivotvalue = a_list[first]
  • leftmark = first + 1
  • rightmark = last
  • done = False
  • while not done:
  • while leftmark <= rightmark and a_list[leftmark] <= pivotvalue:
  • leftmark = leftmark + 1
  • while a_list[rightmark] >= pivotvalue and rightmark >= leftmark:
  • rightmark = rightmark -1
  • if rightmark < leftmark:
  • done = True
  • else:
  • a_list[leftmark], a_list[rightmark] = a_list[rightmark], a_list[leftmark]
  • a_list[first], a_list[rightmark] = a_list[rightmark], a_list[first]
  • 動畫演示:

    說明:

    1.參考https://interactivepython.org/runestone/static/pythonds/SortSearch/toctree.html

    2.動畫來自https://visualgo.net/en?截圖

    總結

    以上是生活随笔為你收集整理的排序算法 - 6种 - 超炫的动画演示 - Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。