What is argsort?

Posted on 2009-04-24
Last Modified: 2012-05-06
I have an assignment where I need to do an argsort of 5 words.  It was suggested that I should use strcmp to compare strings to sort in alphabetic order.  So, this is what I have right now, but do I need to add something to sort these???
#include <stdio.h>
#include <string.h>      

int main(int argc, char *argv[]) {              
    int i = 0;                                  
    char word[128] = "";                        
      while (word >= 1) {
            printf("Enter a word (-1 to end):\n");
            scanf("%s", word);      

    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\n", i, argv[i]);
    return 0;                                    
Question by:clscor2
    LVL 39

    Expert Comment

    >> but do I need to add something to sort these???
    Is it the arguments in argv you want to sort? If so you can implement a simple bubble sort to do this (there are plenty of examples if you Google "Bubble Sort". Your sort algorithm will need a comparator function (a function to compare 2 strings and return which is greater) and for this you can, as you observe, use strcmp.

    Have a go at implementing this... post back if you get struck.
    LVL 39

    Expert Comment

    BTW: if your assignment isn't to actually write a sort routine you could use qsort to do the sorting. You can still use strcmp to provide the comparator (since qsort can take a user defined function to do the comparing); however, it's signature is slightly different from strcmp so you'll have to write your own function that wraps strcmp.

    #include <stdlib.h>
    #include <string.h>
    // Your comparator
    int comparator ( const void * lhs, const void * rhs) {
    	return strcmp((char const *)lhs,(char const *)rhs);
    int main(int argc, char * argv[])
    	// Calling qsort with your comparator to sort argv
    	qsort ( argv, argc, sizeof(char *), comparator  );

    Open in new window


    Author Comment

    So, if I added something like this.....

     for (i = last ; i > 0 ; i--)
             for (j = 1 ; j <= i ; j++)
                if (strcmp(name[j],name[j - 1]) < 0) {
                   strcpy(name[j],name[j - 1]);
                   strcpy(name[j - 1],hold);
    LVL 39

    Accepted Solution

    Yeah, that looks like a very simple bubble sort. Thing to do is shove it into your code and see if it does what you expect.

    One possible optimization is that if you managed to read all through the list in the inner loop without performing a swap the list is sorted so you cant terminate early (otherwise you'll have to wait for all iterations of the outer and inner look to complete).

    Author Closing Comment

    Thank you!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now