0
Thanks

A few words of thanks would be greatly appreciated.

Pascal - Sort by merging- recursion





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.
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
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.

Published by . Latest update on by Olivia Long.

This document, titled "Pascal - Sort by merging- recursion," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).

0 Comments