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

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. 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.

3. 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.

4. @laserlight yes you are right. I changed my first post.