Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Help with selection sort for strings?

Wed Mar 04, 2009 7:17 pm

I have to turn in this C program today that is able to selectively sort a list of names from an input file, but when I compile it and run it, it doesn't sort it correctly. Can anyone please help me here? I am losing my mind to my computer.

cpp code
#include<stdio.h>

# define LEN 20
# define NUM 10

int readFromFile(char text[NUM][LEN]) {

FILE *fp;
int i;


if ((fp = fopen("input.txt", "r")) == NULL) {
printf("Error opening file");
return -1;
}


for (i = 0; i < NUM; i++) {
fscanf(fp, "%s..n", text[i]);
}


fclose(fp);
return 0;
}


void selectionSort(char text[NUM][LEN]) {
int i, j;
int min;
for(i = 0; i < NUM -1; i++)
{
min = i;
for(j = i + 1; j < NUM; j++)
{
CompareStrings(text[i], text[i+1]);
if(CompareStrings(text[i], text[i+1]) == -1);
{min = j;}
}
SwapStrings(text[i], text[i+1]);
}
return;
}

int CompareStrings(char s1[10], char s2[10])
{
int i;
for(i = 0; i < 10; i++)
{
if (s1[i] > s2[i])
{ return (1);}
if (s1[i] < s2[i])
{return (-1);}
if (s1[i] == 0 || s2[i] == 0)
{break;}
}

return 0;
}

void SwapStrings(char s1[10], char s2[10])
{

int i;
char c;

for(i = 0; i <10; i++)
{ c = s1[i];
s1[i] = s2[i];
s2[i] = c;
}
}

void SwapStrings(char s1[10], char s2[10]);
int CompareStrings(char s1[10], char s2[10]);
int readFromFile(char text[NUM][LEN]);
void selectionSort(char text[NUM][LEN]);

int main() {
char input[NUM][LEN];
int i;
if(readFromFile(input) == -1) {
printf("Error reading from file..n");
return -1;
}

readFromFile(input);
printf("UNSORTED ARRAY: ..n");
for(i = 0; i<10; i++)
{printf("%s", input[i]);
printf("..n");}

selectionSort(input);

printf("..nSORTED ARRAY: ..n");
for(i = 0; i<10; i++)
{printf("%s", input[i]);
printf("..n");}

return 0;
}




Re: Help with selection sort for strings?

Thu Mar 05, 2009 10:08 pm

: well if i understand what you want to do correctly have a look at this example i think it will help you solve your problem ...
Code:
#include<string.h>
int main()
{
  int i,n,j;
  int bSwapped=1;
  char *names[100]; // your actual Array Declaration
  char* pstr;
  char temp[20];   // temperory Char Array to store the Readed Sring
  printf("Enter total no Of Students");
  scanf ("%d",&n);
  for(i=0;i<n;i++)
   {
     printf("Enter the %d StudentName : ",(i+1) );
     scanf("%s",temp);
     name[i] = strdup(temp);
   }
  while (bSwapped)
  {
   bSwapped=0;
   for(i=0;i<n-1;i++)
    for(j=i+1;j<n;j++)
      if(strcmp(name[i],name[j])>0)
        {
          pstr=name[i];
          name[i]=name[j];
          name[j]=pstr;
          bSwapped=1;
        }
  }
   // Print the Sorted List of Student names
  for(i=0;i<n;i++)
   printf("\n %s",name[i]);
  return 0;
}
 


Re: Help with selection sort for strings?

Thu Mar 05, 2009 10:12 pm

Try :
c-c/selection-sort-t27.html
c-examples/c-selection-sort-t2916.html

Post a reply
  Related Posts  to : Help with selection sort for strings?
 Sort strings java-Sorting Array of Strings     -  
 C++ Selection Sort     -  
 selection sort     -  
 Good looking selection box with image per option     -  
 selection of checkbox in buttongroup in netbeans     -  
 calculate data from multiple selection     -  
 fill selection options from list     -  
 validate age entered as selection box in javascript     -  
 C++ Strings     -  
 PHP Strings     -  

Topic Tags

C++ Sorting