KnowledgeBoat Logo
LoginJOIN NOW

Computer Applications

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.

Java

Java Arrays

ICSE Sp 2024

84 Likes

Answer

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] + " ");
        }
    }
}

Variable Description Table

Program Explanation

Output

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

Answered By

36 Likes


Related Questions