Sort by

recency

|

959 Discussions

|

  • + 1 comment

    include

    using namespace std;

    int main(){ int n; cin >> n; vector unsorted(n); for(int unsorted_i = 0; unsorted_i < n; unsorted_i++){ cin >> unsorted[unsorted_i]; }

    sort(unsorted.begin(), unsorted.end(), [](const string& a, const string& b) {
        if (a.length() != b.length()) {
            return a.length() < b.length();
        }
        return a < b;
    });
    
    for (auto x:unsorted) cout << x << endl;
    
    return 0;
    

    }

  • + 0 comments

    I implemented my own mergesort in python but I was failing tests due to the conversion from str to int, and int to str.

    Fix? Simple, just run the mergesort with strings instead of ints.

  • + 0 comments
        public static List<String> bigSorting(List<String> unsorted) {
            unsorted.sort((a, b) -> {
                if (a.length() != b.length()) {
                    return Integer.compare(a.length(), b.length());
                } else {
                    return a.compareTo(b);
                }
            });
            return unsorted;
        }
    
  • + 0 comments

    include

    include

    include

    using namespace std; bool compare(const string &a, const string &b) { if (a.length() != b.length()) return a.length() < b.length(); return a < b; }

    vector bigSorting(vector unsorted) { sort(unsorted.begin(), unsorted.end(), compare); return unsorted; }

    int main() { int n; cin >> n; vector unsorted(n);

    for (int i = 0; i < n; ++i) {
        cin >> unsorted[i];
    }
    
    vector<string> sorted = bigSorting(unsorted);
    
    for (const string &num : sorted) {
        cout << num << "\n";
    }
    
    return 0;
    

    }

  • + 0 comments

    include

    include

    using namespace std;

    int main() { int n; cin >> n;

    const int MAX_LEN = 100;  
    char arr[100][MAX_LEN];  
    
    
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    
    
    for (int i = 0; i < n - 1; ++i) {
        for (int j = 0; j < n - i - 1; ++j) {
            int len1 = strlen(arr[j]);
            int len2 = strlen(arr[j + 1]);
            if (len1 > len2 || (len1 == len2 && strcmp(arr[j], arr[j + 1]) > 0)) {
                char temp[MAX_LEN];
                strcpy(temp, arr[j]);
                strcpy(arr[j], arr[j + 1]);
                strcpy(arr[j + 1], temp);
            }
        }
    }
    
    
    for (int i = 0; i < n; ++i) {
        cout << arr[i] << endl;
    }
    
    return 0;
    

    }