-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise07_17.java
More file actions
57 lines (46 loc) · 1.84 KB
/
Copy pathExercise07_17.java
File metadata and controls
57 lines (46 loc) · 1.84 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
/******************************************************************************
* This program takes the name of students and their grades and sorts them by
* their grade and displays the results. The method insertionSortPlusString
* sorts the numbers and uses the string as their index, or a name in this case.
******************************************************************************/
/**
*
* @author jorda
*/
public class Exercise07_17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int nStudents = input.nextInt();
double[] grades = new double[nStudents];
String[] names = new String[nStudents];
//Loop that creates the
for (int i = 0; i < names.length; i++) {
System.out.print("Enter a student name: ");
names[i] = input.next();
System.out.print("Enter a student score: ");
grades[i] = input.nextDouble();
}
insertionSortPlusString(grades, names);
for (int i = grades.length-1; i >= 0; i--) {
System.out.println(names[i] + " " + grades[i]);
}
}
//Sorts
public static void insertionSortPlusString(double[] array, String[] names){
for (int i = 1; i < array.length; i++) {
double temp = array[i];
String sTemp = names[i];
//move elements to the right until temp find it's spot
int j;
for (j = i-1; j >= 0 && temp < array[j]; j--) {
array[j+1] = array[j];
names[j+1] = names[j];
}
//insert temp into it's spot
array[j+1] = temp;
names[j+1] = sTemp;
}
}
}