Thread: qsort problem (ish)

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    qsort problem (ish)

    Well I was reading up on function pointers - all very interesting - and the example it showed me was the use of the qsort function. So, I closed the page, opened my IDE and fiddled. The result:

    Code:
     
    #include <algorithm>
    #include <iostream>
    int Comparison (const void* _a, const void* _b)
    {
    	const int a = (const int) _a;
    	const int b = (const int) _b;
    	if (a > b)
    		return 1;
    	else {
    		if (a == b)
    			return 0;
    		else
    			return -1; // a < b
    	}
    }
    void walk_array (int a[], int length)
    {
    	for (int i=0; i<length; i++)
    		std::cout << a[i] << std::endl;
    }
     
    int main()
    {
    	int data[10] = { 3, 7, 5, 9, 11, 90, 44, 12, 54, 47 };
    	qsort ((void*) data, 10, sizeof (int), &Comparison);
    	walk_array (data, 10);
    	return 0;
    }
    It compiles and runs fine, I just get this output, apparently sorted, but not in any particular order. Weird:

    Code:
     
    90 
    3
    5
    9
    11
    7
    44
    12
    54
    47
    Very unsorted.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ah I just noticed something. Is my compare function returning the wrong number(s)?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    The numbers are fine, your casting is a bit off. Plus you need to leave out the & operator when passing the function as an argument.

    Code:
    #include <algorithm>
    #include <iostream>
    
    int Comparison (const void* _a, const void* _b)
    {
    	const int a = *((const int*)_a);
    	const int b = *((const int*)_b);
    
    	if (a > b) return 1;
    	
    	if (a == b) return 0;
    		
    	return -1; // a < b
    
    }
    void walk_array (int a[], int length)
    {
    	for (int i=0; i<length; i++)
    	{
    		std::cout << a[i] << std::endl;
    	}
    }
     
    int main()
    {
    	int data[10] = { 3, 7, 5, 9, 11, 90, 44, 12, 54, 47 };
    
    	qsort( data, 10, sizeof(data[0]), Comparison );
    
    	walk_array( data, 10 );
    
    	return 0;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thanks that did the job!

    Oh and thanks for clearing up my Comparison function; it was a little messy.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM