Dev-Pascal - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Dev-Pascal

spamservspamserv Posts: 2Member
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:ase.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

Comments

  • _Atex__Atex_ Posts: 163Member
    :
    : 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 ;-)[code]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;

    [/code]You just have to adapt it to your program...

  • spamservspamserv Posts: 2Member
    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;
  • _Atex__Atex_ Posts: 163Member
    : 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.
Sign In or Register to comment.