Home > Mobile >  How to sort integer array in descending order (but starting at index 0 and moving up)?
How to sort integer array in descending order (but starting at index 0 and moving up)?

Time:11-04

my question has to do with sorting an integer array into descending order, but I've got a very specific problem and was wondering if there's a way to solve it without destroying the binary search function I also implemented.

My project overall is perfect, but has one problem which is a dealbreaker according to my instructor. I read a txt file into an array and list them, then sort them into descending order, and have a function to binary search for what index a number is in. My numbers are sorted in descending order and print as such (144, 115, 100, 89, etc). This is GOOD. This is what we want. But when reading the index, 144 is listed as index[19], 115 as index[18] and so on. This is BAD. I would like to maintain the exact same output, but have the highest number be at index[0], then index[1], and be ordered like that.

Here is a picture of what my program looks like.

I'm a little confused as to how to accomplish this. I've tried a few things but regardless of how I do the sorting, the highest numbers are at the highest point on the index.

Here are the relevant bits of my code.

int binarySearch(int array1[], int p, int r, int num) {
    if (p <= r) {
        int mid = (p   r) / 2;
        if (array1[mid] == num)
            return mid;
        if (array1[mid] > num)
            return binarySearch(array1, p, mid - 1, num);
        if (array1[mid] < num)
            return binarySearch(array1, mid   1, r, num);
    }
    return -1;
}

int main() {

    int array1[20]{}; //The array that stores the numbers.
    char letters[15]{}; //The array that stores the characters.
    ifstream inputData("input.txt"); //This assumes input.txt is stored in the same folder.
    int n = 0; //Counter for the arrays
    int num; //This is for the binary search function.
    char x{};


    // Error checking for making sure the file can open
    if (!inputData)
    {
        cout << "Cannot open file.\n";
        return 0;
    }
    else {
        cout << "The unsorted list of integers: \n";
        for (int n = 0; n <= 19; n  ) {
            inputData >> array1[n];
            if (inputData.fail()) break;
            cout << array1[n] << " ";

        }

        cout << "\n\nThe sorted list of integers in descending order: \n";
        int n = sizeof(array1) / sizeof(array1[n]); //I have also tried replacing this line with just "int n = 20" and it didn't appear to make any real difference.
        sort(array1, array1   n);
        for (int n = 19; n >= 0; n--)
            cout << array1[n] << " ";

//When copy-pasting my code across I might be missing a curly bracket or two, don't worry about that stuff.

        cout << "\n \nEnter an integer to search: ";
        cin >> num;
        int index = binarySearch(array1, 0, n - 1, num);
        if (index == -1) {
            cout << num << " could not be found. Please restart the program and try another number.";
        }
        else {
            cout << "Integer " << num << " found at index [" << index << "] in the sorted integer array.";
        }

I feel like I must be missing something obvious. Please help, because I cannot get this array working properly. Occasionally when I mess about, it also stops my binary search from working properly, and it can't detect anything, so I need to tread carefully.

CodePudding user response:

In the code, you sort the array with

sort(array1, array1   n);

That will be ordered using the less-than < operator, which will sort it in ascending order.

But when you display it:

for (int n = 19; n >= 0; n--)
    cout << array1[n] << " ";

which you do in reverse order (making it seem like it's sorted in descending order). If you display it in the right order, from 0 to 19 then it will be shown in the actual sorted order.

To sort in descending order you need to use std::greater instead:

std::sort(array1, array1   n, std::greater{})

And display in the correct order:

for (int value : array1)
    std::cout << value << ' ';
  • Related