-
Problem w/ Bubble Sort
I am having a small problem with my bubble sort. It will read the file print the current file then maybe or or maybe not does not go into my bubblesort funcion then closes. But i need it to print back out the sorted data. Any ideas and or suggestions please feel free to post. Code is below.
Code:
#include <stdio.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
void bubblesort(char x[][16], char y[][7],int N);
void printlist(char x[][16], char y[][7]);
main()
{
char name1[25][16], name2[25][7];
int i, N;
i=0;
N=0;
FILE *fin, *fout;
fin =fopen("A:\\BubbleSortIn.txt","r");
fout=fopen("A:\\BubbleSortOut.txt","w");
printf("|| Here Is The Unsorted List ||\n\n");
while(fscanf(fin,"%s %s",name1[i],name2[i]) != EOF)
{
printf("%-17s\t%-7s\n",name1[i], name2[i]);
fprintf(fout,"%-17s\t%-7s\n",name1[i], name2[i]);
i = i + 1;
}
printf("\n|| Here Is The Sorted List ||\n\n");
bubblesort(name1, name2, N);
printlist(name1, name2);
fclose(fin);
fclose(fout);
}
void bubblesort(char x[][16], char y[][7],int N)
{
int I, J ,J_End;
char Hold_it_x[16], Hold_it_y[7];
int Done;
J_End = N - 1;
Done = FALSE;
I = 0;
while ((I < N-1) && (!Done))
{
Done = TRUE;
J = 0;
while (J < J_End )
{
if ( strcmp(x[J], x[J+1])> 0)
{
strcpy(Hold_it_x , x[J]);
strcpy( x[J], x[J+1]);
strcpy(x[J+1], Hold_it_x);
strcpy(Hold_it_y, y[J]);
strcpy(y[J], y[J+1]);
strcpy(y[J+1], Hold_it_y);
Done = FALSE;
}
J = J + 1;
}
J_End = J_End - 1;
I = I + 1;
}
}
void printlist(char name1[][16], char name2[][7])
{
int i=0;
int N=0;
while(i < N)
{
printf("%-17s\t%-7s\n",name1[i], name2[i]);
i = i + 1;
}
}
-
Why aren't you using a structure to store both of those names? It would be way less confusing to read. Also, you say it "may or may not" be going into your bubble sort function. How about adding some handy dandy printf lines to debug your code?
Code:
printf("\n|| Here Is The Sorted List ||\n\n");
printf("Entering bubblesort...\n");fflush(stdout);
bubblesort(name1, name2, N);
printf("bubblesort has returned.\n");fflush(stdout);
printlist(name1, name2);
...
void bubblesort(char x[][16], char y[][7],int N)
{
int I, J ,J_End;
char Hold_it_x[16], Hold_it_y[7];
int Done;
J_End = N - 1;
Done = FALSE;
I = 0;
printf("In bubblesort, beginning sort...\n");fflush(stdout);
...
I = I + 1;
}
printf("Bubblesort finished...\n");fflush(stdout);
}
Quzah.
-
You know the sad thing is I should have known to do what you suggested. Sometimes it takes a child to open adult’s eyes. Thanks ill get right on that when I get home. Thank you much.
-
>Sometimes it takes a child to open adult’s eyes.
That's an interesting choice of words. Anyway, why are you using bubble sort? Insertion sort is much easier and it's more efficient too.