Watch selection sort find the minimum and swap it to the front on every pass. Worked example on 29 10 14 37 13 with the code, O(n²) time and O(1) extra space.
About this algorithm
Selection sort puts a list in order by repeatedly finding the smallest value that is not yet placed and swapping it to the front of the unsorted part. It always takes O(n²) time, however the input is arranged, because every pass scans the whole unsorted part, but it never makes more than n - 1 swaps. After the first pass the smallest value is in slot 0, after the second the next smallest is in slot 1, and so on.
Selection sort earns its place when writing to memory is much more expensive than reading, as on flash storage, because it performs at most one swap per position. For small lists it is easy to write and easy to reason about. Insertion sort is usually the better simple sort: it is stable and finishes in linear time on nearly sorted input, while selection sort scans the whole remainder every pass no matter what. For large inputs use merge sort, quick sort or heap sort. Heap sort is the same idea, take the extreme value and put it in place, with a heap that finds that value in log n steps instead of a full scan.
The sorted part starts empty at the left. Position i marks where the next smallest value has to go, starting at 0.
Assume the value at i is the smallest, and remember its index as minIdx. Walk j from i + 1 to the end; whenever the value at j is smaller than the value at minIdx, set minIdx to j.
If minIdx is not i, swap the two values. Position i is now final and will never be looked at again. If the minimum was already at i, skip the swap.
Increase i by one and scan again. After n - 1 passes the last value is the largest left over, so it is already in place.
This page opens on 40 random bars, so type 29,10,14,37,13 into the Custom box to follow along. Pass 1 starts with 29 as the smallest, finds 10 at index 1, and nothing after it is smaller, so 10 and 29 swap: 10 29 14 37 13. Pass 2 starts at 29, finds 14 and then 13, and swaps 13 into index 1: 10 13 14 37 29. Pass 3 finds that 14 is already the smallest of what is left, so no swap happens. Pass 4 compares 37 with 29 and swaps them: 10 13 14 29 37. That is 4 + 3 + 2 + 1 = 10 comparisons and 3 swaps. A sorted input of the same length would still take 10 comparisons; only the swaps would drop to zero.
Equal values can change their order. Sort 5a 5b 2: the first pass swaps 2 with 5a, giving 2 5b 5a, so the two fives have traded places. If you sort records by one field and need ties to keep their earlier order, use insertion sort or merge sort instead.
Time: pass i compares the value at minIdx with every one of the n - i - 1 values after it, which adds up to n(n - 1) / 2 comparisons on every input. Space: it sorts in place, needing only the indices i, j and minIdx.