Question: I'm working on a c++ project that uses multiple sort functions (insertion sort, selection sort, merge sort, etc.) and the purpose is to time each

I'm working on a c++ project that uses multiple sort functions (insertion sort, selection sort, merge sort, etc.) and the purpose is to time each sort to determine which sort takes the longest/shortest. I am struggling to get the initialized array to work with the sort function. I call the InitArrary() function that populates the array with random numbers. Then I start the timer which is given to me by the professor so that is correct. Then I call the sort function and I don't understand how to get the array into the sort. I'm thinking I might need a helper function of some kind? If I could just get some help with the InsertionSort() I am sure I will be able to do the rest. This is a work in progress therefore not everything is complete

-------------------------------Main.cpp-------------------------------------

#include #include "Sort.h" #include "Timer.h"

using namespace std;

int main() { const int DATA_SIZE = 100000; // you will have to adjust this to suit the speed of your machine. Sort *sort = new Sort(DATA_SIZE); Timer ti;

sort->SetRandomSeed(12345);

sort->InitArray(); cout << "starting InsertionSort" << endl; ti.Start(); sort->InsertionSort(); ti.End(); cout << "Insertion sort duration: " << ti.DurationInMilliSeconds() << "ms" << endl;

delete sort;

cout << endl <<"Press [Enter] key to exit"; getchar();

}

------------------------------Sort.h-------------------------

#pragma once #include #include

using namespace std;

class Sort { public: Sort(); Sort(int startArraySize); ~Sort();

//array functions

void InitArray(); int GetSize(); void SetRandomSeed(unsigned int seed);

//debugging stuff friend ostream& operator<< (ostream& out, const Sort& s);

//sorts n stuff void InsertionSort(int ar[], int size); void InsertionSortList(); void SelectionSort(int ar[], int size); void MergeSort(int ar[], int n); void merge(int ar[], size_t n1, size_t n2); void QuickSort(int ar[], int size); void partition(int ar, int size, size_t pivot_index); void VectorSort();

//void printArray(int ar[], int size); void swap(int *xp, int *yp);

private: int size; int *data; };

----------------------------Sort.cpp----------------------------

#include #include #include #include

#include "Sort.h"

using namespace std;

Sort::Sort() {

}

Sort::Sort(int startArraySize) { size = startArraySize; }

Sort::~Sort() { delete data; }

void Sort::InitArray() { int* ar = NULL; // Pointer to int, initialize to nothing. ar = new int[size]; // Allocate n ints and save ptr in a. for (int i = 0; i < size; i++) { ar[i] = 1 + rand() % (size * 3); //generate a random number from 1 to size of array cout << ar[i] << endl; } }

int Sort::GetSize() { return size; }

void Sort::SetRandomSeed(unsigned int seed) { srand(seed); }

void Sort::InsertionSort(int ar[], int size) { int i, point, j; for (i = 1; i < size; i++) { point = ar[i]; j = i - 1;

while (j >= 0 && ar[j] > point) { ar[j + 1] = ar[j]; j = j - 1; } ar[j + 1] = point; } }

//I have most everything else written out but I will leave those out of here

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!