Home > Archive > VC Language > November 2005 > Qsort not working
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Charles Tam 2005-11-22, 4:01 am |
| I'm using qsort to sort an array of Doubles. However, it is not working. Does
anyone know why?
// Here is the compare function.
int classA::compareValueByAscendOrder(const void* arg1, const void* arg2)
{
double* value_1 = (double*)arg1;
double* value_2 = (double*)arg2;
double delta_value = value_1 - value_2;
if( delta_value < 0 )
{
return -1;
}
else
{
if( delta_value > 0 )
{
return 1;
}
else
{
return 0;
}
}
}
// Here is the calling function
CArray<double, double> temp_array;
temp_array.Add( 5 );
temp_array.Add( 3 );
temp_array.Add( 4 );
temp_array.Add( 2 );
qsort( temp_array.GetData(), temp_array.GetSize(), sizeof(double),
compareValueByAscendOrder );
// After qsort, the array is not sorted.
=====================
5
3
4
2
after qsort
5
3
4
2
=====================
| |
| Igor Tandetnik 2005-11-22, 4:01 am |
| "Charles Tam" <CharlesTam@discussions.microsoft.com> wrote in message
news:EFEC87C4-0C67-4747-AA40-A72B3072411E@microsoft.com...
> I'm using qsort to sort an array of Doubles. However, it is not
> working. Does
> anyone know why?
>
> // Here is the compare function.
> int classA::compareValueByAscendOrder(const void* arg1, const void*
> arg2)
> {
> double* value_1 = (double*)arg1;
> double* value_2 = (double*)arg2;
> double delta_value = value_1 - value_2;
You are ordering by addresses of values (which are obviously already in
order), not the values themselves. Make it
double delta_value = *value_1 - *value_2;
Better yet, use std::sort:
#include <algorithm>
double* data = temp_array.GetData();
std::sort(data, data + temp_array.GetSize());
And once you partake of the wonder that is STL, consider doing away with
CArray and using std::vector instead:
#include <vector>
#include <algorithm>
std::vector<double> temp_array;
temp_array.push_back( 5 );
temp_array.push_back( 3 );
temp_array.push_back( 4 );
temp_array.push_back( 2 );
std::sort(temp_array.begin(), temp_array.end() );
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
| Barry Schwarz 2005-11-23, 3:59 am |
| On Mon, 21 Nov 2005 20:12:03 -0800, "Charles Tam"
<CharlesTam@discussions.microsoft.com> wrote:
>I'm using qsort to sort an array of Doubles. However, it is not working. Does
>anyone know why?
>
>// Here is the compare function.
>int classA::compareValueByAscendOrder(const void* arg1, const void* arg2)
>{
> double* value_1 = (double*)arg1;
> double* value_2 = (double*)arg2;
> double delta_value = value_1 - value_2;
You are doing pointer arithmetic when you want to do arithmetic on the
values the pointers point to.
> if( delta_value < 0 )
> {
> return -1;
> }
> else
> {
> if( delta_value > 0 )
> {
> return 1;
> }
> else
> {
> return 0;
> }
> }
>}
>
>// Here is the calling function
>CArray<double, double> temp_array;
>temp_array.Add( 5 );
>temp_array.Add( 3 );
>temp_array.Add( 4 );
>temp_array.Add( 2 );
>qsort( temp_array.GetData(), temp_array.GetSize(), sizeof(double),
>compareValueByAscendOrder );
>
>// After qsort, the array is not sorted.
>=====================
>5
>3
>4
>2
>after qsort
>5
>3
>4
>2
>=====================
<<Remove the del for email>>
|
|
|
|
|