Skip to content

Commit f5a4a2c

Browse files
committed
Selection Sorting
1 parent 95a4a53 commit f5a4a2c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.algorithm.sorting;
2+
3+
import java.util.Arrays;
4+
5+
public class SelectionSort {
6+
7+
public static void main(String[] args) {
8+
int array[] = {64,25,12,22,11};
9+
System.out.println("Before Sorting :: "+Arrays.toString(array));
10+
selectionSort(array);
11+
System.out.println("After Sorting :: "+Arrays.toString(array));
12+
}
13+
14+
public static void selectionSort(int array[]) {
15+
int n = array.length;
16+
for (int i = 0; i < n - 1; i++) {
17+
// Find the minimum element in unsorted array
18+
int minIndex = i;
19+
for (int j = i + 1; j < n; j++)
20+
if (array[j] < array[minIndex])
21+
minIndex = j;
22+
int temp = array[minIndex];
23+
array[minIndex] = array[i];
24+
array[i] = temp;
25+
}
26+
}
27+
28+
}

0 commit comments

Comments
 (0)