Note: This is a C++ assignment. I got several answers of this ansginment but non
ID: 3740410 • Letter: N
Question
Note: This is a C++ assignment. I got several answers of this ansginment but non of them was run successfully in my Xcode complier. please add statements and i am using macbook pro (Xcode IDE). Please dont use vector.
Requested files: Mergesort.h, Mergesort.cpp, Mergesort_test.cpp (Download)
Type of work: Individual work
For this program, you will learn about using an index into an array to sort the array contents. The chapter gives you several examples of implementing sorts using indices into an array.
Your program must open the binary file threesData.bin and read each element into an array. Once the integers have been read in, you will write one or more functions that sorts the integers in the array into ascending order (low to high.) Your program should sort this array using the Mergesort.
Mergesort is a method that uses divide and conquer to sort a list of items.
Divide the list into two equal or nearly equal lists.
Sort each half of the list into ascending order using selectionsort from the textbook..
Merge the two sorted lists into one sorted list by repeatedly removing the smaller of the two items at the front of each list and putting it in the final array.
Your program should be organized using the practice we have adopted: Mergesort.h, Mergesort.cpp and Mergesort_test.cpp which contains your main() function.
Your program should write the sorted data to a file name "sortedThreesData.bin" This file will be examined to determine your grade.
Explanation / Answer
Problem Description
1. Union sort depends on an algorithmic outline design called isolate and-win.
2. It shapes tree structure.
3. The stature of the tree will be log(n).
4. we consolidate n component at each level of the tree.
5. The time intricacy of this calculation is O(n*log(n)).
Problem Solution
1. Split the information into two equivalent half until the point that we get at most one component in both half.
2. Union Both into one ensuring the subsequent arrangement is arranged.
3. Recursively split them and converge based on limitation given in stage 1.
4. Show the outcome.
5. Exit.
Program/Source Code
C++ program to execute Merge sort.
This program is effectively keep running on Dev-C++ utilizing TDM-GCC 4.9.2 MinGW compiler on a Windows framework.
#include <iostream>
using namespace std;
void Mergesort(int *a, int lowval, int highval, int midva)
{
int i, j, k, temp[highval-lowval+1];
i = lowval;
k = 0;
j = midval + 1;
while (i <= midval && j <= highval)
{
if (a[i] < a[j])
{
temp[k] = a[i];
k++;
i++;
}
else
{
temp[k] = a[j];
k++;
j++;
}
}
while (i <= midval)
{
temp[k] = a[i];
k++;
i++;
}
while (j <= highval)
{
temp[k] = a[j];
k++;
j++;
}
for (i = lowval; i <= highval; i++)
{
a[i] = temp[i-lowval];
}
}
void MergeSort(int *a, int lowval, int highval)
{
int midval;
if (lowval < highval)
{
midval=(lowval+highval)/2;
MergeSort(a, lowval, midval);
MergeSort(a, midval+1, highval);
MergeSort(a, lowval, highval, midval);
}
}
int main()
{
int n, i;
cout<<" Enter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
MergeSort(arr, 0, n-1);
cout<<" Sorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.