How to do a bubble sort in Occam -
i trying program in occam, , think not easy understand because documentation not good. anyway, hope me.
i trying learn how make simple program. bubble sort in occam.
#include "course.module" [32]int x: int aux: bool flag: flag:= true aux:=0 --put values on array seq k=0 10 x[i] = -x[i] -- bubble sort while (flag) seq = 0 9 if x[i] > x[i+1] aux:= x[i] x[i]:= x[i+1] x[i+1] := aux flag:= false :
occam-pi indentation sensitive
a few hints in code may , courtesy of tio-ide may provide place online experimentation:
#include "course.module" proc main( chan byte keyboard, screen, error ) [32]int x: int aux: bool flag: seq -- ------------------------- boseq: seq = 0 31 -- seq used <-an-iterator-> x[i] := -- put values on array seq = 0 10 -- seq used <-an-iterator-> x[i] := -x[i] -- put values on array flag := true while ( flag ) -- while seq -- seq bubble sort seq = 0 10 -- seq used <-an-iterator-> if -- if x[i] > x[i+1] -- case: x[i] > x[i+1] seq -- seq of steps aux := x[i] -- 1 x[i] := x[i+1] -- 2 x[i+1] := aux -- 3 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- interesting [occam-pi] feature is, -- [occam-pi] can _swap_ in-place -- using x[i], x[i+1] := x[i+1], x[i] true -- otherwise: skip -- seq used above <-an-iterator-> got exhausted flag := false -- set false -- ----------------------------- eoseq: : -- main()
Comments
Post a Comment