forked from ezaz039/Hactoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
37 lines (30 loc) · 903 Bytes
/
Copy pathSelectionSort.java
File metadata and controls
37 lines (30 loc) · 903 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
package com.company;
import java.util.Arrays;
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 1};
selection(arr);
System.out.println(Arrays.toString(arr));
}
static void selection(int[] arr){
for (int i = 0; i < arr.length ; i++) {
int last = arr.length - i - 1;
int max = large(arr, 0, last);
swapping(arr, max, last);
}
}
static int large(int[] arr, int start, int last) { //function for finding out the largest number
int l = start;
for (int i = 1; i <= last; i++) {
if (arr[i] > arr[l]) {
l = i;
}
}
return l;
}
static void swapping(int[] arr, int first, int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}