Question

In: Computer Science

. Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list...

. Implement a method that meets the following requirements:

(a) Calls mergesort to sort an array/list of at least 5 integers

(b) Prints the list before and after sorting.

Solutions

Expert Solution

As the language is not mentioned so I am solving this question in C++. I hope you won't mind.

code for merge_sort


void merge_list(int arr[],int start,int mid,int end){
int l1 = mid-start+1;
int l2 = end-mid;
  
//create temporary arrays to store values for left side and right side
int left[l1],right[l2];
  
//copy data from array to left and right array
for(int i = 0;i<l1;i++){
left[i] = arr[start+i]; // strore values from start to mid
}
for(int i = 0;i<l2;i++){
right[i] = arr[mid+i+1]; // store values from mid+1 to end in this array
}
  
// now writing code to merge these two arrays in the original array by comparison
  
int i = 0,j=0,k=start;
//code to merge both array
while(i<l1 and j<l2){
if(left[i]<=right[j]){ // checking if left side is greater or right side
arr[k++] = left[i++]; // if left side is smaller or equal to right array then assign it to arr ..... it is sorting in ascending fashion
}
else{
arr[k++] = right[j++];
}
  
}
  
// code for if left array is remaining
while(i<l1){
arr[k++] = left[i++];

}
// code for if right array is remaining
while(j<l2){
arr[k++] = right[j++];
}
  
}

code to print before sort

//print array before sort
void print_list_before_sort(int arr[],int n ){
cout<<"list before sort \n";
for(int i = 0;i<n;i++){
cout<<arr[i]<<" ";
}
}

code to print after sort

void print_list_after_sort_sort(int arr[],int n ){
cout<<"list after sort \n";
for(int i = 0;i<n;i++){
cout<<arr[i]<<" ";
}
}

complete merge sort code

#include <bits/stdc++.h>
using namespace std;
//print array before sort
void print_list_before_sort(int arr[],int n ){
cout<<"list before sort \n";
for(int i = 0;i<n;i++){
cout<<arr[i]<<" ";
}
}
//print array after sort
void print_list_after_sort_sort(int arr[],int n ){
cout<<"list after sort \n";
for(int i = 0;i<n;i++){
cout<<arr[i]<<" ";
}
}
void merge_list(int arr[],int start,int mid,int end){
int l1 = mid-start+1;
int l2 = end-mid;
  
//create temporary arrays to store values for left side and right side
int left[l1],right[l2];
  
//copy data from array to left and right array
for(int i = 0;i<l1;i++){
left[i] = arr[start+i]; // strore values from start to mid
}
for(int i = 0;i<l2;i++){
right[i] = arr[mid+i+1]; // store values from mid+1 to end in this array
}
  
// now writing code to merge these two arrays in the original array by comparison
  
int i = 0,j=0,k=start;
//code to merge both array
while(i<l1 and j<l2){
if(left[i]<=right[j]){ // checking if left side is greater or right side
arr[k++] = left[i++]; // if left side is smaller or equal to right array then assign it to arr ..... it is sorting in ascending fashion
}
else{
arr[k++] = right[j++];
}
  
}
  
// code for if left array is remaining
while(i<l1){
arr[k++] = left[i++];

}
// code for if right array is remaining
while(j<l2){
arr[k++] = right[j++];
}
  
}
void sort_list(int arr[],int start,int end){
//divide and conquer
//divide the array until start becomes greater than or equal to end
if(start<end){
int mid = (start+end)/2;
sort_list(arr,start,mid); //call the function again for left side
sort_list(arr,mid+1,end); // cal the function again for right side
merge_list(arr,start,mid,end); // call function to merge both left and right side
}
}

int main()
{
int arr[7] = {3,5,2,4,5,10,7};
int n = 7;
print_list_before_sort(arr,n);
cout<<endl;
sort_list(arr,0,6);
print_list_after_sort_sort(arr,n);
return 0;
}

output file:-

if any doubt, comment below and please upvote


Related Solutions

Java . Implement a method that meets the following requirements: (a) Calls mergesort to sort an...
Java . Implement a method that meets the following requirements: (a) Calls mergesort to sort an array/list of at least 5 integers (b) Prints the list before and after sorting.
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array...
Implement a method that meets the following requirements: (a) Takes as parameter 1) an int array A 2) an int number x to search for (b) determines whether x exists in A, and prints a message indicating the result (c) has the best worst case Big Oh complexity you can manage, using only your own thinking and the materials (the worst case growth rate for the number of items searched should be as low as possible, given that A contains...
Implement a method that meets the following requirements: (a) Has the same requirements as the above...
Implement a method that meets the following requirements: (a) Has the same requirements as the above method, but works with an int array that is sorted in increasing order. Attempt your best complexity (b) In comments above the method, explain what its algorithmic complexity is and why (constant, logarithmic, linear, quadratic...)
. Implement a method that meets the following requirements: Computer Language:Java (a) Try to write this...
. Implement a method that meets the following requirements: Computer Language:Java (a) Try to write this method with as few lines of code as you can (b) Sorts a group of three integers, x,y and z, into increasing order (they do not have to be in a sequence). (c) Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and z (none of them contain the same...
1. Implement a method that meets the following requirements: (a) Do not reuse any code for...
1. Implement a method that meets the following requirements: (a) Do not reuse any code for the following: i. Try to write this method with as few lines of code as you can ii. Sorts a group of three integers, x,y and z, into decreasing order (they do not have to be in a sequence). iii. Assume the value in x is less than the value in z. You can also assume there are no duplicates among x, y and...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
Write a generic method mergeSort based on the sort program of Fig. 19.6 (the source code...
Write a generic method mergeSort based on the sort program of Fig. 19.6 (the source code is given as a separate file along with this final document, and also appended at the end of this document). Test your program that prints before sorting, sorts, and prints after sorting an Integer array, a Double array, and a String array as follows:       Integer[] dataInt = {63, 19, 65, 38, 26, 74, 27, 25, 70, 38};       Double[] dataDouble = {102.5, 1.98,...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and fill it with the names of various sandwiches. Make an empty list called finished_sandwiches. Loop through the list of sandwich orders and spring a message for each order such as "I am working on your tuna sandwich" As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT