| |||||||||||||||||||||
| |||||||||||||||||||||
|
Subroutines Description
Further explanation of this example: 'Mosel User Guide', Chapter 9 Functions and procedures
Source Files By clicking on a file name, a preview is opened at the bottom of this page.
qsort2.mos
(!******************************************************
Mosel User Guide Example Problems
=================================
file qsort2.mos
```````````````
Overloading of subroutines.
(c) 2008 Fair Isaac Corporation
author: Y. Colombani, 2001
*******************************************************!)
model "Quick sort 2"
parameters
LIM=50
end-parameters
forward procedure qsort(L:array(range) of integer)
(! Equivalent form of declaration:
declarations
procedure qsort(L:array(range) of integer)
end-declarations
!)
declarations
T:array(1..LIM) of integer
end-declarations
forall(i in 1..LIM) T(i):=round(.5+random*LIM)
writeln(T)
qsort(T)
writeln(T)
! Swap the positions of two numbers in an array
procedure swap(L:array(range) of integer,i,j:integer)
k:=L(i)
L(i):=L(j)
L(j):=k
end-procedure
! Main sorting routine
procedure qsort(L:array(range) of integer,s,e:integer)
v:=L((s+e) div 2) ! Determine a partitioning value
i:=s; j:=e
repeat ! Partition the array into two subarrays
while(L(i)<v) i+=1
while(L(j)>v) j-=1
if i<j then
swap(L,i,j)
i+=1; j-=1
end-if
until i>=j
! Recursively sort the two subarrays
if j<e and s<j then qsort(L,s,j); end-if
if i>s and i<e then qsort(L,i,e); end-if
end-procedure
! Start of the sorting process
procedure qsort(L:array(r:range) of integer)
qsort(L,getfirst(r),getlast(r))
end-procedure
end-model
| |||||||||||||||||||||
| © Copyright 2025 Fair Isaac Corporation. |