I have a problem with two kinds of sorts — Heap and Shell. I have some code in C and I need to try using the same commands (like when I use in C a 'for-loop', I want to use a 'for-loop' for Pascal as well) in the bodies of the procedures. I try, but the sorts don't work like they do in C. In C, all is working great with arrays of size 40000.
Code for ShellSort in C where parameter 'n' is a size of array:
shellSort(int n){
int shift,pom,j,i;
shift = n/2;
while(shifta>0){
for (i = shift; i < n; i += 1){
pom=arr[i];
j=i;
while((j>=shift) && (arr[j-shift]>pom)){
arr[j] = arr[j - shift];
j=j-shift;
}
arr[j]=pom;
}
shift= shift / 2;
}
}
Declaration in Pascal
arr :array[0..22] of integer;
size:integer =23;
Code in Pascal with the same parameters:
procedure ShellSort(n:integer);
Var
shift , pom : Integer;
Begin
shift:=n div 2 ;
While shift>0 Do
Begin
For i:=shift to n Do
Begin
pom:=arr[i];
j:=i;
While (j>=shift) and (arr[j-shift]>pom) Do
Begin
arr[j]:=arr[j-shift];
j:= j-shift
End;
arr[j]:=pom;
End;
shift:=shift div 2;
End;
End;
Code for Heap sort in C:
heapSort() {
int N = size;
int k;
int pom;
for (k = N / 2; k > 0; k--) {
downHeap(k, N);
}
do {
pom = arr[0];
arr[0] = arr[N - 1];
arr[N - 1] = pom;
N = N - 1;
downHeap(1, N);
} while (N > 1);
}
downHeap(int k, int N) {
int T = arr[k - 1];
while (k <= N / 2) {
int j = k + k;
if ((j < N) && (arr[j - 1] < arr[j])) {
j++;
}
if (T >= arr[j - 1]) {
break;
} else {
arr[k - 1] = arr[j - 1];
k = j;
}
}
arr[k - 1] = T;
}
Code for Heap sort in Pascal:
procedure downHeap(k:integer;N:integer);
var
T:integer;
begin
T:=arr[k-1];
while(k<= (N div 2)) do
begin
j:=k+k;
if ((j<N) and (arr[j-1]<arr[j])) then
begin
j:=j+1;
end;
if (T>=arr[j-1]) then
begin
exit;
end
else
begin
arr[k-1]:=arr[j-1];
k:=j;
end;
end;
arr[k-1]:=T;
end;
procedure heapSort;
var
n, pom:integer;
begin
n:=size;
for i:=n div 2 downto 1 do
begin
downHeap(i,n);
end;
repeat
begin
pom:=arr[0];
arr[0]:=arr[n-1];
arr[n-1]:=pom;
n:=n-1;
downHeap(1,n);
end;
until n>1;
end;
Output from Pascal:
Unsorted array
10 9 8 7 6 5 4 3 3 2 1 0 15 15 15 15 14 18 99 1 19 95 95
BubbleSort
0 1 1 2 3 3 4 5 6 7 8 9 10 14 15 15 15 15 18 19 95 95 99
HeapSort
95 95 15 18 95 15 15 15 18 19 95 0 5 4 15 3 14 7 3 1 2 1 99
ShellSort
0 0 1 1 2 3 3 4 5 7 14 15 15 15 15 15 18 18 19 95 95 95 95
Expected
0 1 1 2 3 3 4 5 6 7 8 9 10 14 15 15 15 15 18 19 95 95 99
I am really depressed.
Transferring comments into an answer.
In the Shell sort, the C loop is:
for (i = shift; i < n; i += 1){
and the Pascal loop is
For i:=shift to n Do
The Pascal loop is looping once too often; it needs to be:
For i := shift to n-1 Do
assuming expressions are allowed in loop limits — if not, you need an extra variable, roughly like this:
var ub: integer;
ub := n - 1;
For i := shift to ub Do
The C for loop is much more general and flexible than the Pascal for loop.
In the Heap sort, the C loop is:
do { … } while (N > 1);
and you've translated that to
repeat … until n > 1;
The terminating condition is wrong; until
is equivalent to while not
, so you need:
repeat … until n <= 1;
I've by no means scrutinized the whole of the Pascal code — and I have no Pascal compiler to test with — so there could be other issues to be resolved still, but these two should get you going. (Make sure you've checked all the occurrences of for
and repeat … until
for the problems outlined here.)