forked from csfx-py/hacktober2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
42 lines (35 loc) · 785 Bytes
/
SelectionSort.java
File metadata and controls
42 lines (35 loc) · 785 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
39
40
41
42
package sorting;
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter n: ");
n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter Array: ");
for(int i=0;i<n;i++) {
a[i] = sc.nextInt();
}
System.out.println("Array Before Sorting: ");
for(int i:a) {
System.out.print(i+" ");
}
for(int i=0;i<n-1;i++){
boolean sort = true;
int ind = i;
for(int j=0;j<n;j++) {
if(a[j]<a[ind]) {
ind = j;
}
}
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
System.out.print("Array After Sorting: ");
for(int i:a) {
System.out.println(i+" ");
}
}
}