Python无法让这个排序算法模拟器工作

2024-09-30 02:26:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想看看Python中不同排序算法的速度差异,所以我尝试制作一个图形程序。当我这样做时,我试图通过模拟循环中的所有函数来节省空间,但我无法让它工作。我对编程相当陌生,希望能得到一些帮助

以下是我编写的代码:

import random
from time import perf_counter
import matplotlib.pyplot as plt

def create_list(n,max):
    listan=[]
    for i in range (0,n,1):
        listan.append(random.randint(0,100))
    return listan


#finding pos with lowest value
def find_lowest_value(start_pos,lista):
    lowest_value = lista[start_pos]
    lowest_value_pos = start_pos
    for i in range (start_pos+1,len(lista),1):
        if lowest_value > lista[i]:
            lowest_value_pos = i
            lowest_value = lista[i]
    return lowest_value_pos 
            
######################
# algoritmer
######################

# bubble sort
def bubble_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Bubble Sort')
    bubble_lista = lista.copy()
    osorterad = True
    while osorterad:
        osorterad = False
        for i in range(len(bubble_lista) - 1):
            if bubble_lista[i] > bubble_lista[i+1]:
                bubble_lista[i], bubble_lista[i+1] = bubble_lista[i+1], bubble_lista[i]
                osorterad = True       
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return bubble_lista

# selection sort
def selection_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Selection Sort')
    selection_lista = lista.copy()
    for i in range(len(selection_lista) - 1):
        low_pos = find_lowest_value(i, selection_lista)
        if not i == low_pos:
            selection_lista[i], selection_lista[low_pos] = selection_lista[low_pos], selection_lista[i]
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return selection_lista

# insert sort
def insert_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Insertion Sort')
    insert_lista = lista.copy()
    for i in range(1, len(insert_lista)):
        nyckel = insert_lista[i]
        j = i - 1
        while j >= 0 and nyckel < insert_lista[j]:
            insert_lista[j + 1] = insert_lista[j] 
            j = j - 1 
        insert_lista[j + 1] = nyckel
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return insert_lista

def python_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Python Sort')
    
    python_lista = sorted(lista)
    
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return python_lista

##################
# grafdelen
#################
sim_range = 20
repetitioner = 20

lista = []

funktioner = [bubble_sort, selection_sort, insert_sort, python_sort]
x1, x2, x3, x4 = [], [], [], []
y1, y2, y3, y4 = [], [], [], []
x = [x1, x2, x3, x4]
y = [y1, y2, y3, y4]

for i in range(len(funktioner)):
    aktuell_funktion = funktioner[i]
    aktuellt_x = x[i]
    aktuellt_y = y[i]
    intervall = 0
    for i in range(sim_range):
        intervall += 1000
        aktuellt_x.append(intervall)
        tidtotal = 0
        for i in range(repetitioner):
            create_list(intervall, intervall)
            aktuell_funktion(lista)
            tidtotal += tidtog
        tidmedel = tidtotal / repetitioner
        aktuellt_y.append(tidmedel)
        
def plot():
    plt.xlabel('intervall')
    plt.ylabel('tid')
    plt.plot(x1, y1)
    plt.plot(x2, y2)   
    plt.plot(x3, y3)
    plt.plot(x4, y4)    

plot()

Tags: inposforvaluecounterrangesortperf
1条回答
网友
1楼 · 发布于 2024-09-30 02:26:36

多谢各位。问题确实是我没有正确地使用create_list()。以下是工作代码:

import random
from time import perf_counter
import matplotlib.pyplot as plt

def create_list(n,max):
    listan=[]
    for i in range (0,n,1):
        listan.append(random.randint(0,max))
    return listan


#finding pos with lowest value
def find_lowest_value(start_pos,lista):
    lowest_value = lista[start_pos]
    lowest_value_pos = start_pos
    for i in range (start_pos+1,len(lista),1):
        if lowest_value > lista[i]:
            lowest_value_pos = i
            lowest_value = lista[i]
    return lowest_value_pos 
            
######################
# algoritmer
######################

# bubble sort
def bubble_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Bubble Sort')
    bubble_lista = lista.copy()
    osorterad = True
    while osorterad:
        osorterad = False
        for i in range(len(bubble_lista) - 1):
            if bubble_lista[i] > bubble_lista[i+1]:
                bubble_lista[i], bubble_lista[i+1] = bubble_lista[i+1], bubble_lista[i]
                osorterad = True       
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return bubble_lista

# selection sort
def selection_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Selection Sort')
    selection_lista = lista.copy()
    for i in range(len(selection_lista) - 1):
        low_pos = find_lowest_value(i, selection_lista)
        if not i == low_pos:
            selection_lista[i], selection_lista[low_pos] = selection_lista[low_pos], selection_lista[i]
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return selection_lista

# insert sort
def insert_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Insertion Sort')
    insert_lista = lista.copy()
    for i in range(1, len(insert_lista)):
        nyckel = insert_lista[i]
        j = i - 1
        while j >= 0 and nyckel < insert_lista[j]:
            insert_lista[j + 1] = insert_lista[j] 
            j = j - 1 
        insert_lista[j + 1] = nyckel
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return insert_lista

def python_sort(lista):
    global tidtog
    tidstart = perf_counter()
    print('Python Sort')
    
    python_lista = sorted(lista)
    
    tidstopp = perf_counter()
    tidtog = round((tidstopp - tidstart) * 1000, 4)
    return python_lista

##################
# grafdelen
#################
sim_range = 20
repetitioner = 3

lista = []

funktioner = [bubble_sort, selection_sort, insert_sort, python_sort]
x1, x2, x3, x4 = [], [], [], []
y1, y2, y3, y4 = [], [], [], []
x = [x1, x2, x3, x4]
y = [y1, y2, y3, y4]

for i in range(len(funktioner)):
    aktuell_funktion = funktioner[i]
    aktuellt_x = x[i]
    aktuellt_y = y[i]
    intervall = 0
    for i in range(sim_range):
        intervall += 100
        aktuellt_x.append(intervall)
        tidtotal = 0
        for i in range(repetitioner):
            lista = create_list(intervall, intervall)
            aktuell_funktion(lista)
            tidtotal += tidtog
        tidmedel = tidtotal / repetitioner
        aktuellt_y.append(tidmedel)
        
def plot():
    plt.xlabel('intervall')
    plt.ylabel('tid')
    plt.plot(x1, y1, label = "Bubble")
    plt.plot(x2, y2, label = "Selection")   
    plt.plot(x3, y3, label = "Insertion")
    plt.plot(x4, y4, label = "Python Default") 
    plt.legend()

plot()

相关问题 更多 >

    热门问题