Like most sorting algorithms, it consists of two loops.
- The outer (first) loop states how many elements need to be sorted.
- Generally needs to run through all of the elements.
- Technically, one element is often left out.
- Sorting often compares two elements. You often leave an element out since you need to compare an element to the one next to it. You don’t sort the last element since you have noting to compare it to.
- If you have 20 elements to sort, by the time you have sorted the first 19, the 20th will be will in place.
- If you only need to sort say the first 5 elements, this loop can be limited to 5.
- The inner loop does the actual sorting and swapping.
- Search though the set for the smallest element.
- This is a Linear Search. We also did this twice earlier in the year with the Math Methods assignment.
- If you want to reverse the order, search for the largest element.
- Swap the first element with the smallest element.
- When repeated (the outer loop), use a smaller set of elements.
- Don’t include the first elements since they are already sorted and the smallest elements.
- Hint: this loop doesn’t start at 0.