Initializing help system before first use

Sort an array of numbers


Type: Programming
Rating: 2
Description: The idea of the Shell sort algorithm is to first sort, by straight insertion, small groups of numbers. Then several small groups are combined and sorted. This step is repeated until the whole list of numbers is sorted.
File(s): shsort.mos

shsort.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file shsort.mos 
   ``````````````` 
   Combining the 'repeat-until', 'while-do', and 
   'forall-do' loops.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001
*******************************************************!)

model "Shell sort"

 declarations
  N: integer                    ! Size of array ANum
  ANum: array(range) of real    ! Unsorted array of numbers
 end-declarations

 N:=50
 forall(i in 1..N)
  ANum(i):=round(random*100)

 writeln("Given list of numbers (size: ", N, "): ")
 forall(i in 1..N) write(ANum(i), " ")
 writeln

 inc:=1                         ! Determine the starting increment
 repeat                         
   inc:=3*inc+1
 until (inc>N)  
 
 repeat                         ! Loop over the partial sorts
   inc:=inc div 3
   forall(i in inc+1..N) do     ! Outer loop of straight insertion
     v:=ANum(i)
     j:=i
     while (ANum(j-inc)>v) do   ! Inner loop of straight insertion
       ANum(j):=ANum(j-inc)
       j -= inc
       if j<=inc then break; end-if
     end-do
     ANum(j):= v     
   end-do  
 until (inc<=1)
 
 writeln("Ordered list: ")
 forall(i in 1..N) write(ANum(i), " ")
 writeln
 
end-model