Question

In: Computer Science

As stated in the chapter, many different factors affect the running time of an algorithm. Your...

As stated in the chapter, many different factors affect the running time of an algorithm. Your task is to conduct experiments to see how sorting algorithms perform in different environments.


To construct this experiment you must have a program or two programs that sorts sets of data. The program or programs should sort a set of number using a quick sort and using a merge sort. To expedite this, example code that performs a quick sort and a merge sort have been provided.


You should conduct sets of experiments. Categorize your data in two major sections, one for merge sort results, one for quick sort results. Each section can be sub divided into criteria that you used to bench mark the sort.


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?


Use Data Sets Starting at at 10,000,000 randomly generated numbers


All programming projects must have an associated Professional Lab Report submitted with in. A Lab Report must contain the minimum Requirements List Below


Template Workbook for Bench Marking Data

benchmarking.xlsx




Document Formatting

A document header - The Header section should contain your name
A document footer - The footer should contain a page number

Paragraphs Formatting should be formatted

1.5 line spacing
12 points before and after the paragraph

Character Formatting

Body Text should be 12 points
Segment Headers should be boldface

Submit a Project containing the Quick Sort Tool program you created to conduct the research. If the project contains the features of both programs, then only submit one project.

Submit a Project containing the Merge Sort Tool program you created to conduct the research. If the project contains the features of both programs, then only submit one project.

Submit an Excel Workbook containing the Bench Marking Data.

Submit a Lab Report for building your project tool.
Submit an Essay Report describing your work, your results, and your conclusions.Essays should follow the same formatting requirements as Lab Reports

Solutions

Expert Solution

Answer :

How sorting algorithms perform in different environments

Four different test cases were run:

Items are 32-bit integers. These are both very fast to compare and copy.
Also 32-bit integers, but with a high number of repetitions. Each value in the array repeats approximately 100 times in average.
Items are C++ strings with identical beginnings. Strings of 50 characters were used for the test. This tests the case where copying is fast but comparison is slow.
Items are arrays of integers. Arrays of 50 integers were used. Only the first integer was used for the comparison. This tests the case where comparison is fast but copying is slow.
More detailed info for these test cases is given in their individual pages.

I tried to implement the program so that it first counts how much time is spent generating the data to be sorted, and then this time is subtracted from the total time. While it's not possible to do this in a very exact way, I'm confident that the results are close enough to reality.

Each test case with each sorting algorithm was run several times, every time with different random data.

This was done to average out individual worst cases.

Quick sort

It is the most popular sorting algorithm. Its virtue is that it sorts in-place and that it usually is very fast
The main problem with quicksort is that it's not trustworthy: Its worse-case scenario is O(n2) and the pathological cases tend to appear too unexpectedly and without warning, even if an optimized version of quicksort is used.
Implementation:
#ifndef QUICK_SORT_HH
#define QUICK_SORT_HH

#include "InsertionSort.hh"

#include <algorithm>

template<typename ItemType>

unsigned Partition(ItemType* array, unsigned f, unsigned l, ItemType pivot)

{

unsigned i = f-1, j = l+1;

while(true)

{

while(pivot < array[--j]);

while(array[++i] < pivot);

if(i<j)

{

ItemType tmp = array[i];

array[i] = array[j];

array[j] = tmp;

}

else

return j;

}

}

template<typename ItemType>

void QuickSortImpl(ItemType* array, unsigned f, unsigned l)

{

while(f < l)

{

unsigned m = Partition(array, f, l, array[f]);

QuickSortImpl(array, f, m);

f = m+1;

}

}

template<typename ItemType>

void QuickSort(ItemType* array, unsigned size)

{

QuickSortImpl(array, 0, size-1);

}

template<typename ItemType>

void MedianHybridQuickSortImpl(ItemType* array, unsigned f, unsigned l)

{

while(f+16 < l)

{

ItemType v1 = array[f], v2 = array[l], v3 = array[(f+l)/2];

ItemType median =

v1 < v2 ?

( v3 < v1 ? v1 : std::min(v2, v3)

) :

( v3 < v2 ? v2 : std::min(v1, v3)

);

unsigned m = Partition(array, f, l, median);

MedianHybridQuickSortImpl(array, f, m);

f = m+1;

}

}

template<typename ItemType>

void MedianHybridQuickSort(ItemType* array, unsigned size)

{

MedianHybridQuickSortImpl(array, 0, size-1);

InsertionSort(array, size);

}

#endif

Merge Sort:

The virtue of merge sort is that it's a truely O(nlogn) algorithm and that it's stable.

