(*
** selection sort implementation
*)

(* ****** ****** *)

staload _(*non*) = "prelude/DATS/array.dats"
staload _(*non*) = "prelude/DATS/array0.dats"

(* ****** ****** *)

staload "selsort.sats"

(* ****** ****** *)

implement{a}
selsort (A) = let
  fun loop (
    A: array0 a, n: size_t, i: size_t, imin: size_t, xmin: a
  ) : size_t =
    if i < n then
      if lte_selsort (xmin, A[i])
        then loop (A, n, i+1, imin, xmin) else loop (A, n, i+1, i, A[i])
      // end of [if]
    else imin // end of [if]
  fun sort (
    A: array0 a, n: size_t, i: size_t
  ) : void =
    if i < n then let
      val imin = loop (A, n, i+1, i, A[i])
      val tmp = A[i]
      val () = A[i] := A[imin]
      val () = A[imin] := tmp
    in
      sort (A, n, i+1)
    end (* end of [if] *)
  // end of [sort]
in
  sort (A, array0_size (A), 0)
end // end of [selsort]

(* ****** ****** *)

(* end of [selsort.dats] *)