What do you mean "you cannot make it work"? It's full
program and it works just fine. Do you know how to
compile it? Or you still have problem understanding sorting?
The idea in any sorting algorythm is to:
1. go through all records,
2. compare and then
3. swap them where needed (three step swap - a,b,c).
Difference between sorting algorythms is only in loops.
Rest is the same. It doesn't matter if you are
sorting one array or multiple arrays.
Check the following procedure which was used in previous
post:
procedure sort_by_grade;
begin
for i:=1 to 4 do { (1) search all records }
for j:=i to 5 do
if class[i].grade<class[j].grade then { (2) compare grades }
begin { (3) swapp the record(s) }
temp:=class[i]; { A copy one variable to temp }
class[i]:=class[j]; { B copy second variable to first one }
class[j]:=temp; { C copy temp to second variable }
end;
end;
It is doing exactly what was mentioned in the begining:
It has
for loop(s) (
i and
j),
it has one comparison (
if), and it has swap
section (
begin ..... end;) with the famous
three step rotation (ABC). Remember this well since
we will use them later.
Ok let's do some spagetti code:
{ create arrays and one temp variable of same type for each array }
var grade : array[1..5] of real;
temp_grade : real;
name : array[1..5] of string;
temp_name : string;
last_name : array[1..5] of string;
temp_last_name : string;
smart : array[1..5] of boolean;
temp_smart : boolean;
i,j : byte; { these are needed for loops }
{ etc. }
for i:=1 to 4 do { (1) same loops as before }
for j:=i to 5 do
if grade[i]<grade[j] then { (2) same compare as before }
begin { (3) swap ALL records!!! }
temp_grade := grade[i]; { A1 remember ABC from swap thing above ?? }
temp_name := name[i]; { A2 }
temp_last_name := last_name[i]; { A3 }
temp_smart := smart[i]; { A4 }
;
;
grade[i] := grade[j]; { B1 }
name[i] := name[j]; { B2 }
last_name[i] := last_name[j]; { B3 }
smart[i] := smart[j]; { B4 }
;
;
grade[j] := temp_grade; { C1 }
name[j] := temp_name; { C2 }
last_name[j] := temp_last_name;{ C3 }
smart[j] := temp_smart; { C4 }
end;
If you still don't get it, try to find someone else to
help you since I'm quite exhausted...
Iby