I need help in C++ assignment. Requested files : Mergesort.h, Mergesort.cpp, Mer
ID: 3733355 • Letter: I
Question
I need help in C++ assignment.
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
** Mergesort.h **
#include <iostream>
using namespace std;
// MergeSort class and method declaration
class Mergesort {
public:
int *arr, arr_size;
Mergesort(int a[], int size);
void Print();
void applySort(int l, int r);
void merge(int l, int m, int r);
};
** Mergesort.cpp **
#include"Mergesort.h"
//Counstructor of Mergesort class
Mergesort::Mergesort(int a[], int size) {
arr_size = size;
arr = new int[size];
arr = a;
}
//Function to print array
void Mergesort::Print() {
for (int i=0; i < arr_size; i++)
printf("%d ", arr[i]);
printf(" ");
}
//Function to apply sort on array
void Mergesort::applySort(int l, int r) {
if (l < r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;
// Sort first and second halves
applySort(l, m);
applySort(m+1, r);
merge(l, m, r);
}
}
void Mergesort::merge(int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
//Create temp arrays
int L[n1], R[n2];
//Copy data to temp arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
//Merge the temp arrays back into array
i = 0; //Initial index of first subarray
j = 0; //Initial index of second subarray
k = l; //Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/*Copy the remaining elements of L[],
if there are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy the remaining elements of R[],
if there are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
** Mergesort_test.cpp **
#include<stdlib.h>
#include<stdio.h>
#include <fstream>
#include"Mergesort.h"
int arr[50];
int arr_size = 0;
//Read array elements from file
void readDataFromFile(char f_name[20]) {
ifstream ifile;
ifile.open(f_name);
arr_size=0;
while (ifile >> arr[arr_size]) {
arr_size++;
}
ifile.close();
}
//Read sorted array elements into file
void writeDataToFile(char f_name[20]) {
ofstream ofile;
ofile.open(f_name);
for(int i=0; i<arr_size; i++) {
ofile << arr[i] << endl;
}
ofile.close();
}
int main()
{
//Read array from file
readDataFromFile("threesData.bin");
//Create object of MergeSort class with given array and size
Mergesort sort(arr, arr_size);
printf("Original array : ");
sort.Print();
//Apply merge sort on array
sort.applySort(0, arr_size - 1);
printf(" Sorted array : ");
sort.Print();
//Write result of sorted array to file
writeDataToFile("sortedThreesData.bin");
return 0;
}
** OUTPUT **
Original array :
72 51 43 15 6 33
Sorted array :
6 15 33 43 51 72
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.