-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_Sort.java
More file actions
53 lines (46 loc) · 1.04 KB
/
Selection_Sort.java
File metadata and controls
53 lines (46 loc) · 1.04 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
47
48
49
import java.util.Scanner;
public class Selection_Sort
{
void sort(int arr[])
{
int x;
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
x=i;
for (int j = i+1; j < n; j++)
{
if (arr[x] > arr[j])
{
x=j;
}
}
if (x!=i)
{
int temp= arr[x];
arr[x]= arr[i];
arr[i]=temp;
}
}
}
void print_array(int arr[])
{
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args)
{
Selection_Sort obj = new Selection_Sort();
int arr[]=new int[5];
Scanner s = new Scanner(System.in);
System.out.println("Enter the array to be sorted : ");
for (int a=0 ; a<5; a++)
{
arr[a]= s.nextInt();
}
obj.sort(arr);
obj.print_array(arr);
}
}