-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.java
53 lines (47 loc) · 1.13 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
48
49
50
51
52
53
package sorting;
import java.util.Scanner;
/**
* This is a Selection sort example;
*
* input:[9, 8, 10, 5], then
* [5, 8, 10, 9]
* [5, 8, 9, 10] done.
*
* @author Guru
*
*/
public class SelectionSort {
public static void selectionSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
int min = i;
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] < nums[min]) {
min = j;
}
}
if (i != min) {
int tmp = nums[i];
nums[i] = nums[min];
nums[min] = tmp;
}
}
}
public static void displayResult(int[] ret) {
for (int element : ret) {
System.out.print(element + " ");
}
System.out.println();
}
public static void main(String[] args) {
System.out.println("*** Welcome to @codingbro's Selection Sort Test ***");
Scanner sc = new Scanner(System.in);
System.out.print("Input your integer array, leave each number by space: ");
String[] strs = sc.nextLine().split(" ");
int[] testArray = new int[strs.length];
for (int i = 0; i < strs.length; i++) {
testArray[i] = Integer.parseInt(strs[i]);
}
selectionSort(testArray);
displayResult(testArray);
}
}