Compare commits

...

1 Commits

Author SHA1 Message Date
Gustavo Henrique Santos Souza de Miranda 82956d484e Modified To add the list merge system 2025-05-23 02:30:20 -03:00
1 changed files with 48 additions and 10 deletions

View File

@ -47,11 +47,17 @@ def send_to_goulag(input_list:list)-> list :
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])
@ -66,16 +72,48 @@ def send_to_goulag(input_list:list)-> list :
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
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()
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):
if list1[ptr1] > list2[ptr2]:
list1.insert(ptr1, list2[ptr2])
ptr1 += 1
list2.pop(ptr2)
else:
ptr1 += 1
if list2:
list1.extend(list2)
list2.clear() # Esvazia list2
return list1