Thread: qsort

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    69

    qsort

    How is this sort function supposed to work?

    Code:
    void qsort(char *v[], int i, int j);
    
    
    {
       
       int i, last;
       void swap(char *v[], int i, int j);
       
       if (left >= right)                     
       return;                               
    
    
        swap(v, left, (left + right)/2);
        last = left;
        for (i = left+1; i<= right; i++)
             if (strcmp(v[i], v[left]) < 0)
             swap(v, ++last, i);
        swap(v, left, last);
        qsort(v, left, last-1)
        qsort(v, last+1, right);
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks intended to sort an array of pointers to char, each of which points to the first character of a null terminated string. The algorithm itself looks like an implementation of quicksort.

    By the way, avoid the name qsort as it is the name of a function from the standard library.
    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

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    69
    I don't understand the algorithm. How is it supposed to work.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by coder222
    I don't understand the algorithm. How is it supposed to work.
    I suggest that you read up on quicksort and try out examples yourself.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort
    By Moon River in forum C Programming
    Replies: 1
    Last Post: 09-28-2014, 01:02 AM
  2. how to use qsort
    By zcrself in forum C Programming
    Replies: 11
    Last Post: 12-29-2009, 09:22 PM
  3. qsort - aid
    By dayknight in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2006, 07:30 PM
  4. qsort
    By dayknight in forum C++ Programming
    Replies: 9
    Last Post: 09-17-2004, 02:06 PM
  5. qsort please help. Thanks
    By Ann Lim in forum C Programming
    Replies: 8
    Last Post: 12-12-2002, 02:20 AM