Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dvt32/c006ecaf513309bc8fe1cc013810f86b to your computer and use it in GitHub Desktop.
Save dvt32/c006ecaf513309bc8fe1cc013810f86b to your computer and use it in GitHub Desktop.
ALGORITHMO #13 - Selection Sort Implementation
public class SelectionSort {
public static void selectionSort(int[] arr) {
int lastElementIndex = arr.length - 1;
for (int subarrayStartIndex = 0; subarrayStartIndex < lastElementIndex; ++subarrayStartIndex) {
int minElementIndex = subarrayStartIndex;
for (int i = subarrayStartIndex + 1; i < arr.length; ++i) {
if (arr[i] < arr[minElementIndex]) {
minElementIndex = i;
}
}
if (minElementIndex != subarrayStartIndex) {
int temp = arr[subarrayStartIndex];
arr[subarrayStartIndex] = arr[minElementIndex];
arr[minElementIndex] = temp;
}
}
}
public static void main(String[] args) {
int[] arr = new int[] { 89, 45, 68, 90, 29, 34, 17 };
selectionSort(arr);
for (int element : arr) {
System.out.print(element + " ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment