qsort problem (ish)

This is a discussion on qsort problem (ish) within the C++ Programming forums, part of the General Programming Boards category; Well I was reading up on function pointers - all very interesting - and the example it showed me was ...

  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,139
    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, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21