-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathselectionsort.java
More file actions
47 lines (38 loc) · 1.01 KB
/
selectionsort.java
File metadata and controls
47 lines (38 loc) · 1.01 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
import java.util.Scanner;
public class selectionsort {
static void printarray(int [] array){
for(int i = 0;i<array.length;i++){
System.out.print(array[i]+"\t");
}
}
static void inputarray(int [] array){
Scanner sc = new Scanner(System.in);
for(int i=0; i<array.length; i++){
array[i] = sc.nextInt();
}
}
static void Selectionsort(int [] array){
for(int i=0;i<array.length-1;i++){
int smallest = i;
for(int j=i+1;j<array.length;j++){
if(array[smallest]>array[j]){
smallest = j;
}
}
int temp = array[smallest];
array[smallest]= array[i];
array[i] = temp;
}
printarray(array);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array --->");
int n = sc.nextInt();
int array [] = new int[n];
System.out.println("Enter the array --->");
inputarray(array);
Selectionsort(array);
sc.close();
}
}