c++ - using the qsort() function -


i'm student & looked function in book. works should don't quite understand inner workings of sortfunction() passed qsort() function. if 1 explain in detail, please do. in advance.

#include<iostream> #include<stdlib.h>  using namespace std;  //form of sort function required qsort() int sortfunction(const void *intone,const void *inttwo);  const int tablesize = 10;  int main() {     int i, table[tablesize];      //fill table values     for(i = 0; < tablesize; i++)     {         cout << "enter value " << (i + 1) << " : ";         cin >> table[i];     }     cout << "\n";      //sort values     qsort((void*)table, tablesize, sizeof(table[0]), sortfunction);      //print results     for(i = 0; < tablesize; i++)     {         cout << "value " << (i + 1) << " : " << table[i] << endl;     }      cout << "\ndone\n";      return 0; }  int sortfunction(const void *a, const void *b) {     int intone = *((int*)a);     int inttwo = *((int*)b);      if (intone < inttwo)     {         return -1;     }     if (intone == inttwo)     {         return 0;     }      return 1;     } 

if @ actual call qsort...

qsort((void*)table, tablesize, sizeof table[0], sortfunction);  

...you'll see provides:

  • a void* address , size (in bytes) of entire data array sorted, then
  • the size of 1 data element in array, then
  • a pointer comparison function "sortfunction".

there's no argument passed allows qsort know type of element - i.e. how individual bits in single data element used represent data value - there's no way qsort can meaningfully compare 2 such elements. when supply...

int sortfunction(const void *a, const void *b)    {        int intone = *((int*)a);        int inttwo = *((int*)b);    

...and qsort calls it, you're getting 2 pointers - they're memory addresses when qsort calls sortfunction void pointers still tell nothing data element type, qsort has no insight pass along. last 2 lines of code above - programmer coordinating qsort call - reapply knowledge you've had along data element type is: in case, they're ints, cast each void* int* (using (int*)a), dereference int* int @ memory address a. b. in doing so, you've recovered 2 numbers there as numbers. then, job of sortfunction indicate how should ordered when sorting finishes. indicate a should first, sortfunction can return negative value (e.g. -1); if they're equivalent, return 0;, , if b should first, return positive value (e.g. 1). qsort() receives information , uses work out how shuffle data elements around sorts.

fwiw, c lets express bit more succinctly as...

return intone < inttwo ? -1 :        intone == inttwo ? 0 :        1; 

...or (faster, relying on boolean comparison results being 0 , 1, may confuse programmers reading code)...

return (intone > inttwo) - (intone < inttwo); 

...or, if you're sure following can never mathematically less int_min (such values wrap around big positive number inappropriately)...

return intone - inttwo; 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -