Define a class to accept values in integer array of size 10. Sort them in an ascending order using selection sort technique. Display the sorted array.

ICSE Class 10 Computer Applications PYQ · 2024 Specimen · Section B

4Written · 2024 · Section B · 2024 Specimen

Define a class to accept values in integer array of size 10. Sort them in an ascending order using selection sort technique. Display the sorted array.

Show SolutionHide Solution↓
import java.util.Scanner;

public class KboatSelectionSort
{
    public static void main(String args[]) 
    {
        Scanner in = new Scanner(System.in);
        int arr[] = new int[10];
        System.out.println("Enter 10 integers: ");
        for (int i = 0; i < 10; i++) 
        {
            arr[i] = in.nextInt();
        }
        
        for (int i = 0; i < 9; i++) 
        {
            int idx = i;
            for (int j = i + 1; j < 10; 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 < 10; i++) 
        {
            System.out.print(arr[i] + " ");
        }
    }
}
BlueJ output of KboatSelectionSort.java
← Previous questionNext question →

Browse the free Computer Applications sample bank