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

. 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,...
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....
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...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to...
IN MATLAB!! Q6. Create a 1D array of numbers and implement ‘Merge Sort’ in MATLAB to sort it in ascending order
You are provided with an array of Strings and a list of Strings. Sort the elements...
You are provided with an array of Strings and a list of Strings. Sort the elements (1) in natural order, (2) in reverse natural order and (3) by the length of each String. You can fill in the details by using the following stub file: import java.util.Arrays; import java.util.Collections; import java.util.List; public class Midterm01 { public static void main(String[] args) { String[] arrayOfCities = { "Atlanta", "Savannah", "New York", "Dallas", "Rio" }; List<String> listOfCities = Arrays.asList("Atlanta", "Savannah", "New York", "Dallas",...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT