1/*
2 * Copyright 2014, NICTA
3 *
4 * This software may be distributed and modified according to the terms of
5 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
6 * See "LICENSE_BSD2.txt" for details.
7 *
8 * @TAG(NICTA_BSD)
9 */
10
11unsigned len(unsigned *x)
12{
13  unsigned i;
14  for (i = 0; x[i]; i++);
15  return i;
16}
17
18/* lmin returns the index of the minimum element in the array, stopping when
19   the array element is zero (this one doesn't count) */
20unsigned lmin(unsigned *x)
21{
22  unsigned minsofar = x[0], mini = 0, i = 0;
23  while (x[i]) {
24    if (x[i] < minsofar) { minsofar = x[i]; mini = i; }
25    i++;
26  }
27  return mini;
28}
29
30unsigned* ssort(unsigned* x)
31{
32  unsigned sidx,i,t,lo;
33  unsigned ln;
34  unsigned mini;
35  ln = len(x);
36  if (ln < 2) return x;
37
38  for (sidx = 0 ; sidx < ln ; sidx++) {
39    mini = lmin(x + sidx);
40    lo = sidx + mini;
41    t = x[sidx];
42    x[sidx] = x[lo];
43    x[lo] = t;
44  }
45  return x;
46}
47