-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionSort.java
39 lines (31 loc) · 972 Bytes
/
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
import java.util.Scanner;
public class selectionSort {
private static void sort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int minIndex = i;
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] < nums[minIndex]) {
minIndex = j;
}
}
int temp = nums[minIndex];
nums[minIndex] = nums[i];
nums[i] = temp;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = in.nextInt();
System.out.println("Enter the elements of the array: ");
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
sort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
in.close();
}
}