hay...i want to know how i can sort the records within text file...using c
sorting items within a file
Collapse
X
-
Tags: None
-
Here are our Posting Guidelines.
There are many ways to do that, and many different things that need clarification on that - for instance, are you re-ordering the file, or just printing stuff out? Do you want this in a data structure so you can manipulate it (this is probably necessary either way...)? Are you concerned with time - do you want the best algorithm? Is this a text file, numbered file - how do you want them sorted (numerically/alphabetically) and what is being sorted?
Please post another, more specific question about an exact difficulty you are having, as well as what you have tried to fix it. -
i hve to sort records.....wit hin the file..its text file....and sort them acccording to time feild ...Comment
-
Ok, what have you tried? What have you searched? Can you post the code and any error messages you have gotten?Originally posted by majidmajidi hve to sort records.....wit hin the file..its text file....and sort them acccording to time feild ...Comment
-
i read the file record wise..then,, i stored the records within a linked list..sorted the list...wrote it back to file....but i wana know some other way..
i wana know how to sort the within the fileComment
-
Sorting within the file itself requires a flat file. That is, each record in the file is the same length. That means no compression. Given that, you can use the file itself as the linked list. Records are fetched and writtten using seek() and tell().
Now all you need is a sort algorithm.
Here's two:
Easiest and slowest is the Exchange Sort (also called Bubble Sort):
1) start with n = 0 and compare record[n] to record[n+1] using your sort keys
2) if record[n+1] < record[n], then swap record[n] with with record[n+1].
3) increment n and repeat steps 1 and 2 until you get to the end of the file
4) when there are no swaps in steps 1-3, the file is sorted.
Trial Minimum:
1) start with n= 0 and call that record[n] the trial minimum
2) compare all other records in the file to this trial minimum. If any record is less, then swap that record with record[n].
3) when you get to the end of the file, the minimum for the file is record[n].
4) Increment n and repeat steps 1-3.
5) When the starting record[n] is the last record in the file, the file is sorted.
Both of these methods require only the key portions of the records to be read into memory. Clever designers precede the record with a "sort handle" that contains all of the fields you are allowed to sort by. In the sort only the sort handle need be read into memory.Comment
Comment