Problem w/ Bubble Sort

This is a discussion on Problem w/ Bubble Sort within the C Programming forums, part of the General Programming Boards category; I am having a small problem with my bubble sort. It will read the file print the current file then ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    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;
       }
    
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    2
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bubble sort problem
    By wwwildbill in forum C Programming
    Replies: 2
    Last Post: 03-27-2009, 04:43 PM
  2. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  3. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  4. Problem with Bubble Sort code
    By lisa1234 in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2004, 02:40 PM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21