Created
July 13, 2016 07:01
-
-
Save mitulmanish/90e967a95e8add079c07101a2fe6e43e to your computer and use it in GitHub Desktop.
Selection Sort
This file contains hidden or 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
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