Insertion Sort

    0

    0

    HARIGOVIND VALSAKUMAR

    PythonInsertionSort
    HGV Public Cookbook

    Function to perform Insertion sort.

    def insertion_sort(collection):
        for loop_index in range(1, len(collection)):
            insertion_index = loop_index
            while (
                insertion_index > 0
                and collection[insertion_index - 1] > collection[insertion_index]
            ):
                collection[insertion_index], collection[insertion_index - 1] = (
                    collection[insertion_index - 1],
                    collection[insertion_index],
                )
                insertion_index -= 1
        return collection
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.