- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathselection_sort.java
More file actions
Latest commit
49 lines (39 loc) · 1.33 KB
/
Copy pathselection_sort.java
File metadata and controls
49 lines (39 loc) · 1.33 KB
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
39
40
41
42
43
44
45
46
47
48
49
importjava.util.Scanner;
publicclassselection_sort {
publicstaticvoidmain(Stringargs[]) {
intnum;
try (Scanners = newScanner(System.in)) {
System.out.print("ENTER THE NUMBER OF ELEMENTS: ");
num = s.nextInt();
// Array input
int[] array = newint[num];
System.out.println("ENTER THE ELEMENTS OF ARRAY: ");
for (inti = 0; i < num; i++) {
array[i] = s.nextInt();
}
// Prining unsorted array
System.out.println("UNSORTED ARRAY: ");
for (inti = 0; i < num; i++) {
System.out.println(array[i]);
}
// selection sort
for (inti = 0; i < num - 1; i++) {
intsmallest = i;
for (intj = i + 1; j < num; j++) {
if (array[smallest] > array[j]) {
smallest = j;
}
}
// swapping
inttemp = array[smallest];
array[smallest] = array[i];
array[i] = temp;
}
// Prining sorted array
System.out.println("SORTED ARRAY: ");
for (inti = 0; i < num; i++) {
System.out.println(array[i]);
}
}
}
}