0

GOAL

The goal of this programming assignment is two-fold. The first goal is to observe empirically the complexities of different algorithms solving the same problem. The second goal is to discover how accurate the theoretical estimates of complexity are when compared to real execution times.

So im suppose to Run experiments varying n (where n is the number of elements) from ns to nf, with increment delta.

For each n value, run each algorithm m times on different input arrays and take the average of the running times.

Run the three algorithms on the same input arrays.  To generate numbers, use a random number generator. Then for each array instance run the algorithms.

Determine the unit of time for your measurements, such as milliseconds (ms) or microseconds

Then get the average of the sorting algorithm.

QUESTION

1. SO i literally tried all my option but no matter what I cant seem to get my QUICK SORT portion of the program to work, it all comment it out. My max heap is fine, but dont know it my insertion is correct base on the output when it runs (example below) I just need help to figure out what causing my Quick sort to crash the program

  1. I already have the Averages base on the output, but apparently i need to show it up to 20 times. and it only run it 10 times, did I code something wrong?

  2. is there a way to make my output for the seconds a bit nicer? i tried nanosec, but it still give me a large number. thank you

PS: is there a way to make my output for the seconds a bit nicer? i tried nanosec, but it still give me a large number.

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include<stdio.h>
#include <cstdio>
#include <ctime>
#include <chrono>
using namespace std;


void insertion(int A[], int n)
{
    int i, j, key;
    for (j = 2; j < n; j++)
    {
        key = A[j];

        i = j - 1;

        while (i > 0 && A[i] > key)
        {
            A[i + 1] = A[i];
            i--;
        }
        A[i + 1] = key;
    }
}

//int quicksort_Partition(int A[], int p, int r)
//{
//  int x = A[r];
//  int i = p - 1;
//  int temp;
//  for (int j = p; j <= r - 1; j++)
//  {
//      if (A[j] <= x)
//      {
//          i = i + 1;
//
//          temp = A[i];
//          A[j] = A[i];
//          A[i] = temp;
//      }
//
//  }
//  temp = A[i + 1];
//  A[i + 1] = A[r];
//  A[i + 1] = temp;
//  return i + 1;
//}
//
//void Quicksort(int A[], int p, int r)
//{
//  if (p < r)
//  {
//      int q;
//      q = quicksort_Partition(A, p, r);
//      Quicksort(A, p, q - 1);
//      Quicksort(A, q + 1, r);
//
//  }
//}


void heapify(int A[], int n, int i)
{
    int largest = 0;
    int Left = 2 * i + 0;
    int Right = 2 * i + 1;

    if (Left < n && A[Left] > A[i])
    {
        largest = Left;
    }
    else
    {
        largest = i;
    }

    if (Right < n && A[Right] > A[largest])
    {
        largest = Right;
    }

    if (largest != i)
    {
        swap(A[i], A[largest]);
        heapify(A, n, largest);
    }
}

void BuildMaxHeap(int A[], int n) {
    // Build max heap
    for (int i = floor(n / 2); i >= 0; i--)
        heapify(A, n, i);
}

void HeapSort(int A[], int n)
{
    int heapsize = n - 1;
    BuildMaxHeap(A, heapsize);

    // Heap sort
    for (int i = n - 1; i >= 2; i--)
    {
        swap(A[0], A[i]);
        heapsize--;
        heapify(A, heapsize, 0);
    }
}

void Heap_printArray(int A[], int n)
{
    for (int i = 0; i < n; i++)
        cout << A[i] << " ";
    cout << "\n";
}

//https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
    if (!v.empty()) {
        out << '[';
        std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
        out << "\b\b]";
    }
    return out;
}

void bubblesort(int A[], int n) {
    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < n; j++)
        {
            if (A[j] > A[j - 1])
                swap(A[j], A[j - 1]);
        }
    }
}



int main()
{
    srand(time(NULL));

    int min = 1;
    int max = 100;
    int r = 0;

    int ns = 1000;
    int nf = 20000; //end 
    int delta = 1000; //increment 
    int m = 10; //times to run each algorithm 
    vector<vector<int>> A = vector<vector<int>>();

    for (int i = 0; i < m; i++)
    {
        vector<int> b = vector<int>();
        for (int j = 0; j < ns + delta * i; j++)
        {
            r = rand() % max + min;
            b.push_back(r);
        }

        A.push_back(b);
    }


    srand((unsigned int)0);
    vector<vector<double>> averages = vector<vector<double>>();
    averages.push_back(vector<double>());
    averages.push_back(vector<double>());
    averages.push_back(vector<double>());

    auto start = chrono::steady_clock::now();
    auto end = chrono::steady_clock::now();
    double duration;

    for (int i = 0; i < m; i++)
    {
        vector<int> a = A[i];   // makes a deep copy
        vector<int> b = A[i];
        vector<int> c = A[i];

        start = chrono::steady_clock::now();
        insertion(&a[0], a.size());
        end = chrono::steady_clock::now();
        duration = double(chrono::duration_cast <chrono::nanoseconds> (end - start).count());
        averages[0].push_back(duration);


        start = chrono::steady_clock::now();
        //Quicksort(&b[0], 0, b.size() - 1);
        end = chrono::steady_clock::now();
        duration = double(chrono::duration_cast <chrono::nanoseconds> (end - start).count());
        averages[1].push_back(duration);

        start = chrono::steady_clock::now();
        HeapSort(&c[0], c.size());
        end = chrono::steady_clock::now();
        duration = double(chrono::duration_cast <chrono::nanoseconds> (end - start).count());
        averages[2].push_back(duration);


        cout << averages[0] << endl;
        cout << averages[1] << endl;
        cout << averages[2] << endl << endl;
    }

enter image description here

Mark1247
  • 59
  • 7

0 Answers0