Program of sorting algorithms , C/C++ Programming

Assignment Help:

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.


Related Discussions:- Program of sorting algorithms

Describe spaghetti programming, Describe spaghetti programming. - Spagh...

Describe spaghetti programming. - Spaghetti programming refers to codes which tend to get tangled and overlapped throughout the program. - It makes a program complex and ana

Explain register variable, Explain Register Variable Computers have int...

Explain Register Variable Computers have internal registers, which are used to keep data temporarily, before any operation can be performed. Intermediate results of the calcula

Reverse digit function, how can i write reverse digit function like writing...

how can i write reverse digit function like writing 1234 and printing 4321

#include, Write a program to find the area under the curve y = f(x) between...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Define auto pointer?, A: The simplest instance of a smart pointer is auto_p...

A: The simplest instance of a smart pointer is auto_ptr that is included in the standard C++ library. Auto Pointer only takes care of Memory leak & does nothing regarding dangling

Write a c program that accepts the year and weight , Based on the automobil...

Based on the automobile's model year and weight, the city of Cebu determines the car's weight class and registration fee using the following schedule: Model Year Registration

Solution, Damjibhai and Shamjibhai are two jeweler friends. They decide to ...

Damjibhai and Shamjibhai are two jeweler friends. They decide to play a simple game. The game comprises of removing the jewels for polishing, turn by turn. Once a jewel is removed

Define commonly used built-in library functions, Define Commonly Used Built...

Define Commonly Used Built-in Library Functions? Comprise opened a file pointer you will desire to use it for either input or output. The C language supplies a set of functions

Decode the code, Smugglers are becoming very smart day by day. Now they hav...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Write Your Message!

Captcha
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