Skip to content

Instantly share code, notes, and snippets.

@agustarc
Created December 21, 2016 19:43
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 agustarc/ef9f4c659091f364a02d85f94ea906ed to your computer and use it in GitHub Desktop.
Save agustarc/ef9f4c659091f364a02d85f94ea906ed to your computer and use it in GitHub Desktop.
public class SelectionSort {
public static void selectionSort(int[] arr) {
final int length = arr.length;
for (int i = 0; i < length; i++) {
int min = i;
for (int j = i + 1; j < length; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
if (min == i) {
continue;
}
arr[min] = arr[min] + arr[i];
arr[i] = arr[min] - arr[i];
arr[min] = arr[min] - arr[i];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment