Thread: Problem with sorting

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    33

    Problem with sorting

    I want to sort the records in a file in ascending order. Is there any predefined function for this? If there's none, can anyone give me a tutorial link, sample code, or an algorithm that would be helpful in solving my problem?

    Thanks in advance.


    EDIT:

    I'm lost. Most people recommended mergesort. BUT how am I suppose to implement it in a code like this?


    (display function)

    Code:
    void disp_all()
    {
       record stud; 
    
       rewind(fp);
       clrscr();
       printf("STUDENT#\t%-16s%-16s%-12s%-12s%-12s\n\n","LAST NAME","FIRST NAME","MI","BDAY", "COURSE  ");
       while(fread(&stud,sizeof(stud),1,fp))
       {
          if(stud.snum > 0)
          {
         
             printf("%lld\t", stud.snum);    
             printf("%-16s", stud.lname);
             printf("%-16s", stud.fname);
             printf("%-12s", stud.mi);
             printf("%-12s", stud.bday);
             printf("%-12s\n", stud.course);
          }
       }
       getch();
    }
    Please...I need urgent help...
    Last edited by preeengles; 04-21-2009 at 01:45 AM.

  2. #2
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    I'm not using arrays, by the way...Will using qsort still be okay?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by preeengles
    Will using qsort still be okay?
    No, at least from what I know.

    What are you using?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can do a sort, right on the HD, but it is slow and not easy to code, as well.

    It sounds like you want to:

    1) read the records on the HD
    2) put them into an array of structs in memory
    3) sort them in the array (you can use qsort)
    4) write out the sorted results to a file.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by preeengles View Post
    an algorithm that would be helpful in solving my problem?

    Mergesort! There's lots of info around on it, and it's Pretty Damn Clever.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by laserlight View Post
    What are you using?
    File handling? :|

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    What do you mean?

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by Adak View Post
    You can do a sort, right on the HD, but it is slow and not easy to code, as well.

    It sounds like you want to:

    1) read the records on the HD
    2) put them into an array of structs in memory
    3) sort them in the array (you can use qsort)
    4) write out the sorted results to a file.
    Actually, this question is related to the program I made before (the one in the the thread 'Something wrong with output'). This time, I want to sort the records....

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, that's good to know.

    There are lots of good ways to sort records in a file, MK's suggestion is also good.

    How many records are you talking about sorting, and how do you want/need to do it?

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    there many sorting algorithms for bubble sort maybe you can use it in your program for sorting out the records.
    Bubble sort - Wikipedia, the free encyclopedia

  12. #12
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Why not mergesort? It's not too difficult and amazingly fast.

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Alright. I'll try mergesort

    edit:

    @Adak: I'm planning to sort about 10 records. I want them sorted based on student#...
    Last edited by preeengles; 04-20-2009 at 08:39 PM.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Brafil View Post
    Why not mergesort? It's not too difficult and amazingly fast.
    I don't know if "amazingly" is the right word... It's a O(n log n) sort, just like most other reasonable, general-purpose sorts.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by preeengles View Post
    Hey, just to clarify, it seems mergesort works with codes that use arrays...but mine doesn't so it wouldn't apply?
    Mergesort can be implemented for linked lists or doubly-linked lists as well. It can even be implemented for external sorting, of files that do not fit into memory. The code is of course slightly different in each case.

    Anyway, don't just tell us what it isn't stored as is memory, tell us what it is stored as. Show us your data structures. Or if you haven't got any data structure yet, sort that out first because you're going to need some kind of data structure.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 02:16 PM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. What is problem about the sorting?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 07:00 AM
  4. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 01:44 PM