Sort in descending order with index

L

Luigi Napolitano

Hello,

I use this algorithm to sort an array "v" in the descending order.

void quickSort(float v[], int lo0, int hi0)
{
int lo = lo0;
int hi = hi0;
if (lo >= hi) return;
float mid = v[(lo + hi) / 2];

while (lo < hi)
{
while (lo<hi && v[lo] > mid) lo++;
while (lo<hi && v[hi] < mid) hi--;
if (lo < hi) swap(v,lo, hi);
}

if (hi < lo) swap(v, hi, lo);

quickSort(v, lo0, lo);
quickSort(v, lo == lo0 ? lo+1 : lo, hi0);
}

I would like to have an array "index" to order another array "z".

Example:
v = [ 5, 9, 7 ]
index = [ 1, 2, 0 ]
z = [ 1, 2, 3 ]

so...
for (i = 0; i<=2; i++)
cout<<z[index];
Output: [ 2, 3, 1 ]

I would like to have index = [ 1, 2, 0 ]. How?
Thanks to everybody.

Luigi Napolitano
 
B

Buster

Luigi Napolitano wrote:

[...]
I would like to have an array "index" to order another array "z".

Example:
v = [ 5, 9, 7 ]
index = [ 1, 2, 0 ]
z = [ 1, 2, 3 ]

so...
for (i = 0; i<=2; i++)
cout<<z[index];
Output: [ 2, 3, 1 ]

I would like to have index = [ 1, 2, 0 ]. How?


// greater_indexed_t class template:
// a function object that compares indices according
// to the '>'-ordering of the indexed elements.

template <typename RAIterator>
struct greater_indexed_t
{
greater_indexed_t (RAIterator begin)
: _begin (begin) { }

template <typename Index>
bool operator () (Index i, Index j)
{
return _begin > _begin [j];
}

private:
RAIterator _begin;
};

// greater_indexed function template:
// template parameter deduction simplifies usage.

template <typename RAIterator>
greater_indexed_t <RAIterator>
greater_indexed (RAIterator begin)
{
return greater_indexed_t <RAIterator> (begin);
}

// Test it:

#include <algorithm>
#include <ostream>
#include <iostream>

int main ()
{
int v [] = { 5, 9, 7, };
int index [] = { 0, 1, 2, };

std::sort (index, index + 3, greater_indexed (v));

for (unsigned i (0); i != 3; ++ i) std::cout << index << ' ';
std::cout << '\n';
}
 
L

Luigi Napolitano

Buster said:
// Test it:

#include <algorithm>
#include <ostream>
#include <iostream>

int main ()
{
int v [] = { 5, 9, 7, };
int index [] = { 0, 1, 2, };

std::sort (index, index + 3, greater_indexed (v));

for (unsigned i (0); i != 3; ++ i) std::cout << index << ' ';
std::cout << '\n';
}


Thanks for your reply, but it was very simple...

int main()
{
int v[] = { 5, 9, 7 };
int index[] = { 0, 1, 2 };

for (int i = 0; i < v.length; i++)
index = i;

quickSort(v, index, 0, v.length-1);
}

void quickSort(float v[], int index[], int lo0, int hi0)
{
...
swap(v, index, hi, lo);
...
}

void swap(float v[], int index[], int i, int j)
{
...
int t;
t = index;
index = index[j];
index[j] = t;
}

So, I have index[] = { 1, 2, 0 }.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top