-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
52 lines (37 loc) · 1 KB
/
Copy pathSelectionSort.java
File metadata and controls
52 lines (37 loc) · 1 KB
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
48
49
50
51
52
package com.tozu.sorting;
public class SelectionSort {
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] <= arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void printArray(int[] arr) {
for (int value : arr) {
System.out.print(value + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] data = {33, 64, 25, 12, 22, 11};
int[] data2 = {104, 55, 78, 224, 5, 19, 34, 7};
System.out.println("Original array (1):");
printArray(data);
selectionSort(data);
System.out.println("Sorted array (1):");
printArray(data);
System.out.println("Original array (2):");
printArray(data2);
selectionSort(data2);
System.out.println("Sorted array (2):");
printArray(data2);
}
}