Insertion Sort

    0

    2

    Giovanny Gongora

    Codiga's C++ Recipes

    Insertion Sort implementation

    #include <concepts>
    #include <vector>
    
    namespace mehara::sort {
    template <typename T>
    requires std::totally_ordered<T>
    void insertion_sort(std::vector<T>& array){
      for (auto outer_index = 1; outer_index < array.size(); outer_index++) {
        auto key = array[outer_index];
        auto inner_index = outer_index - 1;
        while (inner_index >= 0 && array[inner_index] > key) {
          array[inner_index + 1] = array[inner_index];
          inner_index = inner_index - 1;
        }
        array[inner_index + 1] = key;
      }
    }
    }
    
    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.