How do I go about sorting a 2-D string array in C using bubble sort (or any other kind of sorting in that matter) ? What I'm actlly trying to do is as follows :
Example:
Unsorted 2-D string array :
abfg
abcd
xyzw
pqrs
orde
Sorted 2-D string array:
abcd
abfg
orde
pqrs
xyzw
My current algorithm which is not working (gives me an incompatibility error) is as follows :
#include <stdio.h>
#include<string.h>
int main()
{
char str[5][4];
int i,j;
char temp[4];
for (i=0;i<5;i++)
{
scanf("%s",str[i]);
}
for(i = 0; i<5-1; i++)
{
for(j = 0; j<5-1; j++)
{
if(strcmp(str[j],str[j+1])== -1)
{
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
for(i = 0; i< 5; i++)
printf("%s ", str[i]);
return 0;
}
charpointers or an array ofchararrays (they're not synonymous). Then I'd put the appropriate comparison and swap code in the bubble sort to appropriately handle whatever that decision result was.