import java.util.Arrays;
public class Main {
public static void main
(String[] args
) {
// Example array of integers
int[] array = {5, 3, 8, 2, 1};
System.
out.
println("Sorted Array: " + Arrays.
toString(array
));
// Sorting the array using Selection Sort
selectionSort(array);
// Displaying the sorted array
System.
out.
println("Sorted Array: " + Arrays.
toString(array
)); }
// Implementation of Selection Sort algorithm
public static void selectionSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
// Assume the current index is the minimum
int minIndex = i;
// Find the index of the minimum element in the unsorted part of the array
for (int j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the element at the current index
int temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CgpwdWJsaWMgY2xhc3MgTWFpbiB7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAJCiAgICAgICAgLy8gRXhhbXBsZSBhcnJheSBvZiBpbnRlZ2VycwogICAgICAgIGludFtdIGFycmF5ID0gezUsIDMsIDgsIDIsIDF9OwogICAgICAgIAogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiU29ydGVkIEFycmF5OiAiICsgQXJyYXlzLnRvU3RyaW5nKGFycmF5KSk7CgogICAgICAgIC8vIFNvcnRpbmcgdGhlIGFycmF5IHVzaW5nIFNlbGVjdGlvbiBTb3J0CiAgICAgICAgc2VsZWN0aW9uU29ydChhcnJheSk7CgogICAgICAgIC8vIERpc3BsYXlpbmcgdGhlIHNvcnRlZCBhcnJheQogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiU29ydGVkIEFycmF5OiAiICsgQXJyYXlzLnRvU3RyaW5nKGFycmF5KSk7CiAgICB9CgogICAgLy8gSW1wbGVtZW50YXRpb24gb2YgU2VsZWN0aW9uIFNvcnQgYWxnb3JpdGhtCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgc2VsZWN0aW9uU29ydChpbnRbXSBhcnJheSkgewogICAgICAgIGludCBuID0gYXJyYXkubGVuZ3RoOwoKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG4gLSAxOyBpKyspIHsKICAgICAgICAgICAgLy8gQXNzdW1lIHRoZSBjdXJyZW50IGluZGV4IGlzIHRoZSBtaW5pbXVtCiAgICAgICAgICAgIGludCBtaW5JbmRleCA9IGk7CgogICAgICAgICAgICAvLyBGaW5kIHRoZSBpbmRleCBvZiB0aGUgbWluaW11bSBlbGVtZW50IGluIHRoZSB1bnNvcnRlZCBwYXJ0IG9mIHRoZSBhcnJheQogICAgICAgICAgICBmb3IgKGludCBqID0gaSArIDE7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgICAgIGlmIChhcnJheVtqXSA8IGFycmF5W21pbkluZGV4XSkgewogICAgICAgICAgICAgICAgICAgIG1pbkluZGV4ID0gajsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQoKICAgICAgICAgICAgLy8gU3dhcCB0aGUgZm91bmQgbWluaW11bSBlbGVtZW50IHdpdGggdGhlIGVsZW1lbnQgYXQgdGhlIGN1cnJlbnQgaW5kZXgKICAgICAgICAgICAgaW50IHRlbXAgPSBhcnJheVttaW5JbmRleF07CiAgICAgICAgICAgIGFycmF5W21pbkluZGV4XSA9IGFycmF5W2ldOwogICAgICAgICAgICBhcnJheVtpXSA9IHRlbXA7CiAgICAgICAgfQogICAgfQp9