Skip to content

Commit 889b074

Browse files
committed
Update solutions
1 parent ddc641d commit 889b074

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* (Execution time) Write a program that creates an array of numbers from 1 to
3+
* 100,000,000 in ascending order. Display the execution time of invoking the
4+
* linearSearch method and the binarySearch method in Listings 7.6 and
5+
* 7.7 respectively. Display the execution time of invoking both searches for the following
6+
* numbers: 1; 25,000,000; 50,000,000; 75,000,000; 100,000,000. You can
7+
* use the following code template to obtain the execution time:
8+
* long startTime = System.nanoTime();
9+
* perform the task;
10+
* long endTime = System.nanoTime();
11+
* long executionTime = endTime − startTime;
12+
*
13+
* Created by Sven on 08/20/19.
14+
*/
15+
package Chapter07;
16+
17+
public class Exercise0716_Execution_time {
18+
public static void main(String[] args) {
19+
int[] list = new int[100000000];
20+
for (int i = 1; i < 100000000; i++) {
21+
list[i] = i;
22+
}
23+
24+
long startTime, endTime, executionTime;
25+
int[] searchNum = {1, 25000000, 50000000, 75000000, 100000000};
26+
long[] linearSearchTime = new long[searchNum.length];
27+
long[] binarySearchTime = new long[searchNum.length];
28+
for (int i = 0; i < searchNum.length; i++) {
29+
// linear search
30+
startTime = System.nanoTime();
31+
linearSearch(list, searchNum[i]);
32+
endTime = System.nanoTime();
33+
executionTime = endTime - startTime;
34+
linearSearchTime[i] = executionTime;
35+
36+
// binary search
37+
startTime = System.nanoTime();
38+
binarySearch(list, searchNum[i]);
39+
endTime = System.nanoTime();
40+
executionTime = endTime - startTime;
41+
binarySearchTime[i] = executionTime;
42+
}
43+
44+
System.out.println("Search Number | Linear Search | Binary Search");
45+
System.out.println("-------------------------------------------------");
46+
for (int i = 0; i < searchNum.length; i++) {
47+
System.out.printf("%-13d | %-13d | %-13d\n", searchNum[i], linearSearchTime[i], binarySearchTime[i]);
48+
}
49+
}
50+
51+
52+
/**
53+
* The method for finding a key in the list
54+
*/
55+
public static int linearSearch(int[] list, int key) {
56+
for (int i = 0; i < list.length; i++) {
57+
if (key == list[i])
58+
return i;
59+
}
60+
return -1;
61+
}
62+
63+
/**
64+
* Use binary search to find the key in the list
65+
*/
66+
public static int binarySearch(int[] list, int key) {
67+
int low = 0;
68+
int high = list.length - 1;
69+
70+
while (high >= low) {
71+
int mid = (low + high) / 2;
72+
if (key < list[mid])
73+
high = mid - 1;
74+
else if (key == list[mid])
75+
return mid;
76+
else low = mid + 1;
77+
}
78+
79+
return -low - 1; // Now high < low
80+
}
81+
82+
83+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* (Sort students) Write a program that prompts the user to enter the number of students,
3+
* the students’ names, and their scores and prints student names in decreasing
4+
* order of their scores. Assume the name is a string without spaces, use the Scanner’s
5+
* next() method to read a name.
6+
*
7+
* Created by Sven on 08/20/19.
8+
*/
9+
package Chapter07;
10+
11+
import java.util.Scanner;
12+
13+
public class Exercise0717_Sort_students {
14+
public static void main(String[] args) {
15+
Scanner input = new Scanner(System.in);
16+
System.out.print("Enter number of students: ");
17+
int numOfStudents = input.nextInt();
18+
String[] nameList = new String[numOfStudents];
19+
double[] scoreList = new double[numOfStudents];
20+
21+
for (int i = 0; i < numOfStudents; i++) {
22+
System.out.print("Enter student's name: ");
23+
nameList[i] = input.next();
24+
System.out.print("Enter " + nameList[i] + "'s score: ");
25+
scoreList[i] = input.nextDouble();
26+
}
27+
28+
// Bubble sort
29+
for (int i = 0; i < numOfStudents - 1; i++) {
30+
for (int j = 0; j < numOfStudents - i - 1; j++) {
31+
if (scoreList[j] < scoreList[j + 1]) {
32+
// Swap scoreList[j] and scoreList[j + 1]
33+
double scoreTemp = scoreList[j];
34+
scoreList[j] = scoreList[j + 1];
35+
scoreList[j + 1] = scoreTemp;
36+
37+
// Swap nameList[j] and nameList[j+1]
38+
String nameTemp = nameList[j];
39+
nameList[j] = nameList[j + 1];
40+
nameList[j + 1] = nameTemp;
41+
}
42+
}
43+
}
44+
45+
for (int i = 0; i < numOfStudents; i++) {
46+
System.out.printf("%-15s%-5.2f\n", nameList[i], scoreList[i]);
47+
}
48+
}
49+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* (Sort students) Write a program that prompts the user to enter the number of students,
3+
* the students’ names, and their scores and prints student names in decreasing
4+
* order of their scores. Assume the name is a string without spaces, use the Scanner’s
5+
* next() method to read a name.
6+
*
7+
* Created by Sven on 08/20/19.
8+
*/
9+
package Chapter07;
10+
11+
import java.util.Scanner;
12+
13+
public class Exercise0718_Bubble_sort {
14+
public static void main(String[] args) {
15+
Scanner input = new Scanner(System.in);
16+
System.out.print("Enter 10 double numbers: ");
17+
double[] numbers = new double[10];
18+
for (int i = 0; i < numbers.length; i++) {
19+
numbers[i] = input.nextDouble();
20+
}
21+
double[] sortedList = bubbleSort(numbers);
22+
for (int i = 0; i < sortedList.length; i++) {
23+
System.out.print(sortedList[i] + " ");
24+
}
25+
}
26+
27+
public static double[] bubbleSort(double[] list) {
28+
int n = list.length;
29+
for (int i = 0; i < n - 1; i++) {
30+
for (int j = 0; j < n - i - 1; j++) {
31+
if (list[j] > list[j + 1]) {
32+
// swap arr[j+1] and arr[i]
33+
double temp = list[j];
34+
list[j] = list[j + 1];
35+
list[j + 1] = temp;
36+
}
37+
}
38+
}
39+
return list;
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* (Sorted?) Write the following method that returns true if the list is already sorted
3+
* in decreasing order.
4+
* public static boolean isSorted(int[] list)
5+
* Write a test program that prompts the user to enter a list and displays whether
6+
* the list is sorted or not. Here is a sample run. Note that the first number in the
7+
* input indicates the number of the elements in the list. This number is not part
8+
* of the list.
9+
* Enter list: 8 10 1 5 16 61 9 11 1
10+
* The list is not sorted
11+
* Enter list: 10 21 11 9 7 5 4 4 3 1 1
12+
* The list is already sorted
13+
*
14+
* Created by Sven on 08/20/19.
15+
*/
16+
package Chapter07;
17+
18+
import java.util.Scanner;
19+
20+
public class Exercise0719_Sorted {
21+
public static void main(String[] args) {
22+
Scanner input = new Scanner(System.in);
23+
System.out.print("Enter list: ");
24+
int numberOfElements = input.nextInt();
25+
int[] numbers = new int[numberOfElements];
26+
for (int i = 0; i < numberOfElements; i++) {
27+
numbers[i] = input.nextInt();
28+
}
29+
30+
System.out.println(isSorted(numbers) ? "The list is already sorted" : "The list is not sorted");
31+
}
32+
33+
public static boolean isSorted(int[] list) {
34+
for (int i = 0; i < list.length - 1; i++) {
35+
if (list[i] < list[i + 1]) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}

0 commit comments

Comments
 (0)