Bubble Sort

    0

    0

    Giovanny Gongora

    Codiga's C++ Recipes

    Bubble sort algorithm

    #include <algorithm>
    #include <concepts>
    #include <vector>
    
    namespace codiga::sort {
    template <typename T>
    requires std::totally_ordered<T>
    void bubble_sort(std::vector<T>& array){
      auto swapped = false;
      do {
        swapped = false;
        for (auto index = 0; index < array.size() - 1; index++) {
          if (array[index] > array[index + 1]) {
            std::swap(array[index], array[index + 1]);
            swapped = true;
          }
        }
      } while (swapped == true);
    }
    }
    
    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.