Is my mergesort implementation, correct?

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

Is my mergesort implementation, correct?

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
50
//Constructor copy from @arr to this.arr
MergeSort(String[] arr) {
//complete
this.arr = arr;
}
void mergeSort(int p, int r){
if (p < r) {
int q = (p+r)/2;
//call mergesort twice and merge
mergeSort (p+1, q);
mergeSort (q+1, r);
merge(p, q, r);
else return;
//merge subarray start index @p and finish index @r, and @q index of division
//update this.arr
void merge(int p, int q, int r){
int n1 = q - p + 1;
int n2 = r q;
//Arrays for each half
String[] 11 = new String [n1];
String[] r1= new String [n2];
//Fill in arrays for each half
for (int i = 0; i < l1.length; i++){
//careful start from p
}
//fill
for(int j = 0; j < this.arr.length/2; j++) 11[i] = arr[i];
}
for (int j = 0; j < r1.length; j++){
//fill
for(int k = 0; k < arr.length/2; j++) r1[j] = arr[k];
int i = 0;
int j = 0;
for (int k = p; k <= r; k++) {
if (i <= 11.length-1 && j <= r1. length-1){
if (11[i].compareTo (r1[j]) <= 0) {
this.arr[k] = 11[i];
i++;
}
else{
}
this.arr[k] = r1[j];
j++;
}
//Done with 11 and fill array "arr" with rest of elements in r1
alco
Transcribed Image Text: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 50 //Constructor copy from @arr to this.arr MergeSort(String[] arr) { //complete this.arr = arr; } void mergeSort(int p, int r){ if (p < r) { int q = (p+r)/2; //call mergesort twice and merge mergeSort (p+1, q); mergeSort (q+1, r); merge(p, q, r); else return; //merge subarray start index @p and finish index @r, and @q index of division //update this.arr void merge(int p, int q, int r){ int n1 = q - p + 1; int n2 = r q; //Arrays for each half String[] 11 = new String [n1]; String[] r1= new String [n2]; //Fill in arrays for each half for (int i = 0; i < l1.length; i++){ //careful start from p } //fill for(int j = 0; j < this.arr.length/2; j++) 11[i] = arr[i]; } for (int j = 0; j < r1.length; j++){ //fill for(int k = 0; k < arr.length/2; j++) r1[j] = arr[k]; int i = 0; int j = 0; for (int k = p; k <= r; k++) { if (i <= 11.length-1 && j <= r1. length-1){ if (11[i].compareTo (r1[j]) <= 0) { this.arr[k] = 11[i]; i++; } else{ } this.arr[k] = r1[j]; j++; } //Done with 11 and fill array "arr" with rest of elements in r1 alco
Expert Solution
Step 1

Program Approach:-

1. Create the Main class

2.Create the user-defined function whose name is merge(int arr1[], int l1, int m1, int r1).This function takes 4 parameters.

3.Create the user-defined function whose name is sort_1(int arr1[], int l1, int r1).This function takes 3 parameters.

4.Create the user-defined function whose name is print_Array(int arr1[]).This function takes one parameter.

5. In the main function 

a)Initialize the array 
b)Call the function

6. Display the output

steps

Step by step

Solved in 3 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