108 lines
3.3 KiB
Python
108 lines
3.3 KiB
Python
import threading
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import time
|
|
import copy
|
|
import random
|
|
|
|
|
|
# O algoritmo de ordenação do usuário
|
|
def send_to_goulag(input_list):
|
|
idx = 0
|
|
temp = []
|
|
while True:
|
|
# Loop interno para "filtrar" elementos maiores que o próximo
|
|
while idx < len(input_list) - 1:
|
|
if input_list[idx] > input_list[idx + 1]:
|
|
temp.append(input_list[idx + 1])
|
|
# pop(idx+1) é uma operação O(N) no pior caso
|
|
input_list.pop(idx + 1)
|
|
else:
|
|
idx += 1
|
|
|
|
# Chamada recursiva (pode adicionar complexidade se for muito frequente)
|
|
if len(temp) > 1:
|
|
temp = send_to_goulag(temp)
|
|
|
|
idx = 0
|
|
# Loop para reinserir elementos de 'temp' na lista principal
|
|
while idx < len(input_list):
|
|
if temp and input_list[idx] > temp[0]:
|
|
# Inserção no meio da lista input_list[idx:idx] = temp é O(N) no pior caso
|
|
input_list[idx:idx] = temp
|
|
temp.clear()
|
|
break
|
|
idx += 1
|
|
if len(temp) == 0:
|
|
return input_list
|
|
|
|
if temp:
|
|
input_list.extend(temp) # extend é O(k) onde k é o tamanho de temp
|
|
temp.clear()
|
|
|
|
|
|
|
|
|
|
|
|
def create_unique_random_list(size):
|
|
return random.sample(range(5120000 * 2), size)
|
|
|
|
if __name__ == "__main__":
|
|
list1 = create_unique_random_list(1000)
|
|
list3 = create_unique_random_list(4000)
|
|
list2 = create_unique_random_list(2000)
|
|
list4 = create_unique_random_list(8000)
|
|
list5 = create_unique_random_list(16000)
|
|
list6 = create_unique_random_list(32000)
|
|
list7 = create_unique_random_list(64000)
|
|
# list8 = create_unique_random_list(12800)
|
|
# list9 = create_unique_random_list(25600)
|
|
# list10 = create_unique_random_list(51200)
|
|
|
|
list1_copy = copy.deepcopy(list1)
|
|
list2_copy = copy.deepcopy(list2)
|
|
list3_copy = copy.deepcopy(list3)
|
|
list4_copy = copy.deepcopy(list4)
|
|
list5_copy = copy.deepcopy(list5)
|
|
list6_copy = copy.deepcopy(list6)
|
|
list7_copy = copy.deepcopy(list7)
|
|
# list8_copy = copy.deepcopy(list8)
|
|
# list9_copy = copy.deepcopy(list9)
|
|
# list10_copy = copy.deepcopy(list10)
|
|
|
|
list1_copy.sort()
|
|
list2_copy.sort()
|
|
list3_copy.sort()
|
|
list4_copy.sort()
|
|
list5_copy.sort()
|
|
list6_copy.sort()
|
|
list7_copy.sort()
|
|
# list8_copy.sort()
|
|
# list9_copy.sort()
|
|
# list10_copy.sort()
|
|
lists_to_sort = [list1, list2, list3, list4, list5, list6, list7]
|
|
|
|
threads = []
|
|
|
|
# Inicia uma thread para cada chamada do send_to_goulag
|
|
for i, current_list in enumerate(lists_to_sort):
|
|
# A thread chamará send_to_goulag que modificará current_list in-place
|
|
thread = threading.Thread(target=send_to_goulag, args=(current_list,))
|
|
threads.append(thread)
|
|
thread.start()
|
|
|
|
# Espera que todas as threads terminem antes de verificar os resultados
|
|
for thread in threads:
|
|
thread.join()
|
|
print(list1 == list1_copy)
|
|
print(list2 == list2_copy)
|
|
print(list3 == list3_copy)
|
|
print(list4 == list4_copy)
|
|
print(list5 == list5_copy)
|
|
print(list6 == list6_copy)
|
|
print(list7 == list7_copy)
|
|
# print(list8 == list8_copy)
|
|
# print(list9 == list9_copy)
|
|
# print(list10 == list10_copy)
|