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
    // low is the first index of the array
    // high is the last index of the array
    // last is the index of the last element to be sorted

    var SortArray: array [low..high] of ItemType;
        Temp: ItemType;
        Pass: Integer;
        K: Integer;

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.
 

  1. Create a new unit.
  2. In the interface part of the new unit include the clause "uses stdctrls;"  This allows the use of TMemo in the declaration.
  3. Declare the following procedures and functions in the interface part of the new unit.
  4. In the implementation part of the new unit write the code for each of the functions and procedures.
  5. From the main unit, use the new unit.
  6. 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:
    1. 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;