der_23
asked on
how do i sort a file?
I don't know how to sort a file without using a link list.
I want to copy a file into the array first then sort them by id number.Then copy it back to the original file as a sorted one.
(my file is a student record)
I don't know how to copy a file into an array and then copy its back to the file after sorting it as an array.
I want to copy a file into the array first then sort them by id number.Then copy it back to the original file as a sorted one.
(my file is a student record)
I don't know how to copy a file into an array and then copy its back to the file after sorting it as an array.
That means you have to make an array of record , the same record of the file .
Open the file
read it until the end , and add it to the array
the best way here is Insert sort i think.
Open the file
read it until the end , and add it to the array
the best way here is Insert sort i think.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This is Max-Sort which will take O(n^2).
There are algorithms with complexity of O(n*logn), such as merge sort.
There are algorithms with complexity of O(n*logn), such as merge sort.
Beside Max-Sort, you also have Quick-sort (much more efficient than former), but QS is too complex to be easily understood.
Anyway, to sort a student's record file of, let's say, no more than a couple of hundreds of records, that sort procedure is good enough to do its job.
After all, Der_23 is not asking for the OPTIMAL process... ;)
Anyway, to sort a student's record file of, let's say, no more than a couple of hundreds of records, that sort procedure is good enough to do its job.
After all, Der_23 is not asking for the OPTIMAL process... ;)
OK.
Actually Quick-Sort is less efficient than merge-sort for "worse case".
However, in *most* cases, it is faster.
Actually Quick-Sort is less efficient than merge-sort for "worse case".
However, in *most* cases, it is faster.
"... but QS is too complex to be easily understood. " - understood by whom?
Just for the record :-
1) Sort-Merge cannot be bettered.
2) Quick-Sort can achieve Sort-Merge in performance
3) Quick Sort is very easy to implement in Pascal and arrays
4) The sort proposed here (which is TOTALLY adequate for the job) is Bubble Sort
Big Rat:
A recursive procedure such as Q.S. is difficult enough for anybody who asks for help to sort a file, ¿don't you agree?.
I suggested that bubble sort (to be true, that's known as "false bubble sort") because, as you well state, is totally adequate to solve this problem.
A recursive procedure such as Q.S. is difficult enough for anybody who asks for help to sort a file, ¿don't you agree?.
I suggested that bubble sort (to be true, that's known as "false bubble sort") because, as you well state, is totally adequate to solve this problem.
ASKER
thank you