54
Question Question 48
Given the following array :
Write a program to sort the above array using exchange selection sort. Give the array status after every iteration.
public class KboatSelectionSort
{
public static void main(String args[]) {
int X[] = {13, 7, 6, 21, 35, 2, 28, 64, 45, 3, 5, 1};
int n = X.length;
for (int i = 0; i < n - 1; i++) {
int idx = i;
for (int j = i + 1; j < n; j++) {
if (X[j] < X[idx])
idx = j;
}
int t = X[i];
X[i] = X[idx];
X[idx] = t;
System.out.println("Pass : " + (i + 1));
for(int k = 0; k < n; k++) {
System.out.print(X[k] + " ");
}
System.out.println();
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(X[i] + " ");
}
}
}Output
