quicksort.c

#include "quicksort.h"

int compare(  byte *a , byte *b ) ;

void memswap( byte *s, byte *t, int count ) {  
  
  byte tmp ; 

  while (count-- > 0 ){ 
   
    tmp = *s ;  
    *s++ = *t ;  
    *t++ = tmp ; 

  }
}





byte *select_pivot(byte data[], int n , int elementsize , int (*p_cmp_f)() ){

  return data ; 

}



void quicksort( byte data[], int n, int elementsize, int (*p_cmp_f)( ) )  {

  byte *p_left , *p_right , *p_pivot ; 
  int leftsize ; 

  if(n <= 1 )
    return ; 

  p_left = data ; 

  p_right = data + ( n-1) * elementsize ; 

  p_pivot = select_pivot(data,n,elementsize,p_cmp_f) ; 
  memswap(p_pivot,p_right,elementsize) ; 
  p_pivot = p_right ; 


  while(p_left < p_right){

    while ((*p_cmp_f)(p_left,p_pivot) < 0 && p_left < p_right ) 
      p_left += elementsize ; 
     while ((*p_cmp_f)(p_right,p_pivot) >= 0 && p_left < p_right ) 
        p_right -= elementsize ; 

     if(p_left < p_right){
       memswap(p_left,p_right,elementsize) ; 
       p_left += elementsize ; 

     }

  }

  if(p_right == data){ 
     memswap(p_right,p_pivot,elementsize) ; 
     p_right += elementsize ; 
  }

  leftsize = (p_right - data) / elementsize ; 
  quicksort(data,leftsize,elementsize,compare) ; 
   quicksort(p_right,n-leftsize,elementsize,p_cmp_f) ; 
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "quicksort.h"
#include "quicksort.c"


int compare(  byte *a , byte *b ) {
  if(*(int *)a < *(int *)b ) return -1 ;
  if(*(int *)a == *(int *)b ) return  0 ;
  
  return 1 ;
}



int main( int argc, char *argv[] ) {

  double *A ;
  int n , i ;
  time_t start ;
  time_t stop  ;

  n = atoi( argv[1] ) ;
  
  A = ( double *) malloc(sizeof(double)*n) ;     

  if ( A == NULL ){ printf(" Error mallocing \n" ); return -1 ; } 
 

  for ( i = 0 ; i <= n ; i++ ) A[i] = rand() % 10000 ; 
 
  for ( i = 0 ; i <= 19 ; i++ ) printf(" %.2f ", A[i] ) ;
  printf("\n") ;

  start = time( NULL ) ;

  quicksort( (byte *) A, n , sizeof(double) , compare ) ;

  stop = time( NULL ) ;

  printf("\n") ;
  for ( i = 0 ; i <= 19 ; i++ ) printf(" %.2f ", A[i] ) ;
  printf("\n") ;

  printf("\nTime: %d seconds\n", (int)difftime( stop, start ) ) ;

  return 0 ;

}

quicksort.h

#ifndef _header
#define _header

typedef char byte ; 


extern void quicksort(byte data[], int n , int elementsize , int (*p_cmp_f)() ) ;










#endif

built a quicksort.o file with the quicksort.c

and then compiled it like this gcc -ansi -Wall -o quicksort main.c quicksort.o

errors i get:

quicksort.o: In function `memswap':
quicksort.c:(.text+0x0): multiple definition of `memswap'
/tmp/ccwpEZSE.o:main1.c:(.text+0x0): first defined here
quicksort.o: In function `select_pivot':
quicksort.c:(.text+0x3e): multiple definition of `select_pivot'
/tmp/ccwpEZSE.o:main1.c:(.text+0x3e): first defined here
quicksort.o: In function `quicksort':
quicksort.c:(.text+0x46): multiple definition of `quicksort'
/tmp/ccwpEZSE.o:main1.c:(.text+0x46): first defined here
collect2: ld returned 1 exit status

Get rid of

#include "quicksort.c"

Line 5 in main, don't #include "quicksort.c". You should only include header files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.