-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.java
More file actions
30 lines (28 loc) · 911 Bytes
/
selectionSort.java
File metadata and controls
30 lines (28 loc) · 911 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
import java.util.Scanner;
//selectionSort
public class selectionSort {
public static void main(String[] args) {
int n, i, j, temp;
try (Scanner sc = new Scanner(System.in)) {
n = sc.nextInt();
int[] arr = new int[n];
for (i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
for (i = 0; i < arr.length - 1; i++) {
int smallest = i;
for (j = i + 1; j < arr.length; j++) {
if (arr[smallest] > arr[j]) {
smallest = j;
}
}
temp = arr[smallest];
arr[smallest] = arr[i];
arr[i] = temp;
}
for (i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
}