-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSelectionSort.java
More file actions
42 lines (37 loc) · 853 Bytes
/
SelectionSort.java
File metadata and controls
42 lines (37 loc) · 853 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
35
36
37
38
39
40
41
42
package lecture5;
import java.util.Scanner;
public class SelectionSort {
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = takeinput();
selectionSort(arr);
display(arr);
}
public static void selectionSort(int[] arr) {
int n =arr.length;
for(int counter =0 ; counter <n ; counter++) {
int min = counter;
for(int j=counter +1 ; j<=n-1 ; j++) {
if(arr[j]<arr[min])
min=j ;
}
int temp = arr[min];
arr[min] = arr[counter];
arr[counter] = temp;
}
}
public static int[] takeinput() {
System.out.println("size?");
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
return arr;
}
public static void display(int[] a) {
for (int val : a) {
System.out.println(val);
}
}
}