-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample_6_5.java
More file actions
36 lines (36 loc) · 1.16 KB
/
Example_6_5.java
File metadata and controls
36 lines (36 loc) · 1.16 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
package unit_06.Examples.Example_05;
import java.util.Scanner;
public class Example_6_5 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int[] grades = new int[4];
System.out.println("Imagine you have 4 students:");
for (int i = 0; i < grades.length; i++) {
System.out.println("Enter student " + (i + 1) + " grade: ");
grades[i] = in.nextInt();
}
System.out.println("Before sorting");
for (int grade : grades) {
System.out.print(grade + " , ");
}
System.out.println();
selectionSort(grades);
for (int grade : grades) {
System.out.print(grade + " , ");
}
}
private static void selectionSort(int[] arr){
int smallest, temp, len = arr.length;
for(int i = 1; i < len - 1; i++){
smallest = i;
for(int j = i + 1; j < len; j++){
if(arr[smallest] > arr[j]){
smallest = j;
}
}
temp = arr[smallest];
arr[smallest] = arr[i - 1];
arr[i - 1] = temp;
}
}
}