-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSelectionSort.java
More file actions
50 lines (44 loc) · 934 Bytes
/
SelectionSort.java
File metadata and controls
50 lines (44 loc) · 934 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
43
44
45
46
47
48
49
50
import java.util.Scanner;
public class SelectionSort{
public static void main(String ar[]){
int []a=new int [5];
Scanner s=new Scanner(System.in);
System.out.print("Enter the number of elements:");
int n = s.nextInt();
System.out.println("Enter array elements:");
for(int i=0;i<n;i++)
a[i]=s.nextInt();
System.out.println("Array elements are as:");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
Sort ob=new Sort();
ob.selection(a);
System.out.println("Array after Sorting as follows:");
printa ob2=new printa();
ob2.printArray(a);
}
}
class Sort{
public void selection(int []a)
{
for(int i=0;i<a.length-1;i++)
{
int min = i;
for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[min])
min=j;
}
int temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
}
class printa{
void printArray(int []a){
int n=a.length;
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}