Bubblesort with file

This is a discussion on Bubblesort with file within the C Programming forums, part of the General Programming Boards category; I am stuck on the bublesort portion of my program. Any help for this rookie would be greatly appreciated. Thanks ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    6

    Bubblesort with file

    I am stuck on the bublesort portion of my program. Any help for this rookie would be greatly appreciated. Thanks

    Code:
    void sortFile(FILE *filePtr)
    {
     int num;
       
     printf("Enter 1 to sort by manufacturer, 2 to sort by M.S.R.P., or 3 to exit: \n");
     scanf("%d",&num);
    
     if (num==1)
    	 sortManu(filePtr,vehicle );
     else if (num==2)
    	 sortMSRP(filePtr, vehicle );
     else
    	 return;
     
    	 
    }
    void sortManu(FILE *filePtr, record vehicle[])
    {
    if ((filePtr=fopen("a:\\myfile.dat","r"))==NULL)
     printf("\nERROR-can't open file!");
     else
     {
    	 fread(&vehicle,sizeof(record),1,filePtr);
    
    record car;
    int pass,j,hold;
    
    for(pass=1;pass<SIZE-1;pass++)
    	for(j=0;j<=SIZE-2;j++)
    		if(car[j].name>car[j+1].name)
    		{
    			hold=car[j].name;
    			car[j].name=car[j+1].name];
    			car[j+1].name=hold;
    		}
     }
     }
    void sortMSRP(FILE *filePtr, record vehicle[])
    {
    if ((filePtr=fopen("a:\\myfile.dat","r"))==NULL)
     printf("\nERROR-can't open file!");
     else
     {
    	 fread(&vehicle,sizeof(record),1,filePtr);
    
    record car;
    int pass,j,hold;
    
    	for(pass=1;pass<SIZE-1;pass++)
    	for(j=0;j<=SIZE-2;j++)
    		if(car[j].msrp>car[j+1].msrp)
    		{
    			hold=car[j].msrp;
    			car[j].msrp=car[j+1].msrp];
    			car[j+1].msrp=hold;
    		}
    
     }
    }
    
    }

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    First of all, load your whole file into memory and run your bubblesort on it there. then write it back out to disk into another _sorted_ file. Much faster, much simpler, and it preserves your original data file.

    Second, flow chart, flow chart, flow chart. there's nothing hard about flowcharting to find your errors. Only mere amateurs avoid this powerful tool.

    Thirdly, bubble sort is very simple--

    Code:
       1)  checkValue = a value lower or as low as any you might find in the data set.
    
       2)  aValue = first value in data block
    
       3)  if EOF, bail
    
       4)  if aValue > checkValue:
    
          4a)  swap position of both values in data set.
    
          4b)  remember what highest value is: checkValue = aValue;
    
          4c)  reset aValue to first value in data block
    
          4d)  goto step 3
    
       5)  increment aValue to _next_ character in data set
    
       6)  goto step 3
    Hopefully this will help
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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