Pascal - Sort by merging- recursion

Ask a question




Here is a recursive procedure which can sort an array of n integers using the method of Merge sort

Procedure Sort_Merge (Var t : TAB; g, d : integer); 
Var
m, i, j, k : integer;
s : TAB;
Begin
If d > g Then
Begin
m := (g + d) Div 2;
Sort_Merge (t, g, m);
Sort_Merge (t, m + 1, d);

For i := m DownTo g Do
s[i] := t[i];

For j := m + 1 To d Do
s[d + m + 1 - j] := t[j];

i := g; j := d;
For k := g To d Do
Begin
If s[i] < s[j] Then
Begin
t[k] := s[i];
i := i + 1;
End
Else
Begin
t[k] := s[j];
j := j - 1;
End;
End;
End;
End;



Thanks to Zouari Lazhar for this tip.
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team