/* Shuffle sort a.k.a. "cocktail sort" */

# ArraySort

Data >>
 A : Array
 Flag : Integer : 1 | 0
;

/* * */

ArraySort ->
Arg Array : ^&Array, 
    . : SHUFFLE_SORT

Begin = 0 ..
End = Array._ - 1 ..

Flag = 1 ..

Do while Flag = 1
 - LeftToRight 'Array Flag' Begin End 
 ' Flag = 0; Begin ++

 If Flag then
  - RightToLeft 'Array Flag' Begin End 
  ' Flag = 0; End --
 ;
;

/

/* Walking up the array.
   This function will move the greatest element 
   to the right bound of the array */

LeftToRight ArraySort ->
Arg A : ^&Data, Begin, End : Integer

Do Swapping 'A' i
Where i = Begin to End

/

/* Walking down the array.
   This function will move the lowest element 
   to the left bound of the array */

RightToLeft ArraySort ->
Arg A : ^&Data, Begin, End : Integer

Do Swapping 'A' i
Where i = End to Begin step -1

/

/* Swapping two neighbors */

Swapping LeftToRight, RightToLeft ->
Arg A : ^&Data, First : Integer

Second = First + 1

If A.A.First > A.A.Second then
 A.A.First = A.A.Second = A.A.First
 A.Flag = 1
;

/

/* * */

# >> ArraySort