-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathselectionSort.java
More file actions
36 lines (33 loc) · 993 Bytes
/
Copy pathselectionSort.java
File metadata and controls
36 lines (33 loc) · 993 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
import java.util.Scanner;
public class selectionSort {
public static void main(String[] args) {
int n, temp, k = 0;
Scanner inp = new Scanner(System.in);
System.out.println("Enter number of elements");
n = inp.nextInt();
int[] arr = new int[n];
System.out.println("Enter elements");
for (int i = 0; i < n; i++) {
arr[i] = inp.nextInt();
}
for (int i = 0; i < n - 1; i++) {
boolean swapped = true;
temp = arr[i];
for (int j = 1 + i; j < n; j++) {
if (temp > arr[j]) {
temp = arr[j];
k = j;
swapped = false;
}
}
if (!swapped) {
arr[k] = arr[i];
arr[i] = temp;
}
}
for (int item : arr
) {
System.out.print(item + " ");
}
}
}