forked from Amigo2k20/java_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.java
More file actions
50 lines (44 loc) · 979 Bytes
/
selection_sort.java
File metadata and controls
50 lines (44 loc) · 979 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 Main
{
public static void Sort(int arr[])
{ int n=arr.length;
for(int j=0;j<n;j++){
int min=arr[j],minindex=j;
for(int i=j+1;i<n;i++){
if(arr[i]<min){
min=arr[i];
minindex=i;
}
}
int temp=arr[j];
arr[j]=arr[minindex];
arr[minindex]=temp;
}
}
public static void printarray(int a[])
{
for(int i=0; i < a.length; i++)
{
System.out.print(a[i]+" ");
}
}
public static void main(String[] args)
{
int n, res,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter "+n+" elements ");
for( i=0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.println( "elements in array ");
printarray(a);
Sort(a);
System.out.println( "\nelements after sorting");
printarray(a);
}
}