-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection_sort.java
More file actions
38 lines (36 loc) · 1016 Bytes
/
Selection_sort.java
File metadata and controls
38 lines (36 loc) · 1016 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
import java.util.Arrays;
import java.util.Scanner;
public class Selection_sort {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter size of array- ");
int n=s.nextInt();
int ar[]=new int[n];
System.out.println("enter array- ");
for (int i = 0; i <n ; i++) {
ar[i]=s.nextInt();
}
selection(ar);
System.out.println("array after sorting is- "+ Arrays.toString(ar));
}
public static void selection(int ar[]){
for (int i = 0; i <ar.length ; i++) {
int min=min(ar,i);
swap(ar,i,min);
}
}
public static int min(int ar[],int k){
int min=k;
for (int i = k; i <ar.length ; i++) {
if (ar[i]<ar[min])
min=i;
}
return min;
}
public static void swap(int ar[],int i,int min){
int temp;
temp=ar[min];
ar[min]=ar[i];
ar[i]=temp;
}
}