-
Notifications
You must be signed in to change notification settings - Fork 5
/
SelectionSort.java
47 lines (39 loc) · 1.47 KB
/
SelectionSort.java
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
import java.util.Arrays;
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {0, 6, 4, 88, 2, 1, 3, 5};
selectionSort(arr);
System.out.println(Arrays.toString(arr));
}
static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int last = arr.length - i - 1;
// Find the index of the maximum element in the unsorted part of the array.
int max = getMax(arr, 0, last);
// Swap the maximum element with the last element in the unsorted part.
if (arr[max] > arr[last]) {
swap(arr, max, last);
}
}
}
static void swap(int[] arr, int a, int b) {
// Swap elements at index 'a' and 'b' in the array.
// This can be done using temporary variable or without it as shown below:
// int temp = arr[a];
// arr[a] = arr[b];
// arr[b] = temp;
arr[a] = arr[a] + arr[b];
arr[b] = arr[a] - arr[b];
arr[a] = arr[a] - arr[b];
}
static int getMax(int[] arr, int start, int last) {
int max = start;
// Iterate through the array from 'start' to 'last' and find the index of the maximum element.
for (int i = start; i <= last; i++) {
if (arr[max] < arr[i]) {
max = i;
}
}
return max;
}
}