Created
October 11, 2018 14:15
-
-
Save zmohling/2bbd37424ea05c2b685a6af33391daf5 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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