Given an array arr
of size n
, containing positive integers. You need to sort the elements of array using the Selection Sort algorithm.
Input Format
First line contains an integer n
which is the size of array arr
Second line contains n
space separated integers of arr
Output Format
Return the sorted array
Example 1
Input
5 4 1 3 9 7
Output
1 3 4 7 9
Explanation
The array after performing Selection sort: 1 3 4 7 9.
Example 2
Input
10 10 9 8 7 6 5 4 3 2 1
Output
1 2 3 4 5 6 7 8 9 10
Constraints
1 <= n
<= 104
-106 <= A[i] <= 106
Solution of Implementing Selection Sort in java:–
import java.util.*; import java.lang.*; import java.io.*; public class Main { public static void main (String[] args) throws java.lang.Exception { //your code here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int arr[]= new int[n]; for(int i=0;i<n;i++) { arr[i]=sc.nextInt(); } for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if(arr[i]>arr[j]) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } for(int i=0;i<n;i++) { System.out.print(arr[i]+" "); } } }
Add a Comment