Its main problem is that it requires a second array with the same size as the array to be sorted, thus doubling the memory requirements.

In this test I helped merge sort a bit by giving it the second array as parameter so that it wouldn't have to allocate and deallocate it each time it was called.

Also, instead of doing the basic "merge to the second array, copy the second array to the main array" procedure like the basic algorithm description suggests, I simply merged from one array to the other alternatively.

Implementation:

#ifndef MERGE_SORT_HH
#define MERGE_SORT_HH
template<typename ItemType>
void MergeSort(ItemType* array, ItemType* helpArray, unsigned size)
{
ItemType* src = array;
ItemType* dst = helpArray;


for(unsigned bSize = 2; bSize < size*2; bSize *= 2)
{
unsigned dstInd = 0;
for(unsigned bInd = 0; bInd < size; bInd += bSize)
{
unsigned sbInd1 = bInd, sbInd1e = bInd+bSize/2;
if(sbInd1e > size) sbInd1e = size;
unsigned sbInd2 = sbInd1e, sbInd2e = bInd+bSize;
if(sbInd2e > size) sbInd2e = size;
while(sbInd1 < sbInd1e && sbInd2 < sbInd2e)
{
if(src[sbInd1] < src[sbInd2])
dst[dstInd++] = src[sbInd1++];
else
dst[dstInd++] = src[sbInd2++];
}
while(sbInd1 < sbInd1e) dst[dstInd++] = src[sbInd1++];
while(sbInd2 < sbInd2e) dst[dstInd++] = src[sbInd2++];
}
ItemType* tmp = src; src = dst; dst = tmp;
}
if(src == helpArray)
{
for(unsigned i = 0; i < size; ++i)
dst[i] = src[i];
}
}
#endif

I hope this answer is helpful to you, please upvote my answer. I'm in need of it.. Thank you..


Related Solutions

In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the...
In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the recurrence T (n) = 3T (n/2) + n. We then used the substitution method to solve the recurrence. When using the substitution method, it seems difficult to make a guess about the upper bound on the running time. However, if we use the recursion tree method, it would be a lot easier. In this question, you are asked to solve this recurrence using the...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of...
Give pseudocode to implement a phase of Boruvka’s algorithm. Argue that ˙ the running time of your implementation is O(m)
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Write an algorithm to evaluate an infix expression in Java. Calculate its running time
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Improve the algorithm by using two for loops instead of three, and analyze the running time...
Improve the algorithm by using two for loops instead of three, and analyze the running time of your improved algorithm asymptotically. Show the modified code. Algorithm FactorSum(A, n) Input: Array A of n real numbers. S ← 0 for i ← 0 to n - 1    for j ← i to n - 1       P ← 1       for k ← i to j          P ← P x A[k] S ← S +...
List the factors that influence consumer behavior as stated in chapter 3 - describe how these...
List the factors that influence consumer behavior as stated in chapter 3 - describe how these factors impact marketing within the hospitality industry. Cite your references. Book - Hospitality Marketing Management by Bojanic, David C.; Reid, Robert D. Edition: 6 ISBN: 9781119195122
What factors affect the magnitude of the time value of money?
What factors affect the magnitude of the time value of money?
What social factors affect your family life? In what ways is your family life different from...
What social factors affect your family life? In what ways is your family life different from that of your grandparents when they were your age?
In addition to the five factors discussed in the chapter, dividends also affect the price of...
In addition to the five factors discussed in the chapter, dividends also affect the price of an option. The Black–Scholes option pricing model with dividends is:    C=S × e−dt × N(d1) − E × e−Rt × N(d2)C=S⁢ × e−dt⁢ × N(d1)⁢ − E⁢ × e−Rt⁢ × N(d2) d1= [ln(S  /E ) +(R−d+σ2 / 2) × t ] (σ − t√) d1= [ln(S  /E⁢ ) +(R⁢−d+σ2⁢ / 2) × t ] (σ⁢ − t)  d2=d1−σ × t√d2=d1−σ⁢ × t   ...
In addition to the five factors discussed in the chapter, dividends also affect the price of...
In addition to the five factors discussed in the chapter, dividends also affect the price of an option. The Black-Scholes option pricing model with dividends is: C=S × e−dt × N(d1) − E × e−Rt × N(d2) d1= [ln(S  /E ) +(R−d+σ2 / 2) × t ] (σ − t√)  d2=d1−σ × t√ All of the variables are the same as the Black-Scholes model without dividends except for the variable d, which is the continuously compounded dividend yield on the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT