inhuman_Arif

Sort names in alphabetical order

Oct 28th, 2021
633
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5.  
  6. int main()
  7. {
  8.     #ifndef ONLINE_JUDGE
  9.         freopen("input.txt", "r", stdin);
  10.         freopen("output.txt", "w", stdout);
  11.     #endif
  12.  
  13.     int n;
  14.     cin >> n;
  15.     string names[n];
  16.     for(int i=0;i<n;i++)
  17.         cin >> names[i];
  18.     for(int i=0;i<n-1;i++)
  19.     {
  20.         int key=i;
  21.         for(int j=i+1;j<n;j++)
  22.             if(names[j]<names[key])
  23.                 key = j;
  24.         swap(names[key],names[i]);
  25.     }
  26.     for(int i=0;i<n;i++)
  27.         cout << names[i] << " ";
  28.     cout << endl;
  29.    
  30.     return 0;
  31. }
RAW Paste Data