Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how do i sort a file?

Posted on 1998-11-19
8
Medium Priority
?
212 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:der_23
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

by:der_23
ID: 1216127
Please give a clear explaination for this question .(a sample program included would be highly appreciated)
thank you
0
 
LVL 7

Expert Comment

by:Inteqam
ID: 1216128
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.

0
 
LVL 3

Accepted Solution

by:
vikiing earned 400 total points
ID: 1216129
Let's supose this record structure:

record
    id: integer;
    name: string;
end

That's the record of your file. Then, you must define an array with THE SAME type of that, together an auxilliary variable for swapping during sort process. Of course, you must estimate an array size large enough to accomodate the whole file into it, this is, the number of elements of array must (at least) equals the number of records you have in your file.

To load the file into array, you can do:

Max:=0;     <== Integer variable to be used as array index
While Not Eof(chan) do begin
  Incr(Max); Read(chan, Arr[Max]);
end;

To sort the array:

For i:=1 to Max-1 do
   For j:=i+1 to Max do
      If Arr[j].Id < Arr[i].Id then begin    Swap elements
         Aux:=Arr[i];  Arr[i]:=Arr[j];  Arr[j]:=Aux;
      end;

To write back the array to file:

Seek(chan, 0);   <-- Point to beginning of file
For i:=1 to Max do Write(chan, Arr[i]);

0
[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1216130
This is Max-Sort which will take O(n^2).
There are algorithms with complexity of O(n*logn), such as merge sort.
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1216131
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... ;)

0
 
LVL 1

Expert Comment

by:FuzzyLogic
ID: 1216132
OK.
Actually Quick-Sort is less efficient than merge-sort for "worse case".
However, in *most* cases, it is faster.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 1216133

"... 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
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1216134
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.


0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Read on to get a few ideas on how to promote your next corporate event.
Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question