FICO Xpress Optimization Examples Repository
 FICO Optimization Community FICO Xpress Optimization Home

Subroutines

Description
• lcdiv2.mos: Recursive function calls
• primefct.mos: function returning a set
• qsort1.mos: 'forward' definition of subroutines
• shsortfct.mos: Function returning an array
• subrout.mos: Local and global declarations
Further explanation of this example: 'Mosel User Guide', Chapter 9 Functions and procedures

Source Files

qsort1.mos

```(!******************************************************
Mosel User Guide Example Problems
=================================

file qsort1.mos
```````````````
'forward' definition of subroutines.

(c) 2008 Fair Isaac Corporation
author: Y. Colombani, 2001
*******************************************************!)

model "Quick sort 1"

parameters
LIM=50
end-parameters

forward procedure qsort_start(L:array(range) of integer)

declarations
T:array(1..LIM) of integer
end-declarations

forall(i in 1..LIM) T(i):=round(.5+random*LIM)
writeln(T)
qsort_start(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_start(L:array(r:range) of integer)
qsort(L,getfirst(r),getlast(r))
end-procedure

end-model

```