Solved

Sorting function revisited

Posted on 1998-08-30
4
171 Views
Last Modified: 2010-04-02
null
0
Comment
Question by:wtc108
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1171624
It is possible there is a bug in your compiler, but I think it's more like that it's you calling qsort incorrectly.

qsort is defined:

qsort is defined byvoid qsort( void *base, size_t num, ...etc...

where base is the start of the array, num is number of elements in the array.  

My guess is you are out by 1 for num.  Remember a 2 element array starts from 0 (the last element of such an array is '1').  However num should be set to the number of elements, not the highest allowable index in the array (in this case should be 2).

Try setting num 1 higher than you currently are.  Otherwise post enough sample code for us to see.

Incidentally a good way to debug if this is happening would be to set up the following
1. Output the last item in the array (to screen or debugger)
2. Do qsort
3. Output the last item in the array

if #1 and #3 give the same answer, it pretty much proves by guess.

0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1171625
Well I figured I may as well blow the points to look up the original question...

(begin quote snoeggler)

// This functions tests for less/equal/greater(see above)
int _cdecl qsortHelper(const void *elem1,const void *elem2)
{
const float &val1=*((float *)elem1); // elem1, elem2 point to 'float' var's which
const float &val2=*((float *)elem2); // are elements of the array you pass(see below)

if(val1<val2) return -1;
if(val1==val2) return 0;
if(val1>val2) return 1;
// this will never be called
return 0;
}

// array is a pointer to the float array you wish to be sorted, size contains the
// number of elements your array contains
void SortFloats(float* array,int size)
{
qsort(array,size,sizeof(float),qsortHelper);
}

(end quote)

Make sure you pass the number of elements (not the highest element of the array) into Snoeggler's SortFloats function.

A good way to do this is to use sizeof

example
float x[5] ;
SortFloats( &x, sizeof(x) / sizeof(float) ) ;
0
 
LVL 7

Expert Comment

by:Motaz
ID: 1171626
This is a generic selection sort algorithm it can work with any data type. You can change it to quick sort and it will work faster that qsort which exist in C, because templates mechenism is smarter than qsort. qsort requeres more overhead in the recursive routine.

// A generic selection sort.

#include <iostream.h>
#include <string.h>

template <class Stype> void select(Stype *item, int count);

main()
{
 int i;

 // sort some characters
 char str[]="DJABGRC";;
 select(str,(int) strlen(str));
 cout << "The sorted string is         : " << str << endl;

 // sort some integers
 int nums[]={5,7,3,9,5,1,8};
 select(nums,7);
 cout << "The sorted numbers are       : ";
 for (i=0;i<7;i++) cout << nums[i] << " ";
 cout << endl;

 // sort some floats
 float fnums[]={5.25,7.35,3.15,9.45,5.25,1.05,8.4};
 select(fnums,7);
 cout << "The sorted float numbers are : ";
 for (i=0;i<7;i++) cout << fnums[i] << " ";
 cout << endl;

 return 0;
}

// The generic selection sort.
template <class Stype> void select(Stype *item, int count)
{
 register int a,b,c;
 int exchange;
 Stype t;

 for (a=0; a<count-1;++a) {
  exchange=0;
  c=a;
  t=item[a];
  for(b=a+1;b<count;++b) {
    if (item[b]<t){
       c=b;
       t=item[b];
       exchange=1;
    }
  }
  if (exchange) {
   item[c]=item[a];
   item[a]=t;
  }
 }
}

Regard, Motaz from Sudan.
motaz1@yahoo.com

0
 

Author Comment

by:wtc108
ID: 1171627
Thanks a lot, the sizeof() function worked like a champ. . .
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IdTCPClient1->Disconnect(); not working 3 87
c++, dynamic object by json 1 60
Why  my code (program) build with old compiler? 11 113
Adding items to a C# list incrementally 5 64
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question