Last active
July 27, 2020 21:13
-
-
Save dvt32/c006ecaf513309bc8fe1cc013810f86b to your computer and use it in GitHub Desktop.
ALGORITHMO #13 - Selection Sort Implementation
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 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