Reimplemented the main logic using a tip by Gbowne
This commit is contained in:
parent
82956d484e
commit
100c7b12ee
|
|
@ -1,119 +1,71 @@
|
||||||
def send_to_goulag(input_list:list)-> list :
|
def send_to_goulag(main_list: list) -> list:
|
||||||
"""Sorts a list of numbers using the unique "Goulag Sort" method.
|
if not main_list:
|
||||||
|
return [] # Se a lista de entrada está vazia, retorna vazio
|
||||||
|
|
||||||
This algorithm processes a list of numbers through a distinctive iterative and
|
# Se a lista tem 1 ou 0 elementos, ela já está ordenada
|
||||||
recursive procedure, inspired by the "Stalin Sort" concept. Here's how it operates:
|
if len(main_list) <= 1:
|
||||||
|
return main_list
|
||||||
|
|
||||||
1. **Iterative Filtering:** It iterates through the main list. If an element
|
filtered_list = []
|
||||||
is found to be greater than its subsequent element, the subsequent element
|
gulag_list = []
|
||||||
is moved to a temporary holding list (`temp`). The main list then continues
|
|
||||||
with the remaining elements.
|
|
||||||
|
|
||||||
2. **Recursive Processing of Held Items:** If the `temp` list contains more than
|
# O primeiro elemento sempre vai para a filtered_list como ponto de partida
|
||||||
one item, the `send_to_goulag` algorithm recursively processes `temp`. This
|
filtered_list.append(main_list[0])
|
||||||
filtering and holding process continues until the sub-list contains a single
|
|
||||||
item or becomes empty.
|
|
||||||
|
|
||||||
3. **Partial Re-insertion:** After the recursive calls (or if `temp` was empty/single-item),
|
# Percorre o restante da lista original (a partir do segundo elemento)
|
||||||
the algorithm attempts to re-insert the elements from `temp` back into the main list.
|
# com um único ponteiro 'i'
|
||||||
This re-insertion is performed by finding a suitable position where the held item
|
for i in range(1, len(main_list)):
|
||||||
is less than the current element in the main list, maintaining a partial order.
|
current_item = main_list[i]
|
||||||
|
last_in_filtered = filtered_list[-1] # Pega o último item já colocado na filtered_list
|
||||||
|
|
||||||
4. **Final Appending:** Any remaining elements in `temp` that were not re-inserted
|
if current_item >= last_in_filtered:
|
||||||
into the middle of the list are then appended to the end of the main list.
|
# Se o item atual mantém a ordem com o que já foi filtrado, adicione-o
|
||||||
|
filtered_list.append(current_item)
|
||||||
|
else:
|
||||||
|
# Se o item atual quebra a ordem, ele é "rejeitado" para o gulag
|
||||||
|
gulag_list.append(current_item)
|
||||||
|
|
||||||
For a more detailed step-by-step explanation of the algorithm's flow, please refer to:
|
if len(gulag_list) > 1:
|
||||||
:ref:`goulag_sort_detailed_explanation`.
|
gulag_list = send_to_goulag(gulag_list)
|
||||||
|
|
||||||
|
filtered_list = merge_sorted_lists_efficient(filtered_list,gulag_list)
|
||||||
|
|
||||||
|
return filtered_list
|
||||||
|
|
||||||
|
|
||||||
|
def merge_sorted_lists_efficient(list1: list, list2: list) -> list:
|
||||||
|
"""
|
||||||
|
Mescla duas listas ordenadas de forma eficiente, criando uma nova lista resultado.
|
||||||
|
Evita as operações custosas de insert/pop no meio das listas.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
input_list (list): The list of numbers to be processed and sorted.
|
list1: A primeira lista ordenada.
|
||||||
This list will be modified **in-place**.
|
list2: A segunda lista ordenada.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list: The list after being processed by the Goulag Sort algorithm. The result
|
Uma NOVA lista contendo todos os elementos de list1 e list2 mesclados e ordenados.
|
||||||
is an ordered list.
|
|
||||||
|
|
||||||
Examples:
|
|
||||||
>>> from goulag_sort import send_to_goulag
|
|
||||||
>>> my_list = [5, 2, 8, 1, 9, 4]
|
|
||||||
>>> send_to_goulag(my_list)
|
|
||||||
[1, 2, 4, 5, 8, 9]
|
|
||||||
|
|
||||||
>>> empty_list = []
|
|
||||||
>>> send_to_goulag(empty_list)
|
|
||||||
[]
|
|
||||||
"""
|
"""
|
||||||
|
merged_list = []
|
||||||
|
ptr1 = 0 # Ponteiro para list1
|
||||||
|
ptr2 = 0 # Ponteiro para list2
|
||||||
|
|
||||||
|
# Enquanto houver elementos em ambas as listas para comparar
|
||||||
idx = 0
|
|
||||||
temp = []
|
|
||||||
is_sorted = True
|
|
||||||
while True:
|
|
||||||
|
|
||||||
if not input_list:
|
|
||||||
return []
|
|
||||||
|
|
||||||
if len(input_list)<=1:
|
|
||||||
return input_list
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
merge_two_sorted_lists_in_place(input_list,temp)
|
|
||||||
|
|
||||||
if temp:
|
|
||||||
input_list.extend(temp) # extend é O(k) onde k é o tamanho de temp
|
|
||||||
temp.clear()
|
|
||||||
|
|
||||||
for i in range(len(input_list) - 1):
|
|
||||||
if input_list[i] > input_list[i + 1]:
|
|
||||||
is_sorted = False
|
|
||||||
break
|
|
||||||
if is_sorted:
|
|
||||||
return input_list
|
|
||||||
|
|
||||||
def merge_two_sorted_lists_in_place(list1: list, list2: list) -> list:
|
|
||||||
|
|
||||||
if not list2:
|
|
||||||
return list1
|
|
||||||
if not list1:
|
|
||||||
list1.extend(list2)
|
|
||||||
list2.clear()
|
|
||||||
return list1
|
|
||||||
|
|
||||||
|
|
||||||
ptr1 = 0
|
|
||||||
ptr2 = 0
|
|
||||||
|
|
||||||
|
|
||||||
while ptr1 < len(list1) and ptr2 < len(list2):
|
while ptr1 < len(list1) and ptr2 < len(list2):
|
||||||
if list1[ptr1] > list2[ptr2]:
|
if list1[ptr1] <= list2[ptr2]: # Note o <= para estabilidade (mantém ordem de iguais)
|
||||||
|
merged_list.append(list1[ptr1])
|
||||||
list1.insert(ptr1, list2[ptr2])
|
|
||||||
|
|
||||||
ptr1 += 1
|
ptr1 += 1
|
||||||
|
|
||||||
list2.pop(ptr2)
|
|
||||||
else:
|
else:
|
||||||
|
merged_list.append(list2[ptr2])
|
||||||
|
ptr2 += 1
|
||||||
|
|
||||||
ptr1 += 1
|
# Adicionar quaisquer elementos restantes de list1 (se houver)
|
||||||
|
while ptr1 < len(list1):
|
||||||
|
merged_list.append(list1[ptr1])
|
||||||
|
ptr1 += 1
|
||||||
|
|
||||||
|
# Adicionar quaisquer elementos restantes de list2 (se houver)
|
||||||
|
while ptr2 < len(list2):
|
||||||
|
merged_list.append(list2[ptr2])
|
||||||
|
ptr2 += 1
|
||||||
|
|
||||||
if list2:
|
return merged_list
|
||||||
list1.extend(list2)
|
|
||||||
list2.clear() # Esvazia list2
|
|
||||||
|
|
||||||
return list1
|
|
||||||
Loading…
Reference in New Issue