Thread: quicksort

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    quicksort

    Hello,

    I am trying to use the qsort function but i get an error :

    error : expected expression before 'int' on the line where i call the qsort function

    Code:
    int compare(int *a, int *b);
    void schrijfUit (int array[]);
    
    int main (void){
    
    	int array[10] = {1, 5, 23, 43, 3, 3, 543, 2, 542, 53};
    
    	schrijfUit(array);
    	
    	qsort(array,10,sizeof(int),int compare (int *a, int *b));
    	
    	schrijfUit(array);
    	return 0;
    
    }
    
    void schrijfUit (int array[]){
    	
    	int i;
    
    	for (i = 0; i < 10; i++)
    		printf("%i\t", array[i]);
    	printf("\n");	
    
    }
    
    
    int compare(int *a,int *b) {
     	if (*a==*b)
        		return 0;
      	else{
        		if (*a < *b)
            		return -1;
         		else
          			return 1;
    	}
    }
    I think it is a very stupid mistake but i cant see it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to put in the name of the function, not the prototype.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    After you have fixed that, you should change this:
    Code:
    int compare(int *a, int *b);
    to:
    Code:
    int compare(const void *a, const void *b);
    since that is what qsort expects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    Thank you guys

    it is alive

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with quicksort!
    By coni in forum C Programming
    Replies: 3
    Last Post: 10-03-2009, 02:29 AM
  2. Scalability problems with Quicksort
    By hbejkosa in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 11:26 PM
  3. Question 39: Quicksort vs Linear Search
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-17-2005, 11:03 AM
  4. quicksort problem
    By spinner in forum C Programming
    Replies: 9
    Last Post: 05-04-2003, 02:02 PM
  5. Quicksort
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-15-2002, 09:42 AM