Added a new way to reinsert the temporary list to the main list speeding up the process
This commit is contained in:
parent
72279ed09e
commit
b295ee775b
|
|
@ -19,24 +19,51 @@ def send_to_goulag(main_list:list)->list:
|
||||||
if not itens_to_be_reinserted and main_list == main_list_copy:
|
if not itens_to_be_reinserted and main_list == main_list_copy:
|
||||||
return main_list
|
return main_list
|
||||||
|
|
||||||
print(main_list)
|
|
||||||
|
|
||||||
if len(itens_to_be_reinserted)>1:
|
if len(itens_to_be_reinserted)>1:
|
||||||
itens_to_be_reinserted = send_to_goulag(itens_to_be_reinserted)
|
itens_to_be_reinserted = send_to_goulag(itens_to_be_reinserted)
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
while i < len(main_list) - 1:
|
merge_two_sorted_lists_in_place(main_list,itens_to_be_reinserted)
|
||||||
if itens_to_be_reinserted[0] < main_list[i]:
|
|
||||||
main_list[i:i] = itens_to_be_reinserted
|
|
||||||
itens_to_be_reinserted.clear()
|
|
||||||
break
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
main_list = send_to_goulag(main_list)
|
main_list = send_to_goulag(main_list)
|
||||||
|
|
||||||
return main_list
|
return main_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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue