Link to home
Start Free TrialLog in
Avatar of der_23
der_23Flag for Thailand

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.
Avatar of der_23
der_23
Flag of Thailand image

ASKER

Please give a clear explaination for this question .(a sample program included would be highly appreciated)
thank you
Avatar of Inteqam
Inteqam

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.

ASKER CERTIFIED SOLUTION
Avatar of vikiing
vikiing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is Max-Sort which will take O(n^2).
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... ;)

OK.
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.