Insertion Sort

    0

    0

    Aakanksha Priya

    def insertion_sort(unsorted_arr):
       for i in range(1, len(unsorted_arr)):
          j = i-1
          nxt_element = unsorted_arr[i]
    # Compare the current element with next one
       while (unsorted_arr[j] > nxt_element) and (j >= 0):
          unsorted_arr[j+1] = unsorted_arr[j]
          j=j-1
       unsorted_arr[j+1] = nxt_element
    arr= [45,89,56,78,90,12,10,2,34,1]
    insertion_sort(arr)
    print(arr)
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.