In: Computer Science
. 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.
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