size : 1257
uploaded_on : Tue Aug 11 00:00:00 1998
modified_on : Wed Dec 8 14:03:08 1999
title : Insertion sort
org_filename : INSERT.PAS
author : Weasel
authoremail : weasel@holidayinfo.com
description : demonstrates insertion sort
keywords :
tested : BP 7.0
submitted_by : The CKB Crew
submitted_by_email : ckb@netalive.org
uploaded_by : nobody
modified_by : nobody
owner : nobody
lang : pas
file-type : text/plain
category : pascal-alg-sortsearch
__END_OF_HEADER__
{This program is a demonstration of the
I N S E R T I O N S O R T
alhorithm.
How it works:
This program examines every element and put's it on its correct
position within the elements already examined.
Written by Palfrader Peter (Weasel@holidayinfo.com) in 1998
for the Coder's Knowledge Base at http://www.netalive.org/ckb/
Released into Public Domain. Credits would be fine.
}
{$b-}
program sortdemo;
uses
crt;
const
NoElements = 20;
var
sortarray : array[0..NoElements-1] of integer;
procedure sort;
var
i,
j,
checkval : integer;
begin
for i:=1 to NoElements-1 do
begin
checkval:=sortarray[i];
j:=i;
while (j>0) and (sortarray[j-1]>checkval) do
begin
sortarray[j]:=sortarray[j-1];
dec(j);
end;
sortarray[j]:=checkval;
end;
end;
var
s : integer;
c : char;
begin
clrscr;
{ initialize data to be sorted }
randomize;
for s:=0 to NoElements-1 do sortarray[s]:=random(100);
{ display the unsorted array }
writeln;
write('Before: '); for s:=0 to NoElements-1 do write(sortarray[s],' '); writeln;
writeln;
{ sort it }
sort;
{ display the sorted version }
write('After : '); for s:=0 to NoElements-1 do write(sortarray[s],' '); writeln;
c:=readkey;
end.