Question

In: Computer Science

Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative...

Task:

Write a program that implements several sorting algorithms and use it to demonstrate the comparative performance of the algorithms for a variety of datasets.

Background

The skeleton program sorting.cpp contains a main function for testing the operation of several sort algorithms over various data sizes and dataset organisations.

The program understands the following arguments:

-i insertion sort

-s selection sort (default)

-q quicksort

-a (already) sorted dataset

-v reverse-sorted dataset

-r random dataset (default)

-n no sorting

x generate a dataset of size x (default 10000)

Level 1: Basic sorts

Implement the selectionSort and insertionSort functions. Note that you can base your code on the sample code used in lectures, although you will need to modify it from passing the data using an array and two indexes to passing it using two pointers. The program will check that the final list is sorted correctly

Level 2: Quicksort

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

Level 3: Profiling

Gather data on the number of times that each algorithm compares and swaps items. A simple strategy is to use variables to count the comparisons and swaps. The skeleton code declares comparisons and swaps variables for just this purpose. To make use of them, modify your code to increment each variable where appropriate and then uncomment the output statements to display their final values.

When you have profiling working, run the program and record the number of compares and swaps for each combination of algorithm (-i, -s, and -q) and dataset organisation (-a, -v, and -r). Use dataset sizes of 1000, 2000, 5000, 10,000, 50000, and 100000. In the worst case, execution should not take much longer than 30 seconds.

For each run, compute a normalised execution count by dividing the actual count by the dataset size. For example, if quicksort with a random dataset containing 10000 elements performed 161619 comparisons, the normalised count would be 161619/10000 or about 16.2.

Finally, plot the comparison results (for the options -ia, -iv, -ir, -sa, -sv, -sr, -qa, -qv, and -qr) as separate lines on a single set of axes with dataset size on the horizontal axis and execution count on the vertical axis. Repeat for swaps. 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 find that using a log-log plot shows the values over the full range better

Level 4: Improving quicksort

A simple implementation of quicksort is likely to perform badly for certain datasets. For example, the implementation used as an example in lectures performs very poorly if the dataset 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 using 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 graphs of Level 3) how much improvement you have achieved. Include a brief description of the method you implemented to improve quicksort’s performance.

Solutions

Expert Solution

#include <cstdlib>
#include <iostream>

#include <getopt.h>
using namespace std;

long compares; // for counting compares
long swaps;    // for counting swaps

bool counted_less(int n1, int n2)
{
    ++compares;
    return n1 < n2;
}

void counted_swap(int &n1, int &n2)
{
    ++swaps;
    int t = n1;
    n1 = n2;
    n2 = t;
}

void selectionSort(int *start, int *stop)
{
    // add code here
}

void insertionSort(int *start, int *stop)
{
    // add code here
}

void quickSort(int *start, int *stop)
{
    // add code here
}

void merge(int *start, int *mid, int *stop)
{
    int temp[stop - start];

    int *i = start;
    int *j = mid;
    int *k = temp;

    while (i < mid || j < stop) {
        if (i == mid) {
            counted_swap(*k++, *j++);
        } else if (j == stop) {
            counted_swap(*k++, *i++);
        } else if (counted_less(*j, *i)) {
            counted_swap(*k++, *j++);
        } else {
            counted_swap(*k++, *i++); // bias to left for stability
        }
    }

    for (i = start, k = temp; i != stop; ++i, ++k) {
        counted_swap(*i, *k);
    }
}

void mergeSort(int *start, int *stop)
{
    if (stop - start <= 1)
        return;
    int *mid = start + (stop - start) / 2;
    mergeSort(start, mid);
    mergeSort(mid, stop);
    merge(start, mid, stop);
}

