-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSorted.java
More file actions
68 lines (64 loc) · 2.36 KB
/
Copy pathMergeSorted.java
File metadata and controls
68 lines (64 loc) · 2.36 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
58
59
60
61
62
63
64
65
66
67
68
/* Exercise 7.31
* (Merge two sorted lists) Write the following method that merges two sorted lists
into a new sorted list.
public static int[] merge(int[] list1, int[] list2)
Implement the method in a way that takes at most list1.length + list2.
length comparisons. Write a test program that prompts the user to enter two
sorted lists and displays the merged list. Here is a sample run. Note that the first
number in the input indicates the number of the elements in the list. This number
is not part of the list.
* Extra Credit - 2 points - extend the code done for 7.31 to create a new code file "MergeSortAll.java".
This extension of the code takes in one unsorted list and utilizes the merge method to sort the entire list.
Hint: try dividing the list in half, and each of those in half, and so on..
*/
public class MergeSorted {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Prompt user for input
System.out.print("Enter list1: ");
int[] list1 = new int[input.nextInt()];
for (int i = 0; i < list1.length; i++) {
list1[i] = input.nextInt();
}
System.out.print("Enter list2: ");
int[] list2 = new int[input.nextInt()];
for (int i = 0; i < list2.length; i++) {
list2[i] = input.nextInt();
}
int[] list3 = merge(list1, list2);
// Display the two lists
System.out.print("The merged list is");
for (int e: list3) {
System.out.print(" " + e);
}
System.out.println();
}
// Create a new list from the previously sorted list
public static int[] merge(int[] list1, int[] list2) {
int[] list3 = new int[list1.length + list2.length];
for (int i = 0; i < list1.length; i++)
list3[i] = list1[i];
for (int i = 0, j = list1.length; i < list2.length; i++, j++) {
list3[j] = list2[i];
}
sort(list3);
return list3;
}
// Sorting
public static void sort(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
int min = list[i];
int minIndex = i;
for (int j = i + 1; j < list.length; j++) {
if (list[j] < min) {
min = list[j];
minIndex = j;
}
}
if (minIndex != i) {
list[minIndex] = list[i];
list[i] = min;
}
}
}
}