C++ help please
Sort the following list using MERGE sort as discussed in this
chapter.
Show the list after each iteration of the outer for loop.
26, 45, 17, 65, 33, 55, 12, 18, 2, 12
Sort the following list using MERGE sort as discussed in this
chapter.
Show the list after each iteration of the outer for loop.
36, 55, 17, 35, 63, 85, 12, 48, 3, 66, 15
Code has been executed in C++
#include <iostream>
#include<stdlib.h>
using namespace std;
// Function to merge left and right subarrays
void Merging(int arr[], int x, int y, int z)
{
// Assigning the sizes to Temporary arrays
int n1 = y - x + 1;
int n2 = z - y;
// Temporary arrays
int A[n1], B[n2];
// Storing the elements in the Temporary arrays
for (int l = 0; l < n1; l++)
A[l] = arr[x + l];
for (int r = 0; r < n2; r++)
B[r] = arr[y + 1+ r];
// Transfering the elements in aligned manner back to Temporary
arrays
int l = 0;
int r = 0;
int t = x;
while (l < n1 && r < n2)
{
if (A[l] <= B[r]) // In case elements are lesser from right
subarray
{
arr[t] = A[l];
l++;
}
else
{
arr[t] = B[r]; // Tranfer the right subarray element to the main
array
r++;
}
t++;
}
// Transfering the elements back to Temporary arrays in case any
element was left
while (l < n1) // In case any element of left subarray was
remaining
{
arr[t] = A[l];
l++;
t++;
}
while (r < n2) // In case any element of right subarray was
remaining
{
arr[t] = B[r];
r++;
t++;
}
}
// Function to find minimum of two numbers
int minimal(int p, int q)
{
if(p<q)
return p
else
return q
}
// Merge Sort function to sort the array
void Merge_Sort(int arr[], int n)
{
int count=1; // Intializing the counter
// Merging subarrays from down to top
for (int size=1;size<=n-1;size = 2*size)
{
// Starting from the left subarray index upto the array size
for (int s=0;s<n-1;s+= 2*size)
{
// Trying to reach at end point of left subarray
int v = minimal(s + size-1, n-1);
int u = minimal(s + 2*size-1, n-1);
// Calling merge function
Merging(arr, s, v, u);
}
// Printing the Iteration
cout<<"\nIteration "<<count<<": ";
for(int i=0;i<n;i++)
{
// Printing the step by step sorting in each Iteration
cout<<arr[i]<<" ";
}
cout<<"\n";
count++; // Incrementing the counter
}
}
// Main driver function
int main()
{
int arr[80]; // Initializing the array
int n;
cout<<"Enter the size of the array:";
cin>>n;
cout<<"\nEnter the array: ";
for (int i=0; i < n; i++)
cin>>arr[i];
// Calling the merge sort function.
Merge_Sort(arr, n);
cout<<"\nSorted array: ";
// Printing the sorted array.
for (int i=0; i < n; i++)
cout<<arr[i]<<" ";
cout<<"\n";
return 0;
}
Code snapshot:-
Output snapshot:-
C++ help please Sort the following list using MERGE sort as discussed in this chapter. Show...
How to write this in c++? 1. Sort the following list using the selection sort algorithm as discussed in this chapter. Show the list after each iteration of the outer for loop. 36, 55, 17, 35, 63, 85, 12, 48, 3, 66 (10 points)
in c++ Sort the following list using the bubble sort algorithm as discussed in this chapter. Show the list after each iteration of the outer for loop.2) 82, 17, 40, 28, 15, 55, 46, 93, 6, 67, 11, 3 6.
Sort the following list using the bubble sort algorithm as discussed in this chapter. Show the list after each iteration of the outer for loop. 46, 58, 16, 25, 83, 98, 8, 70, 5, 62
C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...
Recall the insertion sort algorithm as discussed in this chapter. Assume the following list of keys: 30, 20, 35, 27, 96, 82, 60, 48, 75, 5, 80 Exactly how many key comparisons are executed to sort this list using insertion sort? Show the values and the number of comparisons after each iteration for the loop.
Write a MIPS assembly language for sorting an array of integers using non-recursive bottom-up merge sort algorithm. Your program should print the processed array after each step of the merge sort. For example, if the input array is 14 27 13 11 49 63 17 9, your program should print each sort process: Input Arra;y 14 27 13 11 49 63 17 9 Print After first Iteration 14 27 11 13 49 639 17 Print After second iteration 11 13...
8. (5 points) Trace merge sort algorithm as it sorts the following sequence of integer array into a descending order. 42 45 10 64 55 37 96 7 9. (5 points) Trace shell sort algorithm that halves the gap size at each iteration as it sorts the following sequence of integer array into an ascending order. 42 18 10 64 85 37 96 71 8. (5 points) Trace merge sort algorithm as it sorts the following sequence of integer array...
2) Sorting (a) (5 pts) In a Merge Sort of 8 elements, the Merge function gets called 7 times. Consider a Merge Sort being executed on the array shown below. What does the array look like right AFTER the sixth call to the Merge function completes? نرا index value 0 40 2 12 4 11 5 99 6 31 7 16 27 18 0 1 2 زيا 4 5 6 7 Index Value (b) (5 pts) Consider sorting the array...
Please show how you did this in excel. :13-19 Every home football game for the past eight years at Eastern State University has been sold out. The revenues from ticket sales are significant, but the sale of food, beverages, and souvenirs has contrib- uted greatly to the overall profitability of the football program. One particular souvenir is the football pro- gram for each game. The number of programs sold at each game is described by the following probabil- ity distribution:...
1. Given the following list 107, 22, 53, 85, 21, 6, 97, 6, 32 Use merge sort to fill the following table that shows the value of first and last arguments in calling the quick sort method and the content of the list after each merging a. After f list merge firstlast method O 34 6 7 [8) b. Use number of elements in the merge sort in part a to draw the merge sort tree that shows the divide...