dimanche 19 mars 2023

The sorted out put is not correct. It didn't perform ascending sort

I don't know what is wrong with my code. can some help me correct it. The sorted out should be in ascending order

#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>

using namespace std;

void slowSort(vector<int>& a, int i, int j) {
    if (i >= j)
    return;
    int m = (i + j) / 2;
    slowSort(a, i, m);
    slowSort(a, m + 1, j);
    if (a[i] > a[j] && i < j)
    swap(a[i], a[j]);
}

int main() {
    srand(time(0));
    vector<int> a(500);
    for (int i = 0; i < 500; i++) {
        a[i] = rand() % 500; // generate random integers between 0 and 999
    }
    int i = rand() % 500; // generate random index i
    int j = rand() % 500; // generate random index j
    cout << "Before sorting:" << endl;
    for (int x : a) {
        cout << x << " ";
    }
    cout << endl;
    slowSort(a, 0, 999);
    if (i < j && a[i] > a[j]) swap(a[i], a[j]); // swap i and j if necessary
    cout << "After sorting:" << endl;
    for (int x : a) {
        cout << x << " ";
    }
    cout << endl;
    return 0;
}

I don't know what is wrong with my code. can some help me correct it. The sorted out should be in ascending order




Aucun commentaire:

Enregistrer un commentaire