
- •Оглавление
- •Постановка задачи
- •Входные и выходные данные.
- •Способ решения.
- •Укрупненная структура программы
- •Сортировка прямым выбором
- •Блок схема Сортировки прямым выбором
- •Сортировка пузырьком
- •Блок схема: сортировки пузырьком
- •Быстрая сортировка
- •Блок схема: быстрая сортировка.
- •Тестирование программы.
- •Анализ полученных результатов
- •Литература
- •Листинг программы.
Литература
Кормен Томас и другие Алгоритмы Построение и анализ. М., Вильямс. 2005
Мину М.Математическое программирование. Теория и алгоритмы М., Вильямс. 2004
Глухова Л.А. Электронный учебно-методический комплекс «Основы алгоритмизации и программирования»: Учеб. пособие. Часть 1. – Мн.: БГУИР, 2006.
Глухова Л.А. Электронный учебно-методический комплекс «Основы алгоритмизации и программирования»: Учеб. пособие. Часть 2. – Мн.: БГУИР, 2006.
Фаронов В.В. Турбо Паскаль 7.0. Начальный курс. Учеб. Пособие. – М.: Нолидж, 2007.
http://ru.wikipedia.org
http://alglib.sources.ru/
http://algolist.manual.ru/
Листинг программы.
program fff;
uses dos,graph,crt;
var
cik:integer;
y:real;
key:char;
flag:boolean;
t1,t2,tq,ts,tb:integer;
i,j,c,x,k,max,im,n:integer;
hour,hund,min,sec:word;
pquick,pselect,pbuble,srselect,srquick,srbuble:longint;
a,a1,a2:array [0..1000] of integer;
procedure swap(var a,b:integer);
var
c:integer;
begin
c:=a; {Замена двух чисел местами в массиве}
a:=b;
b:=c;
end;
procedure hello2(var n:integer);forward;
procedure hello; {Вывод текстовой информации }
var {первого окна программы}
gd,gm:integer;
begin
clrscr;
randomize;
gd:=detect;
initgraph(gd,gm,'c:\bp\bgi');
moveto(150,150);
outtext('Hello. This program demonstrates the work');
moveto(185,180);
outtext('of the three types of sorting:');
moveto(220,210);
textcolor(green);
outtext('Sort direct selection');
moveto(260,240);
textcolor(blue);
outtext('Sort buble');
moveto(260,270);
textcolor(yellow);
outtext('Sort quick');
moveto(250,450);
outtext('Press key Enter');
textcolor(black);
readln;
hello2(n);
end;
procedure hello2(var n:integer); {Вывод текстовой информации }
var {второго окна программы}
gd,gm:integer;
begin
n:=0;
clrscr;
randomize;
clrscr;
moveto(150,150);
outtext('Choose the size of the array');
moveto(140,180);
outtext('Press 1 and the dimension of the array will be 100:');
moveto(140,210);
outtext('Press 2 and the dimension of the array will be 500:');
moveto(140,240);
outtext('Press 3 and the dimension of the array will be 1000:');
moveto(150,450);
{outtext('Press ESC to return to the Hello page ');}
Repeat {Выбор размера массива}
key:=readkey;
case key of
'1': n:=100;
'2':n:=500;
'3':n:=1000;
end;
if ord(key)=27 then Hello;
until (key='1') or (key='2') or (key='3');
exit;
end;
procedure poriadok; forward;
procedure obrporiadok; forward;
procedure poluporiadok; forward;
procedure bezporiadok; forward;
procedure hello3; {Вывод текстовой информации }
var {третьего окна программы}
gd,gm:integer;
begin
clrscr;
randomize;
moveto(150,150);
outtext('Select sort order');
moveto(140,180);
outtext('Press 1 and direct sort');
moveto(140,210);
outtext('Press 2 and inverse sort');
moveto(140,240);
outtext('Press 3 and half sort');
moveto(140,270);
outtext('Press 4 and random sort');
moveto(150,450);
repeat {Выбор первоначального вида массива}
key:=readkey;
case key of
'1':poriadok ;
'2':obrporiadok;
'3':poluporiadok;
'4':bezporiadok;
end;
until (key='1') or (key='2') or (key='3') or (key='4');
exit;
end;
procedure cir(a,b,c:integer;q1,q2,q3:longint; s:string);{ Процедура вывода }
var {гистограмм на экран}
gd,gm:integer;
s1,s2,s3:string;
begin
randomize;
gd:=detect;
initgraph(gd,gm,'c:\bp\bgi');
moveto(300,50); {Вывод названия гистограммы}
outtext(s);
line(420,380,390,360);
line(390,360,390,400); {рисование стрелочек на графике}
line(390,400,420,380); {и их закраска}
setfillstyle(solidfill,white);
floodfill(395,395,white);
line(50,100,20,130);
line(20,130,80,130);
line(80,130,50,100);
setfillstyle(solidfill,white);
floodfill(25,128,white);
line(50,382,420,382); {рисование координатных прямых}
line(50,382,50,100);
rectangle(60,382,150,a);
setfillstyle(solidfill,green); {рисование первой гистограммы и закраска}
floodfill(65,a+1,white);
str(q1,s1);
moveto(90,a-20);
outtext(s1);
rectangle(150,382,240,b);
setfillstyle(solidfill,blue); {рисование второй гистограммы и её закраска}
floodfill(155,b+1,white);
str(q2,s2);
moveto(180,b-20);
outtext(s2);
rectangle(240,382,330,c);
setfillstyle(solidfill,yellow); {рисование третей гистограммы и её закраска}
floodfill(245,c+1,white);
str(q3,s3);
moveto(270,c-20);
outtext(s3);
rectangle(50,420,70,440);
setfillstyle(solidfill,blue);
floodfill(55,425,white); {вывод подписей под гистограммами}
moveto(80,430);
outtext('Sort buble');
rectangle(50,450,70,470);
setfillstyle(solidfill,yellow);
floodfill(55,465,white);
moveto(80,460);
outtext('Quicksort');
rectangle(50,390,70,410);
setfillstyle(solidfill,green);
floodfill(55,400,white);
moveto(80,400);
outtext('Sort direct selection');
readln;
clrscr;
end;
procedure qsort(l,r:integer); {быстрая сортировка}
var
x,i,j:integer;
begin
i:=l;
j:=r;
x:=a[l+random(r-l+1)];
while i<=j do begin
while(a[i]<x) do begin srquick:=srquick+1;inc(i);end;
while (a[j]>x) do begin dec(j); srquick:=srquick+1; end;
if (i<=j)then begin
swap(a[i],a[j]);
for cik:=1 to 50 do
y:=sqrt(2025.452);
pquick:=1+pquick;
inc(i);
dec(j);
end;
end;
if r>I then qsort(i,r);
if l<j then qsort(l,j);
end;
procedure poriadok; {Ввод трех массивов в прямом порядке}
var i:integer;
begin
for i:=1 to n do
begin
a[i]:=i;
a1[i]:=a[i];
a2[i]:=a[i];
end;
end;
procedure obrporiadok; {Ввод трех массивов в обратном порядке}
var i:integer;
begin
for i:=1 to n do
begin
a[i]:=-i+n+1;
a1[i]:=a[i];
a2[i]:=a[i];
end;
end;
procedure poluporiadok; {Ввод трех массивов в наполовину упорядоченных порядке}
var i:integer;
begin
for i:=1 to (n div 2) do
begin
a[i]:=i;
a1[i]:=a[i];
a2[i]:=a[i];
end;
for i:=(n div 2)+1 to n do
begin
a[i]:=-i+n+1;
a1[i]:=a[i];
a2[i]:=a[i];
end;
end;
procedure bezporiadok; {Ввод трех массивов случайным образом}
var i:integer;
begin
for i:=1 to n do
begin
a[i]:=random(100)+1;
a1[i]:=a[i];
a2[i]:=a[i];
end;
end;
procedure hello4;{надо для того чтобы пользователь знал что программа работает}
begin
clrscr;
moveto(180,200);
outtext('Please press Enter and wait');
end;
BEGIN
randomize;
hello;
hello3;
hello4;
gettime(hour,min,sec,hund);
t1:=hund+sec*100+6000*min;
srquick:=0;
pquick:=0;
qsort(1,n);
gettime(hour,min,sec,hund);
t2:=hund+sec*100+6000*min;
tq:=t2-t1;
if tq=0 then tq:=1;
gettime(hour,min,sec,hund);
t1:=hund+sec*100+6000*min;
srselect:=0;
pselect:=0;
k:=n;
for i:=1 to n-1 do
begin
max:=a1[1];
im:=1;
for j:=1 to k do
if a1[j]>max then
begin
for cik:=1 to 50 do
y:=sqrt(2025.452); {Сортировка прямым выбором}
max:=a1[j];
pselect:=1+pselect;
srselect:=1+srselect;
im:=j;
end
else srselect:=1+srselect;
a1[im]:=a1[k];
a1[k]:=max ;
dec(k);
end;
gettime(hour,min,sec,hund);
t2:=hund+sec*100+6000*min;
ts:=t2-t1;
if ts=0 then ts:=1;
gettime(hour,min,sec,hund);
t1:=hund+sec*100+6000*min;
srbuble:=0;
pbuble:=0;
repeat {Сортировка пузырьком}
flag:=true;
for i:=1 to n-1 do
begin
srbuble:=srbuble+1;
if a2[i]>a2[i+1] then
begin
for cik:=1 to 50 do
y:=sqrt(2025.452);
c:=a2[i];
a2[i]:=a2[i+1];
a2[i+1]:=c;
pbuble:=pbuble+1;
flag:=false;
end;
end;
until flag=true;
writeln(pselect,srselect:10);
gettime(hour,min,sec,hund);
t2:=hund+sec*100+6000*min;
tb:=t2-t1;
if tb=0 then tb:=1;
readln;
{Столько if-ов надо чтоб подсчитать масштаб вывода наших разных гистограмм}
{чтобы они не выходили за границы экрана }
{в тех частных случаях где это может произойти}
if pbuble=0 then cir(150,378,round(380-pquick/pselect*220),pselect,pbuble,pquick,'Count sort') else
if pbuble>pselect
then cir(round(380-pselect/pbuble*220),150,round(380-pquick/pbuble*220),pselect,pbuble,pquick,'Count sort')
else cir(150,round(380-pbuble/pselect*220),round(380-pquick/pselect*220),pselect,pbuble,pquick,'Count sort');
if srbuble=0 then cir(150,378,round(380-srquick/srselect*220),pselect,pbuble,pquick,'Count sort')
else if srbuble>srselect
then cir(round(380-srselect/srbuble*220),150,round(380-srquick/srbuble*220),srselect,srbuble,srquick,'Count srav')
else cir(150,round(380-srbuble/srselect*220),round(380-srquick/srselect*220),srselect,srbuble,srquick,'Count srav');
if tb>150 then cir(round(380-ts/tb*220),150,round(380-tq/tb*220),ts,tb,tq,'Time sort sec*0.01') else
if ts>150 then cir(150,round(380-tb/ts*220)-1,round(380-tq/ts*220)-1,ts,tb,tq,'Time sort sec*0.01') else
cir(379-ts,379-tb,379-tq,ts,tb,tq,'Time sort sec*0.01');
end.