Академический Документы
Профессиональный Документы
Культура Документы
1. SELECTION SORT
2. INSERTION SORT
3. BUBBLE SORT
4. QUICK SORT
5. MERGE SORT
{
This program demos various sorting algorithms, which includes:
Program Sorting_Algorithm;
Uses Crt;
Const f_in='INPUT.TXT';
f_out='OUTPUT.TXT';
Var arrValue, arrTmp, arrZ:Array[0..99] of Integer;
F:Text;
TotalElement: Integer;
{-------------------------------Begin Init---------------------------------}
Procedure Init;
Var i:Integer;
Begin
TextBackGround(LightGray);
ClrScr;
TextColor(Blue);
For i := 2 to 79 do
Begin
Gotoxy(i,1); Write(chr(205));
Gotoxy(i,49); Write(chr(205));
Gotoxy(i,8); Write(Chr(205));
End;
For i := 2 to 48 do
Begin
Gotoxy(1, i); Write(Chr(186));
Gotoxy(80, i); Write(Chr(186));
End;
Gotoxy(1,1); Write(Chr(201));
Gotoxy(80,1); Write(Chr(187));
Gotoxy(1,8); Write(Chr(204));
Gotoxy(80,8); Write(Chr(185));
Gotoxy(1,49); Write(Chr(200));
Gotoxy(80,49); Write(Chr(188));
Gotoxy(20,8); Write(Chr(209));
Gotoxy(20,49); Write(Chr(207));
For i := 9 to 48 do
Begin
Gotoxy(20,i); Write(chr(179));
End;
Gotoxy(25,2);
Write('SORTING ALGORITHMS DEMO PROGRAM');
Gotoxy(8,4);
TextColor(Red);
Write('Faculty of Information Technology - Hanoi University of Technology');
Gotoxy(30,6);
TextColor(Blue);
Write('Developer: Ha Minh Nam');
Gotoxy(5, 10);
Write('Algorithms:');
For i := 2 to 19 do
Begin
Gotoxy(i, 12); Write(Chr(196));
ENd;
TextColor(Red);
Gotoxy(3, 14); Write('1. SELECTION SORT');
Gotoxy(3, 16); Write('2. INSERTION SORT');
Gotoxy(3, 18); Write('3. BUBBLE SORT');
Gotoxy(3, 20); Write('4. QUICK SORT');
Gotoxy(3, 22); Write('5. MERGE SORT');
End;
{---------------------------End of Init------------------------------------}
{---------------------------Begin of OutPut--------------------------------}
Procedure OutPut(var max:Integer; blnMergeSort: boolean);
Var i, j:Integer;
Begin
For i := 10 to 48 do
Begin
For j := 24 to 79 do
Begin
Gotoxy(j,i);
Write(' ');
End;
End;
Gotoxy(24, 10);
Write('- Array before sorting: ');
Gotoxy(24,12);
TextColor(Red);
For i := 1 to max do
Begin
Write(arrTmp[i], ' ');
End;
TextColor(Blue);
Gotoxy(24, 15);
Write('- Array after sorting: ');
TextColor(Red);
Gotoxy(24,17);
{----------------------------------Begin of SELECTION_SORT----------------}
Procedure SELECTION_SORT(Var K:Array of Integer; numElement:Integer; direction:Integer);
Var i,j,m, X:Integer;
Begin
If direction = 1 then
Begin
For i := 1 to numElement - 1 do
Begin
m := i;
For j := i + 1 to numElement do
If K[j] < K[m] then m := j;
If (m <> i) Then
Begin
X := K[i];
K[i] := K[m];
K[m] := X;
End;
End;
End;
If direction = 0 then
Begin
For i := 1 to numElement - 1 do
Begin
m := i;
For j := i + 1 to numElement do
If K[j] > K[m] then m := j;
If (m <> i) Then
Begin
X := K[i];
K[i] := K[m];
K[m] := X;
End;
End;
End;
End;
{-----------------------------End of SELECTION_SORT------------------------}
{-----------------------------Begin of INSERTION_SORT----------------------}
Procedure INSERTION_SORT(Var K:Array of Integer; noe: Integer; dir:Integer);
Var i, j, X: Integer;
Begin
K[0] := -32500;
If dir = 1 then
Begin
For i := 2 to noe do
Begin
X := K[i];
j := i - 1;
While X < K[j] do
Begin
K[j+1] := K[j];
j := j - 1;
End;
K[j+1] := X;
End;
End;
End;
{-------------------------End of INSERTION_SORT----------------------------}
{-------------------------Begin of BUBBLE_SORT-----------------------------}
Procedure BUBBLE_SORT(Var K:Array of Integer; numOfEle: Integer; dir:Integer);
Var i, j, X: Integer;
Begin
If dir = 1 then
For i := 1 to numOfEle do
For j := numOfEle downto i + 1 do
If K[j] < K[j-1] Then
Begin
X := K[j];
K[j] := K[j-1];
K[j-1] := X;
End;
If dir = 0 then
For i := 1 to numOfEle do
For j := numOfEle downto i + 1 do
If K[j] > K[j-1] Then
Begin
X := K[j];
K[j] := K[j-1];
K[j-1] := X;
End;
Output(numOfEle, false);
End;
{------------------------End of BUBBLE_SORT--------------------------------}
{------------------------Begin of QUICK_SORT-------------------------------}
Procedure QUICK_SORT(Var K:Array of Integer; lowerBound: Integer; upperBound: Integer);
Var i, j, KEY, X, Y:Integer;
B: boolean;
Begin
B := true;
If (lowerBound < upperBound) Then
Begin
i := lowerBound;
j := upperBound + 1;
While B Do
Begin
i := i + 1;
While K[i] < KEY Do i := i + 1;
j := j - 1;
While K[j] > KEY Do j := j - 1;
If i < j then
Begin
X := K[i];
K[i] := K[j];
K[j] := X;
End
Else B := false;
End;
Y := K[lowerBound];
K[lowerBound] := K[j];
K[j] := Y;
End;
{------------------------End of QUICK_SORT---------------------------------}
{------------------------Begin MERGE_SORT----------------------------------}
{ This is the two-way merge, is used for recursive call from straight two-way merge sort }
Procedure MERGE_SORT(Var X:Array of Integer; b: Integer; m: Integer; n: Integer; Var Z:Array
of Integer);
Var i, j, k, s: Integer;
Begin
i := b;
k := b;
j := m + 1;
If i > m then
Begin
For s := j to n do
Begin
Z[k] := X[s];
k := k + 1;
End;
End
Else
Begin
For s := i to m do
Begin
Z[k] := X[s];
k := k + 1;
End;
End;
End;
{--------------------------End of MERGE_SORT-------------------------------}
{--------------------------Begin of MPASS----------------------------------}
Procedure MPASS(Var X:Array of Integer; Var Y:Array of Integer; n: Integer; l: Integer);
Var i, s:Integer;
Begin
i := 1;
If (i + l - 1 < n) Then
MERGE_SORT(X, i, i + l - 1, n, Y)
Else
Begin
For s := i to n do
Begin
Y[s] := X[s];
End;
End;
End;
{--------------------------End of MPASS------------------------------------}
{--------------------------Begin of STRAIGHT_MERGE_SORT--------------------}
Procedure STRAIGHT_MERGE_SORT(Var X:Array of Integer; n:Integer);
Var l: Integer;
Y: Array[0..99] of Integer;
Begin
l := 1;
While l < n do
Begin
MPASS(X, Y, n, l);
l := l + 1;
MPASS(Y, X, n, l);
l := l + 1;
End;
End;
{--------------------------End of STRAIGHT_MERGE_SORT----------------------}
{--------------------------Begin READ_FROM_FILE----------------------------}
Procedure READ_FROM_FILE;
Var Number, i:Integer;
Begin
Assign(F, f_in);
Reset(F);
i := 1;
{--------------------------Begin WRITE_TO_FILE-----------------------------}
Procedure WRITE_TO_FILE(Alg:Integer);
Var i: Integer;
Begin
Assign(F, f_out);
Rewrite(F);
Write(F, '-------- SORTING RESULT --------');
Writeln(F);
Write(F, '--- Algorithm used: ');
Case Alg of
1: Write(F, 'SELECTION SORT');
2: Write(F, 'INSERTION SORT');
3: Write(F, 'BUBBLE SORT');
4: Write(F, 'QUICK SORT');
5: Write(F, 'MERGE SORT');
End;
Writeln(F);
For i := 1 to TotalElement Do
Begin
Writeln(F, i, '-', arrValue[i]);
End;
Close(F);
End;
{--------------------------End of WRITE_TO_FILE----------------------------}
{--------------------------Begin Input-------------------------------------}
Procedure Input;
Var n:char;
num: Integer;
i: Integer;
a: Integer;
o: Integer;
Begin
Gotoxy(24, 10);
TextColor(Blue);
Write('- Import Values Manualy (N) or Automatically (A)?');
Readln(n);
If (n='N') or (n='n') then
Begin
Gotoxy(24, 12);
Write('- Number of elements in array N='); Readln(num);
For i := 1 to num do
Begin
Gotoxy(24, 12+2*i);
Write(' + Element ', i, ' = ');
Readln(arrValue[i]);
End;
READ_FROM_FILE;
Case a of
1: SELECTION_SORT(arrValue, TotalElement, 1);
2: INSERTION_SORT(arrValue, TotalElement, 1);
3: BUBBLE_SORT(arrValue, TotalElement, 1);
4: QUICK_SORT(arrValue, 1, TotalElement);
5: STRAIGHT_MERGE_SORT(arrValue, TotalElement);
End;
WRITE_TO_FILE(a);
Gotoxy(24, 14);
Write('Sorting completed. View file for result ');
End;
case a of
1: Begin
SELECTION_SORT(arrValue, num, o);
Output(num, false);
End;
2: Begin
INSERTION_SORT(arrValue, num, o);
Output(num, false);
End;
3: Begin
BUBBLE_SORT(arrValue, num, o);
Output(num, false);
End;
4: Begin
QUICK_SORT(arrValue, 1, num);
Output(num, false);
End;
5: Begin
STRAIGHT_MERGE_SORT(arrValue, num);
OutPut(num, false);
End;
end;
End;
{-----------------------------End of Input---------------------------------}
{-----------------------------Main Program---------------------------------}
BEGIN
Init;
Input;
Readln;
END.
{-----------------------------See you again !------------------------------}
{-------------------------I love computer and programming------------------}
Programming is the Art
$M 65520 0 655360}
program SortingAlgorithmsDemo;
uses crt;
const
max = 15000;
maxV = 15000;
Interval = 1193180 / 65536;
nMenu = 12;
SMenu: array[0..nMenu] of String =
(
' 0. Display Input',
' 1. Selection Sort',
' 2. Bubble Sort',
' 3. Insertion Sort',
' 4. Insertion Sort with binary searching',
' 5. Shell Sort',
' 6. Quick Sort',
' 7. Heap Sort',
' 8. Distribution Counting',
' 9. Radix Sort',
' 10. Straight Radix Sort',
' 11. Merge Sort',
' 12. Exit'
);
type
TArr = array[0..max + 1] of Integer;
var
k: TArr;
n: Integer;
selected: Integer;
StTime: LongInt;
Time: LongInt absolute 0:$46C;
procedure Enter;
var
f: Text;
begin
Assign(f, 'SORT.INP'); Reset(f);
n := 0;
while not SeekEof(f) do
begin
Inc(n); Read(f, k[n]);
end;
Close(f);
StTime := Time;
end;
procedure PrintInput;
var
i: Integer;
begin
Enter;
for i := 1 to n do Write(k[i]:8);
Write('Press any key to return to menu...');
ReadKey
end;
procedure PrintResult;
var
f: Text;
i: Integer;
ch: Char;
begin
Writeln('During Time = ', (Time - StTime) / Interval:1:10, ' (s)');
Assign(f, 'SORT.OUT'); Rewrite(f);
for i := 1 to n do Writeln(f, k[i]);
Close(f);
Write('Press <P> to print Output, another key to return to menu...');
ch := ReadKey; Writeln(ch);
if Upcase(ch) = 'P' then
begin
for i := 1 to n do Write(k[i]:8);
Writeln;
Write('Press any key to return to menu...');
ReadKey;
end;
end;
begin
Enter;
Partition(1, n);
PrintResult;
end;
begin
Enter;
for r := n shr 1 downto 1 do Adjust(r, n);
for i := n downto 2 do
begin
Swap(k[1], k[i]);
Adjust(1, i - 1);
end;
PrintResult;
end;
begin
Enter;
for i := 0 to MaxBit do MaskBit[i] := 1 shl i;
maxValue := k[1];
for i := 2 to n do
if k[i] > MaxValue then maxValue := k[i];
i := 0;
while (i < MaxBit) and (MaskBit[i + 1] <= MaxValue) do Inc(i);
Partition(1, n, i);
PrintResult;
end;
begin
Enter;
Flag := True;
for p := 0 to nDigit - 1 do
begin
if Flag then DCount(k, t, p)
else DCount(t, k, p);
Flag := not Flag;
end;
if not Flag then k := t;
PrintResult;
end;
begin
Enter;
len := 1; Flag := True;
while len < n do
begin
if Flag then MergeByLength(k, t, len)
else MergeByLength(t, k, len);
len := len shl 1;
Flag := not Flag;
end;
if not Flag then k := t;
PrintResult;
end;
(*******************************************************************)
begin
repeat
selected := MenuSelect;
Writeln(SMenu[selected]);
case selected of
0: PrintInput;
1: SelectionSort;
2: BubbleSort;
3: InsertionSort;
4: AdvancedInsertionSort;
5: ShellSort;
6: QuickSort;
7: HeapSort;
8: DistributionCounting;
9: RadixSort;
10: StraightRadixSort;
11: MergeSort;
12: Halt;
end;
until False;
end.