ICSE Class 10 Computer Applications Question 29 of 30

Solved 2024 Question Paper ICSE Class 10 Computer Applications — Question 5

Back to all questions
5
Question

Question 7

Define a class pin code and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.

110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033

Answer
public class Pincode
{
    public static void main(String args[]) {
        int[] arr = {110061, 110001, 
                     110029, 110023, 
                     110055, 110006, 
                     110019, 110033};
                          
        for (int i = 0; i < 7; i++) 
        {
            int idx = i;
            for (int j = i + 1; j < 8; j++) 
            {
                if (arr[j] < arr[idx])
                    idx = j;
            }
            
            int t = arr[i];
            arr[i] = arr[idx];
            arr[idx] = t; 
        }
        
        System.out.println("Sorted Array:");
        for (int i = 0; i < 8; i++) 
        {
            System.out.print(arr[i] + " ");
        }

    }
}
Output
BlueJ output of Pincode.java