From b295ee775b5c93761c54d12c3be80193cba8d3f8 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Santos Souza de Miranda Date: Wed, 21 May 2025 01:36:10 -0300 Subject: [PATCH] Added a new way to reinsert the temporary list to the main list speeding up the process --- goulag_sort/goulagsort.py | 41 ++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/goulag_sort/goulagsort.py b/goulag_sort/goulagsort.py index 093fd40..e5380ff 100644 --- a/goulag_sort/goulagsort.py +++ b/goulag_sort/goulagsort.py @@ -19,24 +19,51 @@ def send_to_goulag(main_list:list)->list: if not itens_to_be_reinserted and main_list == main_list_copy: return main_list - print(main_list) + if len(itens_to_be_reinserted)>1: itens_to_be_reinserted = send_to_goulag(itens_to_be_reinserted) i = 0 - while i < len(main_list) - 1: - 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 + merge_two_sorted_lists_in_place(main_list,itens_to_be_reinserted) main_list = send_to_goulag(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