-1

How can I change this counting sort to make a sort from the max to min? I have no idea how to do that. I can't write the array in reverse order or change order of array after the sort. It must be in the algorithm of the sort. Here is a code.

#include<iostream>
#include<stdio.h>
#include<time.h>
using namespace std;

int main()
{
    int n;
    int k;
    cout<<"number of elements" <<n<<endl;
    cin>>n;
    cout<<"max number"<<endl;
    cin>>k;
    int tab[n];
    cout<<"array before sort :"<<endl;
    for(int i=0; i<n; i++)
    {
        cin>>tab[i];    
    }   
    cout<<"array after sort"<<endl;
    int tabp[k];
    int tabw[n];
    for(int i=0; i<k; i++)
    {
        tabp[i]=0;
    }   
    for(int i=0; i<n; i++)
    {
        tabp[tab[i]]=tabp[tab[i]]+1;
    }
    for(int i=1; i<=k;  i++)
    {
        tabp[i]+=tabp[i-1];
    }
    for(int i=0; i<n; i++) 
    {
        tabw[tabp[tab[i]]-1]=tab[i];
        tabp[tab[i]]=tabp[tab[i]]-1;
    }
    for(int i=0;i<n;i++)
    {
        cout<<tabw[i]<<" ";
    }

    return 0;
}
jww
  • 97,681
  • 90
  • 411
  • 885
Jasiu
  • 7
  • 1
  • 1
    You should use proper indenting. To sort in the reverse order, you pretty much just have to do the part where you sum the counts in reverse. – eesiraed Apr 18 '18 at 00:09
  • Take a look [here](https://stackoverflow.com/a/37105668/2610810) – Caleth Apr 18 '18 at 11:29
  • You also are not writing portable C++. `int tab[n];` is a compiler extension. You should use `std::vector` if you need a dynamically sized sequence – Caleth Apr 18 '18 at 11:38
  • Thank u so much :) – Jasiu Apr 18 '18 at 21:17

1 Answers1

-1

You should use methods from standard template library(STL) in algorithm modules. There is a good method std::sort which can help you. You pass container data to it and the third parameter can accept the way to sort given container. Here you can use functional objects from functional module in STL. You need to look at std::less<> and std::greater<>. They will sort your array by ascending or descending. Also you can pass your own function to sort the way you need.

There is a useful method std::reverse, you can reverse order of your elements in container. For example if you need to sort in ascending and descending then you can sort by ascending at first and then call std::reverse to get descending result.

// Sort by ascending
std::sort(tabw, tabw+n, std::less<int>());

//Reverse current array
std::reverse(tabw, tabw+n);

Short example with default init array data:

#include <iostream>
#include <algorithm>
#include <functional>

void TraceArr( int arr[], const int len, ostream& out )
{
    std::copy(arr, arr+len, ostream_iterator<int>(out,", "));
    std::cout << std::endl;
}

int main(int argc, const char * argv[]) {
    const int SIZE = 10;
    int arr[SIZE] {2,3,2,4,6,4,2,4,6,2};

    // Sort by ascending
    std::sort(arr, arr+SIZE, std::less<int>());
    TraceArr(arr, SIZE, std::cout);//2, 2, 2, 2, 3, 4, 4, 4, 6, 6,

    // Sort by descending
    std::sort(arr, arr+SIZE, std::greater<int>());
    TraceArr(arr, SIZE, std::cout);//6, 6, 4, 4, 4, 3, 2, 2, 2, 2,

    return 0;
}

If you want me to show it in your code, then it looks like this:

#include <iostream>
#include <algorithm>
#include <functional>
#include<stdio.h>
#include<time.h>

using namespace std;

void TraceArr( int arr[], const int len, ostream& out )
{
    std::copy(arr, arr+len, ostream_iterator<int>(out,", "));
    std::cout << std::endl;
}

int main()
{
    int n = 0;
    int k = 0;
    cout<<"number of elements" <<n<<endl;
    cin>>n;
    cout<<"max number"<<endl;
    cin>>k;
    int tab[n];
    cout<<"array before sort :"<<endl;
    for(int i=0; i<n; i++)
    {
        cin>>tab[i];
    }
    cout<<"array after sort"<<endl;
    int tabp[k];
    int tabw[n];
    for(int i=0; i<k; i++)
    {
        tabp[i]=0;
    }
    for(int i=0; i<n; i++)
    {
        tabp[tab[i]]=tabp[tab[i]]+1;
    }
    for(int i=1; i<=k;  i++)
    {
        tabp[i]+=tabp[i-1];
    }
    for(int i=0; i<n; i++)
    {
        tabw[tabp[tab[i]]-1]=tab[i];
        tabp[tab[i]]=tabp[tab[i]]-1;
    }

    TraceArr(tabw, n, std::cout);

    cout<<"array after sort with stl:"<<endl;

    // Sort by ascending
    std::sort(tabw, tabw+n, std::less<int>());
    TraceArr(tabw, n, std::cout);

    // Sort by descending
    std::sort(tabw, tabw+n, std::greater<int>());
    TraceArr(tabw, n, std::cout);

    cout<<"array after reverse with stl:"<<endl;
    //Reverse current array
    std::reverse(tabw, tabw+n);
    TraceArr(tabw, n, std::cout);


    return 0;
}

And the result is:

number of elements0
5
max number
10
array before sort :
8
5
7
9
1
array after sort
1, 5, 7, 8, 9, 
array after sort with stl:
1, 5, 7, 8, 9, 
9, 8, 7, 5, 1, 
array after reverse with stl:
1, 5, 7, 8, 9, 
Program ended with exit code: 0

May be you don't need tabp tabw arrays at all? Don't understand exactly their necessary, you can sort with a help of STL after input data with tab array, like this

std::sort(tab, tab+n, std::less<int>()); // tab keeps sorted array ascending
std::sort(tab, tab+n, std::greater<int>()); // tab keeps sorted array descending
Alexey Usachov
  • 1,364
  • 2
  • 8
  • 15