int main(int argc, char **argv)
{
    string algorithm = "selection sort";
    string dataset   = "random";
    int size         = 1000;

    for (int c; (c = getopt(argc, argv, "ravmqsi")) != -1;) {
        switch (c) {
        case 'r':
            dataset = "random";
            break;
        case 'a':
            dataset = "already sorted";
            break;
        case 'v':
            dataset = "reversed";
            break;
        case 'm':
            algorithm = "merge sort";
            break;
        case 'q':
            algorithm = "quicksort";
            break;
        case 's':
            algorithm = "selection sort";
            break;
        case 'i':
            algorithm = "insertion sort";
            break;
        }
    }

    argc -= optind;
    argv += optind;

    if (argc > 0)
        size = atoi(argv[0]);

    int *data = new int[size];

    if (dataset == "already sorted") {
        for (int i = 0; i < size; ++i)
            data[i] = i;
    } else if (dataset == "reversed") {
        for (int i = 0; i < size; ++i)
            data[i] = size - i - 1;
    } else if (dataset == "random") {
        for (int i = 0; i < size; ++i)
            data[i] = rand() % size;
    }

    compares = 0;
    swaps    = 0;

    if (algorithm == "merge sort") {
        mergeSort(data, data + size);
    } else if (algorithm == "quicksort") {
        quickSort(data, data + size);
    } else if (algorithm == "selection sort") {
        selectionSort(data, data + size);
    } else if (algorithm == "insertion sort") {
        insertionSort(data, data + size);
    }

    for (int i = 1; i < size; ++i) {
        if (data[i] < data[i - 1]) {
            cout << "Oops!" << endl;
            exit(1);
        }
    }

    cout << "Algorithm: " << algorithm << endl;
    cout << "Dataset:   " << dataset << endl;
    cout << "Size:      " << size << endl;

    // uncomment for checkpoints 3 and 4
    // cout << "Compares:  " << compares << endl;
    // cout << "Swaps:     " << swaps << endl;
}

Related Solutions

Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions. Counter...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter...
Write a program that implements the FIFO, LRU, and Optimal page replacement algorithms presented in chapter 8 of your text. First generate a random page-reference string (this should be 20 entries long) where page numbers range from 0 to 9. Apply the random page-reference string to each algorithm, and record the number of page faults incurred by each algorithm. Implement the replacement algorithms so that the number of page frames goes from 1 to 7 and you must compute the...
Write an MSP430 assembly language program that implements the following 2 algorithms: 2) a macro called...
Write an MSP430 assembly language program that implements the following 2 algorithms: 2) a macro called "vdot" that calculates the "dot product" of two vectors "a" and "b", implemented as “arrays” (following the “C” language convention), of 3 elements. the macro should receive 2 pointers to the first element of each vector and return the result in R13.
Please use Java You have to write a programing for the following sorting algorithms in increasing...
Please use Java You have to write a programing for the following sorting algorithms in increasing order and print required steps in order to explain how your sorting algorithms work. 3. Implement the merge sort algorithm. With answers in this link (https://www.chegg.com/homework-help/questions-and-answers/write-programing-following-sorting-algorithms-increasing-order-print-required-steps-order--q53916147?trackid=PJ_TOK85), answer the above questions.
Write a Java program (use JDBC to connect to the database) that implements the following function...
Write a Java program (use JDBC to connect to the database) that implements the following function (written in pseudo code): (20 points) CALL RECURSION ( GIVENP# ) ; RECURSION: PROC ( UPPER_P# ) RECURSIVE ; DCL UPPER_P# ... ; DCL LOWER_P# ... INITIAL ( ' ' ) ; EXEC SQL DECLARE C CURSOR FOR SELECT MINOR_P# FROM PART_STRUCTURE WHERE MAJOR_P# = :UPPER_P# AND MINOR_P# > :LOWER_P# ORDER BY MINOR_P# ; print UPPER_P# ; DO "forever" ; EXEC SQL OPEN C...
Can you write a small program in c++ demonstrate the use of pointer (*) & (&)....
Can you write a small program in c++ demonstrate the use of pointer (*) & (&). Can you also explain the pointer system and how to use them. Thank you.
write the program in java. Demonstrate that you understand how to use create a class and...
write the program in java. Demonstrate that you understand how to use create a class and test it using JUnit Let’s create a new Project HoursWorked Under your src folder, create package edu.cincinnatistate.pay Now, create a new class HoursWorked in package edu.cincinnatistate.pay This class needs to do the following: Have a constructor that receives intHours which will be stored in totalHrs Have a method addHours to add hours to totalHrs Have a method subHours to subtract hours from totalHrs Have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT