Skip to content

Instantly share code, notes, and snippets.

@jp26jp
Created November 19, 2018 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jp26jp/215f269ce60711e3e1b5bc07d929d4b4 to your computer and use it in GitHub Desktop.
Save jp26jp/215f269ce60711e3e1b5bc07d929d4b4 to your computer and use it in GitHub Desktop.
// 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