Skip to content

Instantly share code, notes, and snippets.

@zmohling
Created October 11, 2018 14:15
Show Gist options
  • Save zmohling/2bbd37424ea05c2b685a6af33391daf5 to your computer and use it in GitHub Desktop.
Save zmohling/2bbd37424ea05c2b685a6af33391daf5 to your computer and use it in GitHub Desktop.
/**
* Iterative implementation of selection sort
*
* @param arr Array of ints to be sorted in nondecreasing order.
*/
public static void selectionSort(int[] arr)
{
if(arr == null) throw new NullPointerException();
if(arr.length == 0) throw new IllegalArgumentException();
if(arr.length == 1) return;
for (int i = 0; i < arr.length; i++)
{
int minimumIndex = i;
for (int j = i; j < arr.length; j++)
{
if (arr[j] < arr[minimumIndex])
minimumIndex = j;
}
int temp = arr[i];
arr[i] = arr[minimumIndex];
arr[minimumIndex] = temp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment