Lexicographical sorting

    0

    1

    Giovanny Gongora

    Codiga's C++ Recipes

    Takes 10 words from the user and sorts them in lexicographical order using the bubble sort algorithm.

    #include <iostream>
    
    using namespace std;
    int main()
    {
      string str[10], temp;
      cout << "Enter 10 words: " << endl;
      for(int i = 0; i < 10; ++i){
        getline(cin, str[i]);
      }
      // Use Bubble Sort to arrange words
      for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 9 - i; ++j) {
          if (str[j] > str[j + 1]) {
            temp = str[j];
            str[j] = str[j + 1];
            str[j + 1] = temp;
          }
        }
      }
      cout << "In lexicographical order: " << endl;
      for(int i = 0; i < 10; ++i){
        cout << str[i] << endl;
      }
      return 0;
    }
    
    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.