To further generalize the algorithm (past the doc string): Given a list
E(1) E(3) E(2) E(4) _ E(0) _ where
E(N + 1) is ahead of
E(N) when sorted and
_ represents a "static element" (that is, an element whose index and value will remain unchanged despite the sorting), the list returned should be
E(0) E(1) E(2) E(3) _ E(4) _.
In the following code, non-static elements would be the elements affected by the sorting, while a static element would be unaffected (i.e, an
def static_sort(old_list: list, static_index_list: list) -> list: """ Returns a list whose non-static elements (defined to be the ones whose indexes are not specified in the static_index_list) are sorted in ascending order. The static elements (with indexes specified in the static_index_list) remain unchanged in both numeric value and index in the original list. static_sort([1, 9, 3, 5], [0, 2]) -> [1, 5, 3, 9] static_sort([0, 8, 2, 6], ) -> [0, 6, 2, 8] :param static_index_list: A list of indexes whose associated elements in old_list should be exclusive of the sorting in the rest of the list. :param old_list: The unsorted list, to-be sorted using the comparator and static_index_list. """ sorted_list =  non_sort_subjects = list(map(lambda static_index: old_list[static_index], static_index_list)) def sort_subject_filter(element): return element not in non_sort_subjects sort_subjects = sorted(list(filter(sort_subject_filter, old_list))) whole_index = sort_subject_index = 0 while whole_index < len(old_list): if whole_index in static_index_list: sorted_list.append(non_sort_subjects[whole_index - sort_subject_index]) else: sorted_list.append(sort_subjects[sort_subject_index]) sort_subject_index += 1 whole_index += 1 return sorted_list
Reviews on the algorithm itself in addition to formatting/code style are appreciated.