Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

The program sorting.cpp contains a main function for testing the operation of several sort algorithms over various data sizes and data set organisations. The program understands the following options:

-i insertion sort

-s selection sort (the default)

-q quicksort

-a (almost) sorted data set

-v reverse-sorted data set

-r random data set (the default)

n generate a data set of size n (default 10000)

Background

In part 3 you will need to gather information about the number of times that various parts of the code is executed. On Unix-based systems (including Linux and cygwin), the simplest approach is to use execution profiling tools. To gather profiling data, first compile the program with the -pg option, which will cause it to write information about its execution into a file (called gmon.out).

Then each time you run the program, you can use the profile analysis tool (gprof) to report on the execution counts of each called function.

g++ -pg -o sort sort.cpp

./sort // run with default arguments

gprof -b // -b means "brief" output

./sort -qr 1000000 // quicksort on one million random values

gprof -b // profile for second run

...

If the system that you're using doesn't support profiling, a work around is to add code to the program to increment a counter at each point of interest, then print the values of the counters at the end of execution.

Selection Sort

1. Implement the selectionSort and insertionSort functions. The program checks that the final list is sorted correctly, so you can test your code using try with task  sorting.

Quicksort

2. Implement the quickSort function. The real work of quicksort happens in the partition operation, so it's probably best to define a separate function to do the partitioning.

 Profiling

3. Gather data about the number of times that the innermost loops of each sort algorithm is executed. A simple way to count inner loop executions is to call a "do nothing" function from the inner loop, then look for the execution count of that function in the profiler output.

void inner() {} // for gprof to count

void ...Sort() {

for (...) {

for (...) {

...

inner();

}

}

}

When you have profiling working to your liking, run the program and record the inner loop counts for each combination of algorithm (-i, -s, and -q) and data organisation (-a, -v, and -r), beginning with a data size of 1000 and increasing progressively until execution takes around 30 seconds. A suitable set of size increments would be 1000, 2000, 5000, 10000, 20000 and so on.

For each run, compute a normalised execution count by dividing the actual count by the data set size. For example, if quicksort with a random data set containing 10,000 elements executed its inner loop 161,619 times, then the normalised execution count would be 161610/10000, or about 16.1

Finally, plot the 9 sets of data (for the options -ia, -iv, -ir, -sa, -sv, -sr, -qa, -qv, and -qr) as separate lines on a single set of axes, with data set size on the horizontal axis and normalised execution count on the vertical axis. You'll need to choose scales so that the full range of values can be displayed, and label the lines so you know which one is which. You may also find it worthwhile using a log-log plot to better show the values over the full range.

Improving Quicksort

4. A simple implementation of quicksort is likely to perform badly for certain data sets. For example, the implementation used as an example in lectures performs very poorly if the data set is in reverse-sorted order.

The poor performance can be avoided by choosing a better pivot for the partitioning step.

Common strategies include selecting a random element or finding the median of the first, middle, and last elements. In any case, the chosen pivot is swapped with the last element before proceeding to do the partition as before.

Read about and implement one of the improvements to quicksort and show (by adding its profile data to the graph of part 3) how much improvement you have achieved.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9528912

Have any Question?


Related Questions in C/C++

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As