-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionsort.java
More file actions
31 lines (31 loc) · 888 Bytes
/
Copy pathSelectionsort.java
File metadata and controls
31 lines (31 loc) · 888 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
import java.util.Scanner;
public class Selectionsort {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n, temp;
System.out.println("enter number of elements in array");
n = s.nextInt();
int ar[] = new int[n];
System.out.println("enter elements");
for (int i = 0; i < n; i++) {
ar[i] = s.nextInt();
}
for(int i=0;i<ar.length-1;i++)
{
int smallest=i;
for(int j=i+1;j<ar.length;j++)
{
if(ar[smallest]>ar[j])
{
smallest=j;
}
}
temp=ar[smallest];
ar[smallest]=ar[i];
ar[i]=temp;
}
for (int i = 0; i < n; i++) {
System.out.print(ar[i]);
}
}
}