Concepts of Computer Science II
Sorting and Procedures
Insert Sort
The insert sort is something like filing note cards in a particular order.
You place the first one into the sort tray then for each one after that
you find its place within those previously sorted. The code for the
insert sort is in the following table:
declarations
|
code
for Pass := low+1 to Last do // start the first pass with
the second
// element of the array
begin
Temp := SortArray[Pass]; // save the item
to be inserted
K := Pass; // start comparing with those
items already inserted
While (K > low) and (Temp < SortArray[K-1]) do //assending
order
// proper location not found, move items down
and continue
begin
SortArray[K] := SortArray[K-1];
// move item down in array
K := K-1;
end; //
SortArray[K] := Temp; // place current item to be
inserted into
// its proper position
end; // for Pass
|
For this problem you will need a sample file to test your program.
Click on the link "test file" then save
the resulting file to your directory. Test
File
The purpose of this assignment is to continue the concept of sorting
and to increase your use of procedures in program development. To
do this you will declare, and code, several procedures and functions then
use them to perform the actions of events.
-
Create a new unit.
-
In the interface part of the new unit include the clause "uses stdctrls;"
This allows the use of TMemo in the declaration.
-
Declare the following procedures and functions in the interface part of
the new unit.
-
procedure ReadStrings(??? S: array of string; ??? N: integer; ???filename:
string);
-
procedure WriteString(??? S: array of string; ??? N: integer; ??? filename:
string);
-
procedure SortString(??? S: array of string; ??? N: integer);
-
procedure DisplayString(??? S: array of string; ??? N: integer; ??? M:
TMemo);
-
function SearchString(??? S: array of string; ??? N: integer; ??? Item:
string): integer;
-
function IsSorted(??? S: array of string; ??? N: integer): boolean
-
In the implementation part of the new unit write the code for each of the
functions and procedures.
-
From the main unit, use the new unit.
-
Implement a number of actions that would test the procedures. An
example of a procedure that you might have in your main unit would be:
procedure TMainForm.SortButtonClick(Sender: Object);
var Name: array [1..2000] of string;
Count: integer;
begin
if OpenDialog.execute then
begin
ReadString(Name, Count, OpenDialog.filename);
SortString(Name, Count);
if SaveDialog.execute then
WriteString(Name, Count, SaveDialog.filename);
DisplayString(Name, Count, DisplayMemo);
end;
end;