Skip to content

Instantly share code, notes, and snippets.

@mitulmanish
Created July 13, 2016 07:01
Show Gist options
  • Save mitulmanish/90e967a95e8add079c07101a2fe6e43e to your computer and use it in GitHub Desktop.
Save mitulmanish/90e967a95e8add079c07101a2fe6e43e to your computer and use it in GitHub Desktop.
Selection Sort
private static int [] selectionSort(int [] numbers) {
for(int i = 0; i < numbers.length - 1; i++) {
int currentSmallest = numbers[i];
boolean smallestFound = false;
int smallestCurrentIndex = i;
for (int j = i + 1; j < numbers.length; j++) {
if (numbers[j] < currentSmallest) {
smallestFound = true;
currentSmallest = numbers[j];
smallestCurrentIndex = j;
}
}
if (smallestFound) {
int temp = numbers[i];
numbers[i] = currentSmallest;
numbers[smallestCurrentIndex] = temp;
}
}
return numbers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment