Skip to content

Instantly share code, notes, and snippets.

@vicegd
Last active February 23, 2020 07:58
Show Gist options
  • Save vicegd/f28498e1252a4fc58f6672a9ec807657 to your computer and use it in GitHub Desktop.
Save vicegd/f28498e1252a4fc58f6672a9ec807657 to your computer and use it in GitHub Desktop.
Sorting algorithm: Direct selection method
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