Question

Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets.

Need Help With this Sorting Algorithm task for C++

Base Code for sorting.cpp is given.

The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons

Practical 5: Sorting to Task Winks poyan er impje of the algorithms for a data sets

Background The skeleton program sorting.cpp contains a main function for testing the operation of several sort algorithms ove

The merge sort code shows the idea. Run your program and record the number of compares and swaps for each combination of algo

#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;
}
Practical 5: Sorting to Task Winks poyan er impje of the algorithms for a data sets
Background The skeleton 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: insertion sort -s selection sort (the default) -qquicksort merge sort -a already-sorted data set - reverse-sorted data set -r random data set (the default) generate a data set of size n (default 10,000) 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'll need to change it from passing the data using an array and 2 indexes to passing it using two pointers Level 2: Quicksort 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. Level 3: Profiling For this checkpoint you need to gather data about the number of times that each algorithm compares or swaps items. A simple strategy is to use functions to do the count variables inside the functions. The skeleton program provides the functions counted_less and counted_swap for just this purpose. To use them, modify your code to call the functions where appropriate then uncomment the code that displays the final values of the count variables. comparisons and swaps, and then increment
0 0
Add a comment Improve this question Transcribed image text
Answer #1

to a Series made eyoithn takes Serting input instructions aperotions List he pefaoms Cadiol it Apeiped just means gaeory anfelogeli temp Al AL ALit AStil If 20 tempy 21 22 4 4l1a9 23 printf ( Rourdd n 1ourd) 25 return 26 27 output LL89 11 1S 29 345

Add a comment
Know the answer?
Add Answer to:
Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

  • Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number...

    Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...

  • Write a program that compares the execution speed of two different sorting algorithms: bubble sort and...

    Write a program that compares the execution speed of two different sorting algorithms: bubble sort and selection sort. It should do this via functions you must write with the following prototypes: void setRandomValues(int[], int[], int length); This function sets two integer arrays with identical random values. The first two arguments are two integer arrays of the same length, and the third argument is the length of those arrays. The function then sets each element in the first and second argument...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT