a) In what cases should Merge Sort be used? In what cases is it not efficient? Explain. (b) The following is an implementation of merge and mergeSort - Making sure to take note of the added print statement in merge, what would the following call in main print to the screen?   int[] a = {4, 3, 2, 1}; int[] temp = new int[a.length]; mergeSort(a, 0, a.length-1, temp);

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

(a) In what cases should Merge Sort be used? In what cases is it not efficient? Explain.
(b) The following is an implementation of merge and mergeSort - Making sure to take note of the added print statement in merge, what would the following call in main print to the screen?

 

int[] a = {4, 3, 2, 1};
int[] temp = new int[a.length];
mergeSort(a, 0, a.length-1, temp);

public static void merge (int [] a, int start, int mid, int end, int [] temp) {
int ptr1 = start;
int ptr2 = mid + 1;
int resPtr = start;
while (ptr1 <= mid && ptr2 <= end) {
System.out.println("" + a[ptr1] + "
+ a[ptr2]); // Note this line
if (a[ptr1] <= a[ptr2]){
temp[resPtr++] = a[ptr1++];
else temp[resPtr++] = a[ptr2++];
if (ptri <= mid)
for (int i = ptr1; i <= mid; i++)
temp[resPtr++] = a[i];
else
for (int i = ptr2; i <= end; i++)
temp[resPtr++] = a[i];
System.arraycopy (temp, start, a, start, end - start + 1);
} |/ merge
5
public static void mergeSort(int [] a, int start, int end, int [] temp) {
if (start < end) {
int mid (start + end) / 2;
mergeSort (a, start, mid, temp);
mergeSort (a, mid + 1, end, temp);
merge (a, start, mid, end, temp);
}
} // mergeSort
Transcribed Image Text:public static void merge (int [] a, int start, int mid, int end, int [] temp) { int ptr1 = start; int ptr2 = mid + 1; int resPtr = start; while (ptr1 <= mid && ptr2 <= end) { System.out.println("" + a[ptr1] + " + a[ptr2]); // Note this line if (a[ptr1] <= a[ptr2]){ temp[resPtr++] = a[ptr1++]; else temp[resPtr++] = a[ptr2++]; if (ptri <= mid) for (int i = ptr1; i <= mid; i++) temp[resPtr++] = a[i]; else for (int i = ptr2; i <= end; i++) temp[resPtr++] = a[i]; System.arraycopy (temp, start, a, start, end - start + 1); } |/ merge 5 public static void mergeSort(int [] a, int start, int end, int [] temp) { if (start < end) { int mid (start + end) / 2; mergeSort (a, start, mid, temp); mergeSort (a, mid + 1, end, temp); merge (a, start, mid, end, temp); } } // mergeSort
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY