ICSE Class 10 Computer Applications Question 54 of 71

Arrays — Question 54

Back to all questions
54
Question

Question 48

Given the following array :

13071622133542528664745839510111\underset{0}{\boxed{13}}\underset{1}{\boxed{7}}\underset{2}{\boxed{6}}\underset{3}{\boxed{21}}\underset{4}{\boxed{35}}\underset{5}{\boxed{2}}\underset{6}{\boxed{28}}\underset{7}{\boxed{64}}\underset{8}{\boxed{45}}\underset{9}{\boxed{3}}\underset{10}{\boxed{5}}\underset{11}{\boxed{1}}

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
BlueJ output of KboatSelectionSort.java
Answer