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
BubbleSort(array);
// Displaying the sorted array
System.
out.
println("Sorted Array: " + Arrays.
toString(array
)); }
// Implementation of Selection Sort algorithm
public static void BubbleSort(int[] array) {
int n = array.length;
boolean swapped,
finished = false;
while(!finished) {
swapped = false;
for (int i = 0; i < n - 1; i++) {
if(array[i]>array[i+1]){
int tmp = array[i];
array[i] = array[i+1];
array[i+1] = tmp;
swapped = true;
}
}
if(swapped) n--;else finished = true;
}
}
}
aW1wb3J0IGphdmEudXRpbC5BcnJheXM7CgpwdWJsaWMgY2xhc3MgTWFpbiB7CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewoKICAgICAgICAvLyBFeGFtcGxlIGFycmF5IG9mIGludGVnZXJzCiAgICAgICAgaW50W10gYXJyYXkgPSB7NSwgMywgOCwgMiwgMX07CgogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiU29ydGVkIEFycmF5OiAiICsgQXJyYXlzLnRvU3RyaW5nKGFycmF5KSk7CgogICAgICAgIC8vIFNvcnRpbmcgdGhlIGFycmF5IHVzaW5nIFNlbGVjdGlvbiBTb3J0CiAgICAgICAgQnViYmxlU29ydChhcnJheSk7CgogICAgICAgIC8vIERpc3BsYXlpbmcgdGhlIHNvcnRlZCBhcnJheQogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiU29ydGVkIEFycmF5OiAiICsgQXJyYXlzLnRvU3RyaW5nKGFycmF5KSk7CiAgICB9CgogICAgLy8gSW1wbGVtZW50YXRpb24gb2YgU2VsZWN0aW9uIFNvcnQgYWxnb3JpdGhtCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgQnViYmxlU29ydChpbnRbXSBhcnJheSkgewoKICAgICAgICBpbnQgbiA9IGFycmF5Lmxlbmd0aDsKICAgICAgICBib29sZWFuIHN3YXBwZWQsCiAgICAgICAgICAgICAgICBmaW5pc2hlZCA9IGZhbHNlOwoKICAgICAgICB3aGlsZSghZmluaXNoZWQpIHsKICAgICAgICAgICBzd2FwcGVkID0gZmFsc2U7CiAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuIC0gMTsgaSsrKSB7CiAgICAgICAgICAgICAgaWYoYXJyYXlbaV0+YXJyYXlbaSsxXSl7CiAgICAgICAgICAgICAgaW50IHRtcCA9IGFycmF5W2ldOwogICAgICAgICAgICAgIGFycmF5W2ldID0gYXJyYXlbaSsxXTsKICAgICAgICAgICAgICBhcnJheVtpKzFdID0gdG1wOwogICAgICAgICAgICAgIHN3YXBwZWQgPSB0cnVlOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZihzd2FwcGVkKSBuLS07ZWxzZSBmaW5pc2hlZCA9IHRydWU7CiAgICAgICAgICB9CiAgICB9Cn0=