Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++:Sort the initial part of an array in ascending and the other part in descending order

I'm new to C++ and I'm trying to do this:

I have an array of N elements. User should be able to input all the elements of an array and a number K. After that I have to sort the array such that the first part (elements 1 to K) be sorted in ascending mode and the second part (elements K to N) be sorted in descending mode.

Sorting function is implemented by myself. I can use qsort from cstdlib, but it's not so interesting.

I have coded for sorting an array, but I can't understand how to sort an array in two parts.

#include <iostream>
#include <string>

void print_array(int[], int);
void qsort(int[], int, int);

int main()
{
    int array_length;
    int *array, k;
    std::cout << "Write array length: ";
    std::cin >> array_length;
    array = new int[array_length];
    for (int i = 0; i < array_length; i++) {
        std::cout << "Write " << i + 1 << " element: ";
        std::cin >> array[i];
    }
    print_array(array, array_length);
    do {
        std::cout << "Write k: ";
        std::cin >> k;
    } while (k >= array_length);
    qsort(array, 0, k);
    print_array(array, array_length);
}


void print_array(int* array, int length) {
    for (int i = 0; i < length; i++) {
        std::cout << array[i] << "\n";
    }
}

void qsort(int arr[], int fst, int last)
{
    int i, j, pivot, tmp;
    if (fst < last)
    {
        pivot = fst;
        i = fst;
        j = last;
        while (i < j)
        {
            while (arr[i] <= arr[pivot] && i < last)
                i++;
            while (arr[j] > arr[pivot])
                j--;
            if (i < j)
            {
                tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
        }
        tmp = arr[pivot];
        arr[pivot] = arr[j];
        arr[j] = tmp;
        qsort(arr, fst, j - 1);
        qsort(arr, j + 1, last);
    }
}
like image 852
KardanovIR Avatar asked Dec 22 '15 09:12

KardanovIR


People also ask

How do you sort an array in ascending and descending order?

We can sort arrays in ascending order using the sort() method which can be accessed from the Arrays class. The sort() method takes in the array to be sorted as a parameter. To sort an array in descending order, we used the reverseOrder() method provided by the Collections class.

How do you sort the first half of an array?

Algorithm : Sort the given array. Run a loop up to half the length of the array and print the elements of the sorted array. Run a loop from the last index of the array to the middle of the array and print the elements in reverse order.

How do you sort an array element in descending order?

To sort an array in Java in descending order, you have to use the reverseOrder() method from the Collections class.

How can we sort the array elements in descending order in C?

Use the Reverse() method that would eventually give you a sorted array in descending order. Array. Reverse(list); You can try to run the following code to to sort an array in descending order.


3 Answers

You are sorting one half with:

qsort(array, 0, k);

and similarly, you need sort other half:

qsort(array+k, 0, array_length-k);

Now, the problem is that both parts will be in ascending order. So you need a way to tell qsort() to sort one half in ascending order and the other half in descending order. Pass another flag to qsort() to change the swap order. So you can pas a bool to indicate it:

void qsort(int arr[], int fst, int last, bool pass)
{
           ....
           if (pass && i < j)
            {
                tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
            if(!pass && i > j) {
                tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
       ...
       qsort(arr, fst, j - 1, pass); 
       qsort(arr, j + 1, last, pass);

}

And when you call it you can pass true and false to "switch" the swap order:

  qsort(array, 0, k, true);
  qsort(array+k, 0, array_length-k, false);

Change the prototype of qsort() accordingly.

like image 164
P.P Avatar answered Sep 17 '22 00:09

P.P


You just have to replace following lines, in order to get data in decreasing order:

        //while (arr[i] <= arr[pivot] && i < last)
        while (arr[i] >= arr[pivot] && i < last)
            i++;
        //while (arr[j] > arr[pivot])
        while (arr[j] < arr[pivot])
like image 36
Pawan Avatar answered Sep 19 '22 00:09

Pawan


From what I see, your array contains purely integer values which are primitive in nature and can be sorted using c++ sort method.

#include <algorithm> // this is to access c++ std::sort method

...

std::sort(array + first, array + last) // input range of index of array to be sorted

...

This should take care of it.

Another point to take note is that this sorts in ascending order by default. So you can play around with CompareTo method and all that. But my favorite trick is to multiply -1 to all the values in array and sort it and multiply -1 back, ending up with array sorted in descending order.

like image 45
seokhoonlee Avatar answered Sep 20 '22 00:09

seokhoonlee