-
Notifications
You must be signed in to change notification settings - Fork 4
/
SelectionSort.java
44 lines (42 loc) · 1.18 KB
/
SelectionSort.java
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
import java.util.Scanner;
public class Selection
{
public static void main(String args[])
{
System.out.println("Selection Sort Technique!!!!!!!!!!!!!!!!!!!!!!!!");
System.out.println("=============================================");
Scanner sc=new Scanner(System.in);
//length input
System.out.println("Enter the No. of elements : ");
int x=sc.nextInt();
// declaring array
System.out.println("Enter the Elements of the array : ");
int a[]=new int[x];
for(int i=0;i<a.length;i++)
{
a[i]=sc.nextInt();
}
//now sorting
for(int i=0;i<a.length;i++)
{
int n=i;
for(int j=i+1;j<a.length;j++)
{
if(a[j] < a[n])
{
n=j;
}
}
int t=0;
t=a[n];
a[n]=a[i];
a[i]=t;
}
//now printing the soretd array
System.out.println("The sorted array : ");
for(int i=0;i<a.length;i++)
{
System.out.println(a[i]+" ");
}
}
}