Thread: C Program to Sort set of strings in alphabetical order using Heap Sort

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    1

    C Program to Sort set of strings in alphabetical order using Heap Sort

    Hello, maybe someone know how to change this program, that it sorting set of strings, because now it sorts only numbers
    Code:
    #include <stdio.h>
    
    
    void main()
    {
        int heap[10], no, i, j, c, root, temp;
    
    
        printf("\n Enter no of elements :");
        scanf("%d", &no);
        printf("\n Enter the nos : ");
        for (i = 0; i < no; i++)
           scanf("%d", &heap[i]);
        for (i = 1; i < no; i++)
        {
            c = i;
            do
            {
                root = (c - 1) / 2;
                if (heap[root] < heap[c])   /* to create MAX heap array */
                {
                    temp = heap[root];
                    heap[root] = heap[c];
                    heap[c] = temp;
                }
                c = root;
            } while (c != 0);
        }
    
    
        printf("Heap array : ");
        for (i = 0; i < no; i++)
            printf("%d\t ", heap[i]);
        for (j = no - 1; j >= 0; j--)
        {
            temp = heap[0];
            heap[0] = heap[j];    /* swap max element with rightmost leaf element */
            heap[j] = temp;
            root = 0;
            do
            {
                c = 2 * root + 1;    /* left node of root element */
                if ((heap[c] < heap[c + 1]) && c < j-1)
                    c++;
                if (heap[root]<heap[c] && c<j)    /* again rearrange to max heap array */
                {
                    temp = heap[root];
                    heap[root] = heap[c];
                    heap[c] = temp;
                }
                root = c;
            } while (c < j);
        }
        printf("\n The sorted array is : ");
        for (i = 0; i < no; i++)
           printf("\t %d", heap[i]);
        printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
    }

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    First of all you need an array of strings. So this is easy with an array of pointers for example :

    Code:
      char *planets[] = { "Mercury" , "Venus" , "Earth" , "Mars" , "Jupiter" , "Saturn" , "Uranus" , "Neptune" , "Pluto" };
    Τhis is an array of strings that can hold 9 strings. I initialized this array with specific strings.They are name of known planets. You can also declare an array of strings and get the input from the keyboard.

    Code:
     printf("The first planet is : %s" , planets[0]); // Prints : The first planet is Mercury.
    Since you have your "holding" place where is your array of strings you can get the string from the keyboard and then put it to the appropriate place in your array. Then you can use strcpy function in order to put the string to the correct position according to your sorting algorithm.

    P.S You should use strcmp function as laserlight said in order to compare you strings. Then you can use strcpy function as I said before because there is no dircet way to copy a string only character by character or strcpy function.
    Last edited by Mr.Lnx; 04-12-2015 at 11:58 AM. Reason: adding strcmp function.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mr.Lnx
    Τhis is an unitialized array that can hold 9 strings the last one will be the '\0' character.
    There is no need to terminate the array with an empty string.

    Anyway, for this conversion to work, you need to keep in mind that while you might use say, x < y to compare integers, you would use say, strcmp(x, y) < 0 to compare strings. If you are using Mr.Lnx's suggestion of an array of pointers, then x = y for assigning integers would also work to assign pointers, but if you are using an array of arrays to store the strings, then you may need to use strcpy (or something similiar) instead.
    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

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    @laserlight yes you are right. I changed my first post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-21-2012, 10:31 PM
  2. String sort in alphabetical order
    By ThLstN in forum C Programming
    Replies: 6
    Last Post: 01-06-2008, 02:59 AM
  3. Replies: 8
    Last Post: 10-21-2007, 01:38 PM
  4. Can counting sort sort in descending order?
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 03-06-2003, 09:59 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM