Skip to content

Commit

Permalink
Add an implementation of Selection Sort
Browse files Browse the repository at this point in the history
  • Loading branch information
jbranchaud committed Jul 19, 2018
1 parent 170910e commit d210b25
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Index.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ let expected_list = [1, 2, 3, 4, 5, 6, 7, 8, 9];

let sorted_list = InsertionSort.sort(unsorted_list);

Invariant.expect(sorted_list == expected_list);
Invariant.expect(sorted_list == expected_list);

let sorted_list_2 = SelectionSort.sort(unsorted_list);

Invariant.expect(sorted_list_2 == expected_list);
62 changes: 62 additions & 0 deletions src/SelectionSort.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Selection Sort
https://en.wikipedia.org/wiki/Selection_sort
/* a[0] to a[n-1] is the array to sort */
int i,j;
int n;

/* advance the position through the entire array */
/* (could do j < n-1 because single element is also min element) */
for (j = 0; j < n-1; j++)
{
/* find the min element in the unsorted a[j .. n-1] */

/* assume the min is the first element */
int iMin = j;
/* test against elements after j to find the smallest */
for (i = j+1; i < n; i++)
{
/* if this element is less, then it is the new minimum */
if (a[i] < a[iMin])
{
/* found new minimum; remember its index */
iMin = i;
}
}

if (iMin != j)
{
swap(a[j], a[iMin]);
}
}
*/

let sort = (items: list(int)) : list(int) => {
let j = ref(0);
let len = List.length(items);
let swappable_items = ref(items);

while (j^ < len - 1) {
let iMin = ref(j^);
let i = ref(j^ + 1);

while (i^ < len) {
let iValue = List.nth(swappable_items^, i^);
let iMinValue = List.nth(swappable_items^, iMin^);
if (iValue < iMinValue) {
iMin := i^;
};

i := i^ + 1;
};

if (iMin^ != j^) {
swappable_items := Util.swap(swappable_items^, j^, iMin^);
};

j := j^ + 1;
};

swappable_items^;
};

0 comments on commit d210b25

Please sign in to comment.