Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE READ THE QUESTION BEFORE YOU POST THE ANSWER. USE C++ AND PLEASE DESCRIBE

ID: 3715933 • Letter: P

Question

PLEASE READ THE QUESTION BEFORE YOU POST THE ANSWER. USE C++ AND PLEASE DESCRIBE YOUE CODE. ALSO RUN YOUR PROGRAM AND ANSWER IT PLEASE.

Note: Your code should work with files of any size.

MOST OF THE ANSWER BEFORE ARE FALSE. SO PLEASE DONOT JUST COPY AND PASTE. DO IT FROM SCRATCH.

Requested files: Mergesort.h, Mergesort.cpp, Mergesort_test.cpp
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

//main.cpp
#include<iostream>
#include "Mergesort.h"
#include<fstream>

using namespace std;
int main()
{
    int size;
    int* numbers;
    fstream file;
  
    file.open("threesData.bin", ios::binary | ios::in |ios::ate);
    int memory = file.tellg();   
    size = memory/4;  
    numbers = new int[size];  
    file.seekg(0, ios::beg);
    file.read(reinterpret_cast<char *>(numbers), sizeof(numbers));
    file.close();
  
    mergesort(numbers, size);   //sort the array
  
    file.open("sortedThreesData.bin", ios::binary | ios::out |ios::ate);  
    file.write(reinterpret_cast<char *>(numbers), sizeof(numbers));
    file.close();
  
    delete[]numbers;  
    return 0;
}
---------------------------------------------------------------------------------------------------
//MergeSort.cpp
#include<iostream>
#include"Mergesort.h"
using namespace std;

void selectionsort(int array[], int size){
    int scan;
    int min;
    int minindex;
    for(scan = 0; scan<size-1; scan++){
        min = array[scan];
        minindex = scan;
        for(int i= scan+1; i<size; i++ ){
            if(array[i] < min){
                min = array[i];
                minindex = i;
            }
        }
        array[minindex] = array[scan];
        array[scan] = min;
    }
}


//merges an array
void merges(int A[], int left[], int right[],int nL, int nR){

    int i=0;
    int k=0;
    int j=0;
    //loop through the array
    while(i< nL && j < nR){
        if(left[i] <= right[j]){
            A[k] = left[i];
            i++;
        }

        else{
            A[k] = right[j];
            j++;
        }
        k++;
    }


    while(i < nL){
        A[k] = left[i];
        i++;
        k++;
    }

    while(j < nR){
        A[k] = right[j];
        j++;
        k++;
    }
}

// sorts and merges an array.
void mergesort(int A[], int n){
    if (n<2){
        return;
    }
    int mid = n/2;
    int left[mid];
    for(int i=0; i<mid; i++){
        left[i]=A[i];
    }
    int right[n-mid];
    for(int j=mid; j<n; j++){
        right[j-mid] = A[j];

    }

    selectionsort(left, mid);
    selectionsort(right, n-mid);
    merges(A, left, right, mid, n-mid);

}

// prints an array
void print(int A[], int size){
    for(int i=0; i<size; i++){
        cout << A[i]<< endl;
    }
}
---------------------------------------------------------------
//Mergesort.h
#ifndef MERGESORT_MERGESORT_H
#define MERGESORT_MERGESORT_H


void selectionsort(int array[], int size);
void merges(int A[], int left[], int right[],int nL, int nR);
void mergesort(int A[], int n);
void print(int A[], int size);


#endif //MERGESORT_MERGESORT_H

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote