HOME

Project: ShellSort in FreePascal — Task Two

Introduction

add something here

General Notes

add something here

Source Code: FreePascal

add something here

program ShellSort_Example;

uses
  UShellSort;

const
  MaxNum = 100;

var
  a : array [0..MaxNum] of LongInt; 
  i : Integer;

procedure DisplayNumbers;

var 
  j : Integer;

begin
  for j := 1 to MaxNum do begin
    Write(a[j]:5);
    if (j mod 10) = 0 then WriteLn;
  end;
  WriteLn;
end;

begin
  Randomize; { This way we generate a new sequence every time
               the program is run}

  for i := MaxNum downto 1 do begin
    a[i] := Random(100) + 1;
  end;  
  a[0] := 1000;

  WriteLn;
  WriteLn('Example program to sort a list of numbers');
  WriteLn;

  WriteLn('Unsorted Numbers');
  DisplayNumbers();

  ShellSort(a);

  WriteLn('Sorted Numbers');
  DisplayNumbers();

  WriteLn('');
  WriteLn('Finished.');
end.

{ EOF }

  

.....

{ new stuff }

unit UShellSort;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

type
  TShellSortItem = integer;

procedure ShellSort(var a: array of TShellSortItem);

implementation

procedure ShellSort(var a: array of TShellSortItem);
var i, j, h, n, v : integer;
begin
  n := length(a);
  h := 1;
  repeat
    h := 3*h + 1
  until h > n;

  repeat
    h := h div 3;
    for i := h + 1 to n do begin
      v := a[i];
      j := i;
      while (j > h) and (a[j-h] > v) do begin
        a[j] := a[j-h];
        j := j - h;
      end;
      a[j] := v;
    end
  until h = 1;
end;

end.

(* vim: ts=3: nowrap: syntax=pascal: *)
  

Build Notes

add something here


~kc4zvw: fpc ushellsort.pas
Free Pascal Compiler version 3.2.2 [2022/06/23] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: FreeBSD for x86-64
Compiling ushellsort.pas
43 lines compiled, 0.2 sec
~kc4zvw:

  

Operation Notes

add something here

kc4zvw@www:~$ ./shellsort

Example program to sort a list of numbers

Unsorted Numbers
   54    6   33   12   35    6   27   54    5    7
    8   25   75   34   63   35   41    1   91   58
   78   99  100   77   88   49   68   81   26   62
   28   93   57   27   30   96   79   68   20   90
    5   96   21   61    3   80   21   73   10    1
   47   45   28   71   71   84   92   11   30   66
   36   67   77   38   13   77   97   82   84   30
   26   35   58   11   33   46   68   53   94   50
    2   63   62   93   66   86   74   34   55   62
   65   85   50    6    2   89   82    7   13   22

Sorted Numbers
    0    1    1    2    2    3    5    5    6    6
    6    7    7    8   10   11   11   12   13   13
   20   21   21   22   25   26   26   27   27   28
   28   30   30   30   33   33   34   34   35   35
   35   36   38   41   45   46   47   49   50   50
   53   54   54   55   57   58   58   61   62   62
   62   63   63   65   66   66   67   68   68   68
   71   71   73   74   75   77   77   77   78   79
   80   81   82   82   84   84   85   86   88   89
   90   91   92   93   93   94   96   96   97   99


Finished.
kc4zvw@www:~$

  

References

add something here



Copyright © 2023 by David Billsbrough (KC4ZVW)


Revised: Saturday, March 11, 2023 at 03:30:23 AM (EST)