forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort
36 lines (29 loc) · 931 Bytes
/
SelectionSort
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
import java.util.Scanner;
public class SelectionSort_009 {// search for the smallest ele andswap it with 1st element
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int n = a.length;
System.out.println("Enter the No. of elements you want to sort : ");
int m = sc.nextInt();
int a[] = new int[m];
System.out.println("Enter the elements :");
for (int i = 0; i < m; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < m - 1; i++) {
int minInd = i;
for (int j = i; j < m; j++) {
if (a[j] < a[minInd]) {
minInd = j;
}
}
int temp = a[i];
a[i] = a[minInd];
a[minInd] = temp;
}
for (int e : a) {
System.out.print(e + " ");
}
sc.close();
}
}