ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2024 ICSE Computer Science Specimen Paper



Share with a Friend

Solved 2024 ICSE Computer Science Specimen Paper

Class 10 - ICSE Computer Science Solved Specimen Papers

Selection Sort Program - ICSE 2024 Computer Science Specimen Paper

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

import java.util.Scanner; class SelectionSort{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int a[] = new int[10]; System.out.println("Enter 10 integers:"); for(int i = 0; i < a.length; i++) a[i] = Integer.parseInt(in.nextLine()); for(int i = 0; i < a.length - 1; i++){ int small = a[i]; int pos = i; for(int j = i + 1; j < a.length; j++){ if(small > a[j]){ small = a[j]; pos = j; } } int temp = a[i]; a[i] = small; a[pos] = temp; } System.out.print("Sorted array: "); for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } }

Output

 
 OUTPUT : 
Enter 10 integers:
5
4
8
3
9
2
7
10
1
6
Sorted array: 1 2 3 4 5 6 7 8 9 10