-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathselectionSort.java
More file actions
34 lines (31 loc) · 752 Bytes
/
Copy pathselectionSort.java
File metadata and controls
34 lines (31 loc) · 752 Bytes
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
import java.util.Scanner;
public class SelectionSort {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int arrayLimit = scanner.nextInt();
int[] intArray = new int[arrayLimit];
for (int i = 0; i < arrayLimit; i++) {
intArray[i] = scanner.nextInt();
}
for (int lastIndex = intArray.length - 1; lastIndex > 0; lastIndex--) {
int large = 0;
for (int i = 1; i <= lastIndex; i++) {
if (intArray[i] > intArray[large]) {
large = i;
}
}
swap(intArray, large, lastIndex);
}
for (int i = 0; i < intArray.length; i++) {
System.out.println(intArray[i]);
}
}
public static void swap(int[] arr, int i, int j) {
if (i == j) {
return;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}