Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Dev-Pascal Posted by spamserv on 23 Mar 2011 at 9:51 AM
Hello everyone,I'm new to programming,just started making my own database,and I have a task,using InsertionSort using ONLY while/for/if LOOP,to sort my database by last name,so I have this database:


const datoteka='d:\base.txt';
const maxUcenika = 100;

type ucenik=record
ime:string[20];
pre:string[30];
oc:1..5;
end;

var f:file of ucenik;
u:ucenik;
i,j,bruc:integer;
trazenoPrezime:string;
poljeUcenika:array[1..maxUcenika] of ucenik;

procedure upis;
begin
write('Koliko ucenika zelis upisati? --> '); readln(bruc);
for i:=1 to bruc do begin
writeln('ucenik broj ',i);
write('ime: '); readln(u.ime);
write('prezime: '); readln(u.pre);
write('ocjena: '); readln(u.oc);
write(f,u);
end;
end;

procedure ispisIzDatoteke;
begin
seek(f,0);
while not eof(f) do begin
read(f,u);
writeln(u.ime:20, u.pre:30, u.oc:10);
end;
end;

procedure ispisiZapis(n:integer);
begin
if ((n>=0) and (n<filesize(f))) then begin
seek(f,n);
read(f,u);
writeln(u.ime:20, u.pre:30, u.oc:10);
end
else begin
writeln('Broj mora biti izmedju 0 i ',filesize(f)-1);
end;
end;


and I have no idea how to use the insertion sort,I'm googleing it for like 2-3hours,read all the thing on the wiki/and that sort of pages,and now I'm desperate and I need your help :(

Thanks,sincerely
spamserv
Report
Re: Dev-Pascal Posted by _Atex_ on 25 Mar 2011 at 9:04 PM
:
: and I have no idea how to use the insertion sort,I'm googleing it
: for like 2-3hours,read all the thing on the wiki/and that sort of
: pages,and now I'm desperate and I need your help :(
:

After like 10 seconds my googleing showed up this
Pseudocode of the complete algorithm follows, where the arrays are zero-based and the for-loop includes both the top and bottom limits (as in Pascal):

insertionSort(array A)
 
{ This procedure sorts in ascending order. }
begin
    for i := 1 to length[A]-1 do
    begin
        value := A[i];
        j := i - 1;
        done := false;
        repeat
            { To sort in descending order simply reverse
              the operator i.e. A[j] < value }
            if A[j] > value then
            begin
                A[j + 1] := A[j];
                j := j - 1;
                if j < 0 then
                    done := true;
            end
            else
                done := true;
        until done;
        A[j + 1] := value;
    end;
end;

You just have to adapt it to your program...

Report
Re: Dev-Pascal Posted by spamserv on 26 Mar 2011 at 8:03 AM
lol if only i'm sorting numbers!
but,letters aren't numbers!
i've seen the code for insertion sort and you didn't help at all


procedure ucitajPoljeUcenika;
begin
i := 0;
seek(f,0);
while not eof(f) do
begin
read(f,u);
poljeUcenika[i] := u; {spremamo jedan zapis ucenika u varijablu poljeUcenika,indexa i,da bi mu kasnije mogli pristupiti}
i := i + 1; {povecavamo brojac}
end;
end;

procedure upisiPoljeUcenika(broj:integer); {nakon sortiranja po prezimenu,sortiramo nase polje,moramo ga zapisati natrag u datoteku}
begin
seek(f,0);
for i := 1 to broj do
begin
write(f,poljeUcenika[i]);
end;
end;


procedure sortirajPoPrezimenu(broj:integer); {broj= filesize(f)}
var
value:ucenik;
done:boolean; {varijabla koja moze biti true i false,reci ce nam jesmo li zavrsili sa sortiranjem ili treba jos nesto sortirati}
r:integer; {spremamo rezultat nase funkcije - usporediStringove}
begin
for i := 1 to broj do {poceli smo od DRUGOG zapisa/ucenika}
begin
value := poljeUcenika[i]; {cijeli zapis ucenika spremamo u varijablu 'value'}
j := i - 1; {j dobija vrijednost prvog zapisa,kasnije se povecava}
done := false;
repeat
r := compareText(poljeUcenika[j].pre, value.pre); {usporedjuje 2 stringa,ako je 1. string veci od drugoga,vratit ce vrijednost manju od nule}
if r > 0 then
begin
poljeUcenika[j + 1] := poljeUcenika[j]; {j+1=i}
j := j - 1;
if j < 0 then
done := true;
end
else
done := true;
until done;
poljeUcenika[j + 1] := value; {zamjena}
end;
end;
Report
Re: Dev-Pascal Posted by _Atex_ on 29 Mar 2011 at 9:21 PM
: lol if only i'm sorting numbers!
: but,letters aren't numbers!
: i've seen the code for insertion sort and you didn't help at all
:
:

Pascal lets you compare strings and "letters" too (not just numbers), so you could alphabetize using the same code with minimal modifications.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.