Question

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.

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

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

}

void quickSort(int *first, int *last)

{

    // add code here

}

int main(int argc, char **argv)

{

    string algorithm = "selection";

    string dataset   = "random";

    for (int c; (c = getopt(argc, argv, "ravqsin")) != -1;) {

        switch (c) {

        case 'r':

            dataset = "random";

            break;

        case 'a':

            dataset = "sorted";

            break;

        case 'v':

            dataset = "reverse";

            break;

        case 'q':

            algorithm = "quicksort";

            break;

        case 's':

            algorithm = "selection";

            break;

        case 'i':

            algorithm = "insertion";

            break;

        case 'n':

            algorithm = "none";

            break;

        }

    }

    argc -= optind;

    argv += optind;

    const int size = argc > 0 ? atoi(argv[0]) : 10000;

    int *data = new int[size];

    if (dataset == "sorted") {

        for (int i = 0; i < size; ++i) {

            data[i] = i;

        }

    }

    else if (dataset == "reverse") {

        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;

        }

    }

    if (algorithm == "quicksort") {

        quickSort(data, data + size);

    }

    else if (algorithm == "selection") {

        selectionSort(data, data + size);

    }

    else if (algorithm == "insertion") {

        insertionSort(data, data + size);

    }

    for (int i = 1; i < size; i++) {

        if (data[i] < data[i-1]) {

            cout << "Oops!" << '\n';

            exit(1);

        }

    }

    cout << "OK" << '\n';

    cout << "Algorithm:   " << algorithm << '\n';

    cout << "Data set:    " << dataset << '\n';

    cout << "Size:        " << size << '\n';

    return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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
   int t = *a;
   *a = *b;
   *b = t;

}

void selectionSort(int *first, int *last)

{

    // add code here
   int min_element;
   int size = *(&first+1)-first;   // calculating the size of array
  
   for(int i=0;i<size-1;i++)
   {
       min_element = i;           // it will store the index of minimum element
      
       for(int j=i+1;j<size;j++)
       {
           if(*(first+j) < *(first+min_element))
           {
               min_element = j;
           }
       }
      
       swap(first+min_element,first+i);// call swap at the end of inner loop to exchange value of minimum to its right place
   }

}

void insertionSort(int *first, int *last)

{

    // add code here
   int size = *(&first+1)-first;   //calculating size of array

    for (int i = 1; i < size; i++)
    {
        
        int j = i - 1;               
  
       int pivot = *(first+i);   // it will store the element which is to be placed in array at its right position

       for(;j>=0 && *(first+j)>pivot;j--)
       {
           *(first+j+1) = *(first+j);   // shifting elements one position till we find the right place for pivot
       }
       *(first+j+1) = pivot;
    }

}

// function to find the divide point of array
int divide(int *first, int start, int end)
{
   int x = *(first+end); // we will always choose last element of array as the divide point
  
   int i = start-1;
  
   for(int j=start ;j<end-1;j++)
   {
       if(*(first+j) < x)
       {
           i++;
           swap(first+i,first+j);
       }
   }
   swap(first+i+1,first+end);   // swap the last element which was the divide point in our case with the actual position
   return i+1;       // return i+1 which is the current divide point on left side elements are smaller and on right bigger
}
void quicksort_divide(int *first,int start,int end)
{
   if(start < end)
   {
       int pivot = divide(first,start,end);   // call to partition which will return the poisition for pivot to divide array
      
       quicksort_divide(first,start,pivot-1); // recursive call to left array for division
       quicksort_divide(first,pivot+1,end);   // recursive call to right array for division
      
   }
}

void quickSort(int *first, int *last)

{

    // add code here
   int size = *(&first+1)-first;   // calculating size of array
  
   quicksort_divide(first,0,size-1);

}

int main(int argc, char **argv)

{

    string algorithm = "selection";

    string dataset   = "random";

    for (int c; (c = getopt(argc, argv, "ravqsin")) != -1;) {

        switch (c) {

        case 'r':

            dataset = "random";

            break;

        case 'a':

            dataset = "sorted";

            break;

        case 'v':

            dataset = "reverse";

            break;

        case 'q':

            algorithm = "quicksort";

            break;

        case 's':

            algorithm = "selection";

            break;

        case 'i':

            algorithm = "insertion";

            break;

        case 'n':

            algorithm = "none";

            break;

        }

    }

    argc -= optind;

    argv += optind;

    const int size = argc > 0 ? atoi(argv[0]) : 10000;

    int *data = new int[size];

    if (dataset == "sorted") {

        for (int i = 0; i < size; ++i) {

            data[i] = i;

        }

    }

    else if (dataset == "reverse") {

        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;

        }

    }

    if (algorithm == "quicksort") {

        quickSort(data, data + size);

    }

    else if (algorithm == "selection") {

        selectionSort(data, data + size);

    }

    else if (algorithm == "insertion") {

        insertionSort(data, data + size);

    }

    for (int i = 1; i < size; i++) {

        if (data[i] < data[i-1]) {

            cout << "Oops!" << '\n';

            exit(1);

        }

    }

    cout << "OK" << '\n';

    cout << "Algorithm:   " << algorithm << '\n';

    cout << "Data set:    " << dataset << '\n';

    cout << "Size:        " << size << '\n';

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Can I get some help with this question for c++ if you can add some comments...
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
  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    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 #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Hi, need this question ansered in c++, has multiple levels will post again if you can...

    Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that. here is a sketch of the program from the screenshot int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -=...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • Can someone help with this C++ code. I am trying to compile and I keep running...

    Can someone help with this C++ code. I am trying to compile and I keep running into these 4 errors. #include <iostream> #include <cassert> #include <string> using namespace std; typedef int fsm_state; typedef char fsm_input; bool is_final_state(fsm_state state) { return (state == 3) ? true : false; } fsm_state get_start_state(void) { return 0; } fsm_state move(fsm_state state, fsm_input input) { // our alphabet includes only 'a' and 'b' if (input != 'a' && input != 'b') assert(0); switch (state) {...

  • How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the lin...

    How can i make a counter for the number of exchanges made in the linear algorithm?? The binary counter works but the linear doesn't. Here's my code. #include <iostream> using namespace std; void selectionSort(int[], int, int& ); void showSelection(int[], int); void sortArray(int[], int, int&); void showArray(const int[], int); int main() {    int values[25] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • C++ problem. Please just find and fix the errors and highlight it afterwards. Do not add...

    C++ problem. Please just find and fix the errors and highlight it afterwards. Do not add any new comments or remove comments from teachers. Thank you very much *R 2B PROGRAM 1B: INSERTION SORT Find and rix errors. Ron the program once and save the outpat as a conment at the end of the source file Changed by: IDE #include <iostream> using namespace std: void insertionSort (int aryll, int size) int main double list(1001-(50.1, 30.2, 80.3, 10.5, 30.2, 40.9, 90.8,...

  • Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your...

    Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...

  • The provided code is my solution, stripped of the details needed to make it work. It...

    The provided code is my solution, stripped of the details needed to make it work. It is not a “good” program. It lives in a single file, does not use classes, and it has those evil global variables. That is by design. I want to to craft code. Use my code as a guide to help you put together the needed parts. #include #include #include // defaults const int MAX_STEPS = 100; // how long do we run the simulation...

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