Sei sulla pagina 1di 5

#include<iostream>

using namespace std;


#include<chrono>

using namespace std::chrono;

class EmpiricalAnalysis
{
public:
int size;
int Arr[];

EmpiricalAnalysis()
{}
EmpiricalAnalysis(int Arr[], int size)
{}
void insertionSort(int Arr[], int size)
{
int i, key, j;
for (i = 1; i < size; i++)
{
key = Arr[i];
j = i - 1;

while (j >= 0 && Arr[j] > key)


{
Arr[j + 1] = Arr[j];
j = j - 1;
}
Arr[j + 1] = key;
}
}
void merge(int* array, int l, int m, int r) {
int i, j, k, nl, nr;
nl = m - l + 1; nr = r - m;
int larr[50], rarr[50];

for (i = 0; i < nl; i++)


larr[i] = array[l + i];
for (j = 0; j < nr; j++)
rarr[j] = array[m + 1 + j];
i = 0; j = 0; k = l;
while (i < nl && j < nr) {
if (larr[i] <= rarr[j]) {
array[k] = larr[i];
i++;
}
else {
array[k] = rarr[j];
j++;
}
k++;
}
while (i < nl)
array[k] = larr[i];
i++; k++;
}
while (j < nr) { array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int* array, int l, int r) {
int m;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(array, l, m);
mergeSort(array, m + 1, r);
merge(array, l, m, r);
}
}
void swap(int* a, int* b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}

int Partition(int a[], int l, int h) {


int pivot, index, i;
index = l;
pivot = h;
for (i = l; i < h; i++) {
if (a[i] < a[pivot]) {
swap(&a[i], &a[index]);
index++;
}
}
swap(&a[pivot], &a[index]);
return index;
}
int RandomPivotPartition(int a[], int l, int h) {
int pvt, n, temp;
n = rand();
pvt = l + n % (h - l + 1);
swap(&a[h], &a[pvt]);
return Partition(a, l, h);
}
int QuickSort(int a[], int l, int h) {
int pindex;
if (l < h) {
pindex = RandomPivotPartition(a, l, h);
QuickSort(a, l, pindex - 1);
QuickSort(a, pindex + 1, h);
}
return 0;
}

void printArray(int Arr[], int size)


{
int i;
for (i = 0; i < size; i++)
cout << Arr[i] << " ";
cout << endl;
}
};
int main()
{
EmpiricalAnalysis EM,EM2,EM3;
int size = 75;
int* Arr = new int[size];
for (int i = 0; i < size; i++)
{
Arr[i] = rand() % 100;
cout << Arr[i] << " ";
}
int* Arr2 = new int[size];
for (int i = 0; i < size; i++)
{

Arr2[i] = Arr[i];
}
int* Arr3 = new int[size];
for (int i = 0; i < size; i++)
{

Arr3[i] = Arr[i];
}
cout << endl;
cout << "**************************************************************" << endl;

auto start = high_resolution_clock::now();


EM.insertionSort(Arr, size);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << endl;
cout << "Time for insertion Sort when size is "<<size<< "::" << duration.count()
<<"ms"<< endl;

EM.printArray(Arr, size);

cout << "**************************************************************" << endl;


auto start2 = high_resolution_clock::now();
EM2.mergeSort(Arr2, 0, size - 1);
auto stop2 = high_resolution_clock::now();
auto duration2 = duration_cast<microseconds>(stop2 - start2);
cout << endl;
cout << "Time for Merge Sort when size is " << size << "::" <<
duration2.count()<<"ms" << endl;

EM2.printArray(Arr2, size);

cout << "**************************************************************" << endl;

auto start3 = high_resolution_clock::now();


EM3.QuickSort(Arr3, 0, size - 1);
auto stop3 = high_resolution_clock::now();
auto duration3 = duration_cast<microseconds>(stop3 - start3);
cout << endl;
cout << "Time for Quick Sort when size is " << size << "::" <<
duration3.count()<<"ms" << endl;
EM3.printArray(Arr3, size);
system("pause");
return 0;
}

Output:
When n=25

When n=50
When n=75

When n=100

Potrebbero piacerti anche