forked from raman1200/community_issues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection_sort.java
37 lines (30 loc) · 1.03 KB
/
Selection_sort.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
public class SelectionSort {
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
// Find the index of the minimum element in the unsorted part of the array
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the first element in the unsorted part
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
System.out.println("Original Array:");
for (int value : arr) {
System.out.print(value + " ");
}
selectionSort(arr);
System.out.println("\nSorted Array:");
for (int value : arr) {
System.out.print(value + " ");
}
}
}