forked from AdityaSubrahmanyaBhat/hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.java
More file actions
39 lines (36 loc) · 855 Bytes
/
Copy pathselectionSort.java
File metadata and controls
39 lines (36 loc) · 855 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
package com.company;
import java.util.Arrays;
public class selectionSort {
public static void main(String[] args) {
int[] arr={4,2,1,3,7,5,6,9,8,10,13,12,11,15,16,14};
selection(arr);
System.out.println(Arrays.toString(arr));
}
static void selection(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
int j=arr.length-i-1;
int max=maxi(arr,0,j);
swap(arr,max,j);
}
}
static int maxi(int[] arr,int s,int e)
{
int max=0;
for(int i=s;i<=e;i++)
{
if(arr[i]>arr[max])
{
max=i;
}
}
return max;
}
static void swap(int[] arr,int f1,int f2)
{
int t=arr[f1];
arr[f1]=arr[f2];
arr[f2]=t;
}
}