|
1 | 1 | package com.codefortomorrow.intermediate.chapter10.solutions;
|
2 | 2 |
|
| 3 | +import java.util.Scanner; |
| 4 | + |
3 | 5 | /*
|
4 |
| - * Adapted from Exercise 8.5, |
5 |
| - * Introduction to Java Programming (Comprehensive) |
6 |
| - * by Y. Daniel Liang, 10th ed. |
7 |
| - * |
8 |
| - * Create a program called AddMatrices which |
9 |
| - * adds two 3x3 matrices and prints the result. |
10 |
| - * The program should prompt the user to enter |
11 |
| - * data for 2 matrices and then displays those |
12 |
| - * 2 matrices and the sum matrix. |
| 6 | + * Create a driver class called TestSelectionSort |
| 7 | + * which prompts the user to enter the length of |
| 8 | + * an array and the elements (doubles) in the array. |
| 9 | + * Then, your program should use selection sort to |
| 10 | + * sort the array in place, and then print the resulting array. |
13 | 11 | *
|
14 | 12 | * Example output:
|
15 | 13 | *
|
16 |
| - * Enter matrix1: 1 2 3 4 5 6 7 8 9 |
17 |
| - * Enter matrix2: 0 2 4 1 4.5 2.2 1.1 4.3 5.2 |
18 |
| - * The matrices are added as follows |
19 |
| - * 1.0 2.0 3.0 0.0 2.0 4.0 1.0 4.0 7.0 |
20 |
| - * 4.0 5.0 6.0 + 1.0 4.5 2.2 = 5.0 9.5 8.2 |
21 |
| - * 7.0 8.0 9.0 1.1 4.3 5.2 8.1 12.3 14.2 |
22 |
| - * |
23 |
| - * Hint: You may need to use the Double.parseDouble("a string") |
24 |
| - * to convert the user input to doubles. |
| 14 | + * Enter the length of the array: 5 |
| 15 | + * Enter the elements in the array: -1 236.3 2 6 0 |
| 16 | + * -1.0 0.0 2.0 6.0 236.3 |
25 | 17 | */
|
26 | 18 |
|
27 | 19 | public class TestSelectionSort {
|
28 | 20 |
|
29 | 21 | public static void main(String[] args) {
|
30 |
| - // write code here |
| 22 | + // Prompt user for length of array |
| 23 | + System.out.print("Enter the length of the array: "); |
| 24 | + Scanner input = new Scanner(System.in); |
| 25 | + int arrayLength = input.nextInt(); |
| 26 | + |
| 27 | + // Initialize array |
| 28 | + double[] list = new double[arrayLength]; |
| 29 | + |
| 30 | + // Prompt user for elements of the array |
| 31 | + System.out.print("Enter the elements in the array: "); |
| 32 | + for (int i = 0; i < arrayLength; i++) { |
| 33 | + list[i] = input.nextDouble(); |
| 34 | + } |
| 35 | + |
| 36 | + // goes up to index list.length - 1 |
| 37 | + // because you don't need to check last element |
| 38 | + // since it will be sorted already |
| 39 | + for (int i = 0; i < list.length - 1; i++) { |
| 40 | + double min = list[i]; |
| 41 | + int minIndex = i; |
| 42 | + |
| 43 | + // find the current smallest element |
| 44 | + for (int j = i + 1; j < list.length; j++) { |
| 45 | + if (list[j] < min) { |
| 46 | + min = list[j]; |
| 47 | + minIndex = j; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // swap it with whatever is in the |
| 52 | + // first position of unsorted array if needed |
| 53 | + if (minIndex != i) { |
| 54 | + double temp = list[i]; |
| 55 | + list[i] = min; |
| 56 | + list[minIndex] = temp; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // print the sorted array |
| 61 | + for (double n : list) { |
| 62 | + System.out.print(n + " "); |
| 63 | + } |
| 64 | + System.out.println(); // move cursor to next line |
| 65 | + |
| 66 | + input.close(); |
31 | 67 | }
|
32 | 68 | }
|
0 commit comments