Determine the best threshold size for the combined

Assignment Help C/C++ Programming
Reference no: EM131264407

Assignment: Sort Wars

If quicksort is so quick, why bother with anything else? If bubble sort is so bad, why even mention it? For that matter, why are there so many sorting algorithms?

Your mission (should you choose to accept it) is to investigate these and other questions in relation to the algorithms selection sort, insertion sort, merge sort, and quicksort.

Core Questions

1. Explain each of the algorithms in a way that would be understandable to an intelligent person who is not familiar with programming. You should not use any code (or even pseudo code) in your explanation, but you will probably need to use general concepts such as "compare" and "swap", and you'll certainly need to use procedural words such as "if" and "repeat".

You might find it helpful to consider an algorithm as if it were a game for which you need to define the rules. For example, here's how you could describe the bubble sort algorithm as if it was a solitaire game played with a deck of cards that contain the values to process.

2. Write a set of guidelines for helping a fellow programmer decide which sort algorithm would be most appropriate for a particular situation. Include in your guidelines a description of the advantages and disadvantages of each algorithm, together with an indication as to why those characteristics apply. Your goal is to provide enough information so that someone not familiar with the details of each algorithm would be able to decide which algorithm is right for them.

For example, if someone was considering using counting sort, then the following brief information could help decide if it was appropriate.

Algorithm

Counting Sort

Description

  • Count the number of times each different value appears, then overwrite the values back into the list in lowest-to-highest order, with each value repeated according to the counts. For example, if the value 42 appears 5 times, then you would write 42 into the sorted list 5 times.

Advantages

  • Usually faster than any of the comparison-based sorts. Algorithmic complexity is O(n + k), irrespective of data order, where n is the list length and k is the number of distinct values that might occur. Typical case is where k << n, in which case cost is O(n).
  • Simple to code.

Disadvantages

  • Only usable where the values to be sorted can be used to index an array of value counts, which usually means the values are integers over a small range. In other words, the algorithm can't be used to sort common non-integral values such as strings and floats, and it's inappropriate even for integers if the range of values is large.
  • Requires an auxiliary array (to store the counts) of size equal to the number of different possible sort values. If the range of values is large, the cost of allocating and maintaining this array could be significant.

When to use

  • If your circumstances allow, it's hard to beat this algorithm. But because it places very tight restrictions on the nature of the data to sort, you will often have to choose another approach.

Questions

In this section, you'll need to be able to measure the speed of execution of parts of your code. On a Unix-based system, you can measure how much time a section of code takes by calling the system function getrusage before and after that section. The function returns information about various aspects of resource usage, including the amount of system time (time taken by system routines that you call) and the amount of user time (time taken by your own code). Note that this is process time, not "wall-clock" time, so it's an accurate measure even if the system is busy executing other people's code as well. Consult the documentation for getrusage if you need more information.
#include <sys/resource.h>
intmain() {
struct rusage before, after; // for recording usage stats
// do any needed initialisations getrusage(RUSAGE_SELF, &before); // execute the code you want to time getrusage(RUSAGE_SELF, &after);
int secs = after.ru_utime.tv_sec - before.ru_utime.tv_sec; intusecs = after.ru_utime.tv_usec - before.ru_utime.tv_usec; cout<< secs * 1000000 + usecs<<endl; // in microseconds }
Practical sort implementations usually combine more than one sorting algorithm, attempting to take advantage of the best characteristics of each. For example, a straightforward but effective approach for general-purpose sorting is to use quicksort with the recursion stopping when the partitions reach a threshold size, then a final insertion sort pass to complete the process. The structure of the hybrid sort would look like this:
sort (list) {
sort list with truncated quicksort sort list with insertion sort
}
truncated quicksort (list) {
if list size is greater than threshold {
partition list
recursively sort first part with truncated quicksort recursively sort second part with truncated quicksort
} }

This approach is generally faster that using pure quicksort because insertion sort has a lower overhead than quicksort and is thus faster, provided the elements in the list are not far from their correct positions. To get the greatest speedup, the threshold for truncating the quicksort needs to be carefully chosen: too large, and the greater algorithmic cost of the insertion sort will overwhelm any lower overheads; too small, and the potential benefits of the combined approach are wasted.

3. Design an experiment to determine the best threshold size for the combined "quicksort-plusinsertion-sort" implementation. You'll need to consider a range of data sizes, including both random and "worst-case" data sets.

Write a program that could be used to perform the experiment. You'll need to provide the sort code itself (use your code from prac 5) as well as a suitable main function for testing it (adapt the main function from prac 5).

Your experimental design should be sufficiently detailed that you could hand the task over to a tester who is not familiar with sorting algorithms or even with programming. Ideally, the tester should only need to run the program under specified conditions and record the results.

4. Run your experiment and report on the findings. Your report should include the data you gather, an analysis of that data, and a clear recommendation as to the best cutover threshold.

Consider how best to present your results. You'll certainly want to tabulate the data, but you might also find it helpful to plot it as well. Because the actual times will be heavily dependent on the data size, you might find it useful to normalise the times against the "ideal" time (by dividing by n log n) before plotting them.

Reference no: EM131264407

Questions Cloud

Who is correctly saving their files to a cd : When prompted, Mary Ann will click on Burn files to disc, indicate how she'll use the files, click on Burn to disc, and indicate the recording speed. Who is correctly saving their files to a CD?
Write a function called sort that takes in two parameters : Write a function called Sort that takes in two parameters. Write a function called Reverse that takes in two parameters. Write a function called Delete that takes in three parameters.
Analyze the case and conduct a value chain analysis : What does Coke do that adds value to the Coca-Cola Company. - Analyze the case and conduct a Value Chain Analysis (VCA) for Coca Cola Company.
Write a program that display a menu student can select from : Write a program that will help a student practice basic math (addition, subtraction, multiplication, and division). Display a menu the student can select from.
Determine the best threshold size for the combined : Design an experiment to determine the best threshold size for the combined "quicksort-plusinsertion-sort" implementation. You'll need to consider a range of data sizes, including both random and "worst-case" data sets.
Design an application that has an array of twenty integers : Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to locate one of the values.
Discuss the importance of client follow-up : Discuss the importance of client follow-up and client referral in a Real Estate Agent's career. Describe your plan to develop a solid client follow-up and client referral strategy to ensure your success as a Real Estate Agent
What you have learned about fddi : Using Microsoft Word, write a two to three paragraph summary of what you have learned about FDDI. Include all of the URL addresses of the links that you used at the end of your summary.
Explain task that would occur in each step if use sdlc model : Consider a different SDLC Model (4 step or 12 step). Describe the events/tasks that would occur in each step if your company were to use this SDLC model instead. - 2 pages

Reviews

len1264407

11/3/2016 4:51:40 AM

Your answer for each question should be around 400 to 500 words (around 1 typed page), not including figures and code listings. Your submission should conform to accepted practices for academic writing. Of course, you must give appropriate acknowledgement to any material that you use or reference. Submit written material as PDF documents to the appropriate handins on FLO. Submit source code (where appropriate) to the separate code handin as a zip file. Scoring The "core" score will be based on the 2 "core" questions, and the "extension" score will be based on the 2 "extension" questions.

Write a Review

C/C++ Programming Questions & Answers

  Calculate the plane coefficients

Calculate the plane coefficients (A,B,C and D)  of 3 points in a plane defined by P1, P2 and P3, and determine if the point P4 is behind or in front of the polygon surface contained within that plane.

  Program to compute with two numbers.

I have the 1st two statements figured out, and had the arithmetic figured out based on results of 22 and 16, but turns out I interpreted it wrong and results should be what's posted above when the user changes the two numbers. The tutorial says no..

  Create a program that will round a floating point

Create a program that will round a floating point value to a specified number of decimal places as input by the user. To perform the actual calculation, write a function that takes the user's value and the number of decimal places as input paramet..

  Program to determine the smallest number out of 15 numbers

you are a mortgage broker who is determining whether you should provide a mortgage to perspective home purchasers. If you are willing to provide a mortgage, you need to decide how much you should charge the borrower

  What are the sizes in memory of data types

What are the sizes in memory of other data types in C++? I mean, I know that a double is 8 bytes and an int is 4 bytes. What si the size of a Char and a String?

  Describe the class structure of c++ programming

Describe the class structure of C++ programming. What are the key contents of classes

  Research paper on arrays and use single dimension arrays

Create codes for the following problem/s. Compile, test run, and edit them if necessary. Include your original .cpp and related file(s) (if any) in a single folder. Compress (zip) the folder and submit the single zipped folder.

  Write program that prints the reverse of the string entered

Write a C program that prints the reverse of the string entered

  Determines the largest value

Write a program Largest that reads three integers from the user and determines the largest value - Write a program InOrder that reads three integers from the user and prints the three integers in sorted order.

  Const int num_years

How do you get this program to get both player input and then display results(As described on bottom) #include  const int NUM_YEARS=15;//The number of years const int NAME_SIZE=32;//The max size of the player name string

  Function declare that it can throw an ioexception

Every C++ function that performs file I/O will thus need to either declare that it can throw an IOException, or contain a try-catch-finally block to deal with it. Response?

  Use a loop in main to efficiently handle all three persons

Plan and finish writing the main program, and all the functions, including a constructor. Use a loop in main to efficiently handle all three persons' data.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd