Factors Affecting Running Time.
Your task is to conduct experiments to see how sorting
algorithms perform in different environments.
You should select conduct two sets of experiments:
Conduct benchmarking of quicksort and merge sort several times on the same system - once with as much software turned off a possible, and then with other programs running - Word, Excel, videos, etc. See if you can determine how different software or combinations of software running at the same time slow down the sorting algorithms the most. You might want to include an Internet connection in this test.
Just as with the different software, How does a live connection to a network affect the running time of the algorithms?
You should submit a report describing your work, your results, and your conclusions.
Three tests were conducted as follow:-
Tests were conducted using C++ language and plottings have been done with the help of MATLAB.
System specifications:-
Test 1 results:-


Test 2 (a) results:-


Test 2b results:-


Question:- How does a live connection to a network affect the running time of the algorithms?
Answer:- Considering quicksort's result for both test 2(a) and 2(b) there wasn't any much of a difference between the two but in merge sort we saw a little increase in the time taken to sort while the internet connection was active.
Conclusion:-
With programs turned off, we did get better performance while sorting numbers using both quicksort and merge sort.
When a few programs were opened, we saw an increase in the time taken to sort from test 1's results. Although, internet connection did not play any major role in the time taken to sort.
C++ code used:-
/* mergeSort.hpp */
#ifndef MERGE_H
#define MERGE_H
#include<iostream>
/**************************************************************************************************
* Function: To merge left and right sub-arrays into arr in sorted order.
* Input: Five parameters: left and right sub-arrays and their size and bigger array arr.
* Output: None.
**************************************************************************************************/
void merge(int *arr, int *left, int leftCount, int *right, int rightCount)
{
int i = 0, j = 0, k = 0;
/*Fill arr with elements from left and right sub-arrays in sorted order until one or both
sub-arrays are exhausted*/
while(i < leftCount && j < rightCount)
{
/*Merge in ascending order*/
if(left[i] <= right[j])/*Change to left[i] >= right[i] to sort in descending order*/
arr[k++] = left[i++];
else
arr[k++] = right[j++];
}
/*If all elements of right sub-array are merged into arr but left sub-array still not completely merged
then merge left out elements of left sub-array into arr*/
while(i < leftCount)
{
arr[k++] = left[i++];
}
/*If all elements of left sub-array are merged into arr but right sub-array still not completely merged
then merge left out elements of right sub-array into arr*/
while(j < rightCount)
{
arr[k++] = right[j++];
}
}
/**************************************************************************************************************************
* Function: To divide arr into left and right sub-arrays and call merge function to merge left and right sub-arrays
* Input: Two parameters: Array to be divided and it's size.
* Output: None.
**************************************************************************************************************************/
void mergeSort(int *arr, int size)
{
if(size < 2)
return;
int mid = size/2;
/*Allocate memory for both sub-arrays*/
int *left = new int[mid];
int *right = new int[size - mid];
/*Fill left sub-array*/
for(int i = 0; i < mid; i++)
left[i] = arr[i];
/*Fill right sub-array*/
for(int i = mid; i < size; i++)
right[i - mid] = arr[i];
/*Call mergeSort to further divide sub-arrays left and right*/
mergeSort(left, mid);
mergeSort(right, size - mid);
merge(arr, left, mid, right, size - mid);
delete[] left;
delete[] right;
}
#endif
/* END OF mergeSort.hpp */
/* quickSort.hpp */
#ifndef QUICKSORT_H
#define QUICKSORT_H
#include<iostream>
/**************************************************************************************************
* Function: Swaps passed formal arguments.
* Input: Two parameters: Two pointer-to-int to store address of two variables passed.
* Output: None.
**************************************************************************************************/
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
/**************************************************************************************************
* Function: Creates partition around pivot element.
* Input: Three parameters: Array to partition, left index and right index
* Output: Index of sorted pivot element.
**************************************************************************************************/
int partition(int *a, int left, int right)
{
int pivot = a[right];//Select last element as pivot
int i = left - 1;
for(int j = left; j < right; j++)
{
if(a[j] <= pivot)
{
i++;
swap(a[i], a[j]);
}
}
swap(a[i + 1], a[right]);//Swap pivot element with a[i + 1]
return i + 1;
}
/**************************************************************************************************
* Function: To perform quick sort on array.
* Input: Three parameters: Array to sort, left and right index of array.
* Output: None.
**************************************************************************************************/
void quickSort(int *a, int left, int right)
{
int j;
if(left < right)
{
j = partition(a, left, right);
quickSort(a, left, j - 1);
quickSort(a, j + 1, right);
}
}
#endif
/* END OF quickSort.hpp */
/* main.cpp */
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <chrono>
#include "quickSort.hpp"
#include "mergeSort.hpp"
using namespace std;
/*Function to initialize array with random values*/
void randomize(int *arr, const int &size)
{
for(int i = 0; i < size; i++)
arr[i] = rand();//Initialize array with random values
}
int main()
{
srand(time(NULL));//Seed for random numbers generator rand()
ofstream o;
int *arr;
/*Test quick sort*/
o.open("quickSortResult.csv");
for(int size = 500; size <= 100000; size += 500)
{
arr = new int[size];//Allocate memory for array with size elements
randomize(arr, size);//Initialize array with random values
auto start = chrono::high_resolution_clock::now();//Start counting time
quickSort(arr, 0, size - 1);//Call quickSort to sort randomized array
auto end = chrono::high_resolution_clock::now();//Stop counting time
chrono::duration<float> time_elapsed = end - start;//Get time difference
float time_taken = time_elapsed.count()*1000;//Time in milliseconds
o << size << "," << time_taken << '\n';
delete[] arr;//Free memory allocated for the array
}
o.close();
/*Quick sort testing complete*/
/*Test merge sort*/
o.open("mergeSortResult.csv");
for(int size = 500; size <= 100000; size += 500)
{
arr = new int[size];//Allocate memory for array with size elements
randomize(arr, size);//Initialize array with random values
auto start = chrono::high_resolution_clock::now();//Start counting time
mergeSort(arr, size);//Call mergeSort to sort randomized array
auto end = chrono::high_resolution_clock::now();//Stop counting time
chrono::duration<float> time_elapsed = end - start;//Get time difference
float time_taken = time_elapsed.count()*1000;//Time in milliseconds
o << size << "," << time_taken << '\n';
delete[] arr;//Free memory allocated for the array
}
o.close();
/*Merge sort testing complete*/
return 0;
}
/* END OF main.cpp */
Factors Affecting Running Time. Your task is to conduct experiments to see how sorting algorithms perform...