2020-11-24 算法 選擇排序算法 冒泡排序算法 快速排序算法 二分查找算法 去重算法

選擇排序算法

冒泡排序算法

原理:數字兩兩進行比較,將大的值移動到右側,繼續進行比較直到最大值移動到最右側,則完成一次循環,進入第二次循環,以此類推,直到全部數字排序完成。

import turtle
import time
myPen = turtle.Pen()

myPen.pensize(15)

List = [15, 60, 85, 165, 10, 70, 5]
dis = [0, 50, 100, 150, 200, 250, 300]


# myPen.penup()
# myPen.goto(dis[0], 0)
# myPen.pendown()
# myPen.left(90)
# myPen.forward(List[0])

def draw_line(x):
    myPen.goto(dis[x], 0)
    myPen.pendown()
    myPen.setheading(90)
    myPen.forward(List[x])
    myPen.penup()
# for i in range(len(List)):
#     draw_line(i)
# List = [3, 2, 4, 1]
count = 0
for j in range(len(List)-1):
    for i in range(len(List)-1-j):
        max = List[i]
        if List[i] > List[i+1]:
            List[i], List[i+1] = List[i+1], List[i]
        
        myPen.clear()
        for m in range(len(List)):
            if m == i or m == i+1:
                myPen.pencolor("green")
                draw_line(m)
            else:
                myPen.pencolor("blue")
                draw_line(m)
        time.sleep(1.5)
        
        # count += 1

print(List,count)

turtle.done()

快速排序算法

二分查找算法

去重算法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章