Created
November 19, 2018 11:19
-
-
Save jp26jp/215f269ce60711e3e1b5bc07d929d4b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// start at the beginning of the array and iterate through to the second to last item | |
for (int currentIndex = 0; currentIndex < array.length - 1; currentIndex++) | |
{ | |
// assume the currentIndex is the smallest | |
int minIndex = currentIndex; | |
// set i to be the index immediately proceeding the currentIndex | |
for (int i = currentIndex + 1; i < array.length; i++) | |
{ | |
// if true, we found a smaller index | |
if (array[i] < array[minIndex]) | |
{ | |
// save the index of the smaller item | |
minIndex = i; | |
} | |
} | |
// if true, indexes are not the same so we can swap the items | |
if (minIndex != currentIndex) | |
{ | |
int temp = array[currentIndex]; | |
array[currentIndex] = array[minIndex]; | |
array[minIndex] = temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment