Last active
February 23, 2020 07:58
-
-
Save vicegd/f28498e1252a4fc58f6672a9ec807657 to your computer and use it in GitHub Desktop.
Sorting algorithm: Direct selection method
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
public void sort(int[] elements) { | |
int posMin; | |
for (int i = 0; i < elements.length-1; i++) { | |
posMin = findPosMin(elements, i); //O(n) | |
int temp = elements[i]; | |
elements[i] = elements[posMin]; | |
elements[posMin] = temp; | |
} | |
} | |
public int findPosMin(int[] elements, int firstElement) { | |
int value = Integer.MAX_VALUE; | |
int pos = Integer.MAX_VALUE; | |
for (int i = firstElement; i < elements.length; i++){ | |
if (elements[i] < value){ | |
value = elements[i]; | |
pos = i; | |
} | |
} | |
return pos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment