Thread: qsort Routine issue ? please help !

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    16

    qsort Routine issue ? please help !

    I am having trouble understanding qsort C++ routine.
    The last parameter of qsort. why that function return *ia - *ib
    return Minus ? I did not found any document anywhere ?

    Please help ?

    Code:
    /* qsort int comparison function */int int_cmp(const void *a, const void *b) 
    { 
        const int *ia = (const int *)a; // casting pointer types 
        const int *ib = (const int *)b;
        // Here is the issue // return *ia  - *ib; 
    	}

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,656
    If the second element (*ib) is greater than the first element (*ia), then the result is negative, which is what qsort() expects.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    16
    Quote Originally Posted by rcgldr View Post
    If the second element (*ib) is greater than the first element (*ia), then the result is negative, which is what qsort() expects.
    Sir Not Clear. Between ia and ib there is a Minus operator which is subtraction operation ? How it's mapping greater than or less than ? which is >> or << symbol !
    Thanks!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,777
    Quote Originally Posted by cry
    Between ia and ib there is a Minus operator which is subtraction operation ? How it's mapping greater than or less than ? which is >> or << symbol !
    Firstly, the operators for "greater than" and "less than" are > and < respectively. >> is the bitwise right shift operator and << is the bitwise left shift operator.

    To understand what rcgldr is getting at, recall that qsort expects the comparison function passed to it to return one of three values whenever it is invoked to compare two elements from the given array:
    • 0 if the two elements are equal in value
    • a negative integer if the first element is less than the second element
    • a positive integer if the first element is greater than the second element

    So, assuming that the result is within the range (i.e., you never compare an element having a very negative value with an element having a very positive value), subtracting the second element from the first element would give you a number that satisfies the above conditions.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,064
    The result of qsort comparison functions is expected to be

    A negative number (any negative number) if the first parameter is less than the second parameter
    A positive number (any positive number) if the first parameter is greater than the second parameter
    Zero if both are equal.

    Say your two integers were 3 and 7 respectively. These two numbers are in sorted order.
    3 - 7 results in a negative number.

    Say your two integers were 7 and 3 respectively. These two numbers are NOT in sorted order.
    7 - 3 results in a positive number.

    > return *ia - *ib
    This is actually a cheap shot containing potential numeric underflow issues.

    This is better code, and hopefully easier for you to understand.
    Code:
    if ( *ia < *ib ) return -1;
    if ( *ia > *ib ) return +1;
    return 0; // they're equal
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2016
    Posts
    16
    qshort (/**********/) First Parameter value which can be random mixed integers. and the Last last parameter which is a function has a return type int and has two arguments so from the first parameter of qshort of the all the number assigned as the arguments of last parameter of qosrt routine ?

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is this an example of a co-routine?
    By c_weed in forum Tech Board
    Replies: 1
    Last Post: 06-20-2012, 11:36 AM
  2. Help with my XOR routine..
    By Neo1 in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2007, 03:30 PM
  3. How to write my own qsort routine
    By tonbarius in forum C Programming
    Replies: 3
    Last Post: 05-14-2006, 12:34 PM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 07:21 AM
  5. rounder routine
    By ustuzou in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2002, 08:41 AM

Tags for this Thread