0

Hi everyone,

I am having trouble with a function that mergesorts a generic array (using void *); it seems that I can't swap two elements in it

Here is the code

void mysort( int n,                      // Number of elements
	     int elementSize,            // Size of each element
	     void * array,               // Pointer to an array
	     int ascending,              // 0 -> descending; 1 -> ascending
	     CompareFunction compFunc )  // Comparison function.
{

	  int i, j; 
	  bool flag = true;    
          void * temp;            
           int arrayLength = n; 
          for(i = 1; (i <= arrayLength) && flag; i++)
         {
            flag = false;
            for (j=0; j < (arrayLength -1); j=j+1)
            {
			  void *arrayItem = (void*)((char*)array + j*elementSize );
			  void *arrayMove = (void*)((char*)array + (j+1)*elementSize );
			  

			  
               
			  if(ascending==0)
			   {
				   
				   if (compFunc( arrayItem, arrayMove )<=0)      // descending
				   { 
						temp = arrayItem;             // swap elements
						arrayItem = arrayMove;
						arrayMove = temp;
						temp = NULL;
						flag = true;               // indicates that a swap occurred.
					}
			   }
			   else if(ascending!=0)
			   {
					if (compFunc(arrayItem, arrayMove)>=0)      // ascending
				   {   
					    

						temp = arrayItem;             // swap elements
						arrayItem = arrayMove;
						arrayMove = temp;
						temp = NULL;
						flag = true;               // indicates that a swap occurred.
						

					}
			   }

			   arrayItem = NULL;
			   arrayMove = NULL;
          }

          
  }
     }
     return;   
	
}

The swapping parts does not affect the actual array. I have some guesses, but I couldn't figure out how to deal with it. Can you help?

2
Contributors
1
Reply
4
Views
8 Years
Discussion Span
Last Post by Narue
1

>temp = arrayItem; // swap elements
>arrayItem = arrayMove;
>arrayMove = temp;
This won't do jack squat because you're just swapping the pointer values, not the data stored at those addresses. Ignoring the fact that with C++ you should be using templates instead of the much more error prone void* for generic typing, here's an example of a sorting algorithm for an array of int and the corresponding generic algorithm using void*:

void sort ( int a[], int n )
{
  int i = 0;

  while ( i < n ) {
    if ( i == 0 || a[i - 1] <= a[i] )
      i++;
    else {
      int save = a[i];
      a[i] = a[i - 1];
      a[i - 1] = save;
      --i;
    }
  }
}
typedef int CompareFunc ( const void *a, const void *b );

void sort2 ( void *a, int n, int size, CompareFunc compare )
{
  unsigned char *save = (unsigned char*)std::malloc ( size );
  unsigned char *bytes = (unsigned char*)a;
  int i = 0;

  while ( i < n ) {
    if ( i == 0 || compare ( &bytes[(i - 1) * size], &bytes[i * size] ) <= 0 )
      ++i;
    else {
      std::memcpy ( save, &bytes[i * size], size );
      std::memcpy ( &bytes[i * size], &bytes[(i - 1) * size], size );
      std::memcpy ( &bytes[(i - 1) * size], save, size );
      --i;
    }
  }

  std::free ( save );
}
Votes + Comments
void*??? Rashakil Fol disapproves of this message.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.