forked from sahilbansalweb/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
38 lines (29 loc) · 812 Bytes
/
SelectionSort.java
File metadata and controls
38 lines (29 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.*;
import java.util.Scanner;
class SelectionSort {
static void selectionSort(int arr[], int n){
for(int i = 0; i < n; i++){
int min_ind = i;
for(int j = i + 1; j < n; j++){
if(arr[j] < arr[min_ind]){
min_ind = j;
}
}
int temp = arr[i];
arr[i] = arr[min_ind];
arr[min_ind] = temp;
}
}
public static void main (String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++){
a[i]=scan.nextInt();
}
selectionSort(a, n);
for(int i = 0; i < n; i++){
System.out.print(a[i] + " ");
}
}
}