Learning Goal: I’m working on a c++ multi-part question and need an explanation and answer to help me learn.
Create a class that will implement 4 different sorting algorithms. For this lab you are going to want to have overloaded constructors and mutator functions that will set the data section with a list to sort. Your class should sort a primitive array or a vector. For this assignment we want to be able to sort primitive types (int, char, double, float). So, it might be best to have your sort algorithms work on doubles. Each of your sort functions should produce a list of sorted values.
Additional Functionality
You should have a function that will return the number of iterations it took to sort the list. If I choose one of your sort algorithms I should then be able to call the function to get the number of iterations.
Timer class: Attached to this assignment is the timer class that will allow you to profile each of the sorting algorithms. Implement this class and create a function that will return the time the last sort took to sort the list of numbers. In the end you should be able to successively call each of the sort functions and produce the number of iterations and the time it took to sort.
Note: The timer class is here for you to use. You must use the timer classes given to you. If they do not meet your needs then it is up to you to make them meet your needs. Anyone who uses Data Structures functionality like linked lists, trees, graphs and in this case a timer that is built into the programming language or found online will see a grade reduction of 50%.
Testing your code
In main you should generate a large list of random doubles to be sorted ( No 10 items is not a large list. It should be more like a few thousand), use each function to sort the list, and output the iterations, and the time each algorithm took to sort your list. To get a better feel for how each of these algorithms performs you should vary the size of the list to be sorted. Try varying the size of your lists. In comments let me know which was more efficient and why you think it was.
Timer.h:
#include
class Timer
{
private:
LARGE_INTEGER start;
LARGE_INTEGER stop;
LARGE_INTEGER freq;
double PCFreq;
__int64 CounterStart;
public:
Timer();
void startTimer();
void stopTimer();
double getMilli();
double getSeconds();
double getMicro();
};
Timer code:
#include
#include “Timer.h”
using std::cout;
using std::endl;
int main()
{
Timer t;
t.startTimer();
Sleep(1000);
t.stopTimer();
cout << "In Milliseconds " << t.getMilli() << endl;
cout << "In Seconds " << t.getSeconds()<< endl;
cout << std::fixed << "In Microseconds " << t.getMicro() << endl;
return 0;
}
Timer::Timer()
{
if(!QueryPerformanceFrequency(&freq))
cout << "QueryPerformanceFrequency failed!\n";
}
void Timer::startTimer()
{
QueryPerformanceCounter(&start);
}
void Timer::stopTimer()
{
QueryPerformanceCounter(&stop);
}
double Timer::getMicro()
{
PCFreq = freq.QuadPart / 1000000.0;
return double((stop.QuadPart – start.QuadPart)) / PCFreq;
}
double Timer::getMilli()
{
PCFreq = freq.QuadPart / 1000.0;
return double((stop.QuadPart – start.QuadPart)) / PCFreq;
}
double Timer::getSeconds()
{
return double(stop.QuadPart – start.QuadPart)/ freq.QuadPart;
}
The sorting algorithms to use:
void insertionSort(double* list, int last, long long int& x)
{
double hold = 0;
int search = 0;
for (int current = 1; current = 0 && hold < list[search]; search–)
{
x++;
list[search + 1] = list[search];
}
list[search + 1] = hold;
}
return;
}
void selectionSort(double* list, int last, long long int& x)
{
int smallest = 0;
int holdData = 0;
for (int current = 0; current < last; current++)
{
smallest = current;
for (int index = current + 1; index <= last; index++)
{
x++;
if (list[index] < list[smallest])
{
smallest = index;
}
}
holdData = list[current];
list[current] = list[smallest];
list[smallest] = holdData;
}
return;
}
void shellSort(double* list, int last, long long int& x)
{
int hold;
int incre;
int index;
incre = last / 2;
while (incre != 0)
{
for (int curr = incre; curr = 0 && hold < list[index])
{
x += 2;
list[index + incre] = list[index];
index = (index – incre);
}
x++;
list[index + incre] = hold;
}
incre = incre / 2;
}
return;
}
void merge(double* a, int low, int high, int mid, long long int& x)
{
int i, j, k, c[100000];
i = low;
j = mid + 1;
k = low;
while ((i <= mid) && (j <= high))
{
x++;
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[j];
k++;
i++;
x++;
}
while (j <= high)
{
c[k] = a[i];
k++;
j++;
x++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
x++;
}
}
int mergesort(double* a, int low, int high, long long int& x)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
mergesort(a, low, mid, x);
mergesort(a, mid + 1, high, x);
merge(a, low, high, mid, x);
}
return(0);
}
Why Work with Us
Top Quality and Well-Researched Papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional and Experienced Academic Writers
We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.
Free Unlimited Revisions
If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.
Prompt Delivery and 100% Money-Back-Guarantee
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & Confidential
We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.
Admissions
Admission Essays & Business Writing Help
An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.
Reviews
Editing Support
Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.
Reviews
Revision Support
If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.