0% found this document useful (0 votes)
2 views1 page

Prog 1

The document presents a Java implementation of the selection sort algorithm. It allows users to input an array of integers, sorts the array using selection sort, and then outputs the sorted array. The example provided demonstrates sorting an array of four integers: 2, 5, 7, and 10.

Uploaded by

Mr Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Prog 1

The document presents a Java implementation of the selection sort algorithm. It allows users to input an array of integers, sorts the array using selection sort, and then outputs the sorted array. The example provided demonstrates sorting an array of four integers: 2, 5, 7, and 10.

Uploaded by

Mr Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

1.

SELECTION SORT

import java.util.Scanner;
public class selectionSort{
public static void selectionSort(int[] arr){
int n = arr.length;
for (int i=0; i<n-1; i++) {
int minIndex = i;
for (int j=i+1; j<n; j++){
if (arr[j] < arr[minIndex]){
minIndex= j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
selectionSort(arr);
System.out.println("Sorted array:");
for(int num : arr){
System.out.print(num +" ");
}
scanner.close();
}
}

OUTPUT:
Enter the number of elements: 4
Enter the elements:
2 5 7 10
Sorted array:
2 5 7 10

You might also like