ICSE Class 10 Computer Applications Question 2 of 6

Solved 2011 Question Paper ICSE Class 10 Computer Applications — Question 2

Back to all questions
2
Question

Question 5

Write a program to input and sort the weight of ten people. Sort and display them in descending order using the selection sort technique.

Answer
import java.util.Scanner;

public class KboatSelectionSort
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        double weightArr[] = new double[10];
        System.out.println("Enter weights of 10 people: ");
        for (int i = 0; i < 10; i++) {
            weightArr[i] = in.nextDouble();
        }
        
        for (int i = 0; i < 9; i++) {
            int idx = i;
            for (int j = i + 1; j < 10; j++) {
                if (weightArr[j] > weightArr[idx])
                    idx = j;
            }
            
            double t = weightArr[i];
            weightArr[i] = weightArr[idx];
            weightArr[idx] = t; 
        }
        
        System.out.println("Sorted Weights Array:");
        for (int i = 0; i < 10; i++) {
            System.out.print(weightArr[i] + " ");
        }
    }
}
Output
BlueJ output of KboatSelectionSort.java