0

I'm a beginner in C++ (started 1 week ago).

I am building a simple program to sort a vector of values, that lets the user choose which sorting algorithm to use and keeps track of running time for each sorting method.

So far I did the bubble sort and selection sort algorithms. The program itself gives no warnings nor errors, but it gets stuck once the vector of values has been formed and it needs to be sorted. In fact, I believe that the call to the bubble sort / selection sort function never materializes.

It is likely a rookie mistake, anyone can help?

Here is the code:

main.cpp

#include <iostream>
#include <string>
#include <locale>
#include <vector>
#include "bubblesort.h"
#include "selectionsort.h"

using namespace std;

struct sorting_system {
    int ID;
    string name;
};

vector<int> populateArray();

int main (int argc, char** argv)
{
    setlocale(LC_ALL, "");

    time_t starting_time = 0, ending_time = 0;

    std::cout << "Scegli dalla legenda il tipo di ordinamento che vuoi effettuare: \n";
    std::cout << "1. Bubble Sort" << endl;
    std::cout << "2. Selection Sort" << endl;
    std::cout << "3. Insertion Sort" << endl;
    std::cout << "4. Merge Sort" << endl;
    std::cout << "5. Quick Sort" << endl;
    std::cout << "0. Termina l'applicazione" << endl;

    sorting_system sort;

    std::cin >> sort.ID;

    do{
        switch(sort.ID)
        {
        case 1: {
            sort.name = "Bubble Sort";

            vector<int> array_to_be_sorted;

            array_to_be_sorted = populateArray();
            starting_time = time(0);

            bubblesort bs;
            bs.BubbleSort(array_to_be_sorted);
            ending_time = time(0);
            }
        case 2: {
            sort.name = "Selection Sort";
            vector<int> array_to_be_sorted;

            array_to_be_sorted = populateArray();
            starting_time = time(0);

            selectionsort ss;
            ss.SelectionSort(array_to_be_sorted);
            ending_time = time(0);
            }
        /* (... other algorithms (case 3, 4, 5) ...) */
        case 0: {
            cout << "Terminazione dell'applicazione." << endl;
            break;
            }
        default:
            cout << endl << "Error in menu input. Valid menu options are 0 to 5." << endl;
            break;
        }

        "Tempo trascorso per il metodo ", sort.name, " : ", ending_time - starting_time,".\n";

    } while (sort.ID != 0);

    return 0;
}


vector<int> populateArray()
{
    int size;
    cout <<  endl << "Enter the size of the list with integers:" << endl ;
    cin >> size;

    vector<int> numbers_array(size);
    int random_number;

    for (int i = 0; i < size; i++) 
    {
        random_number = rand()%size + 1;
        numbers_array[i] = random_number;
    } 
    return numbers_array;
}

bubblesort.h

#pragma once
#include <vector>

using std::vector;

class bubblesort
{
public:
    bubblesort(void);
    ~bubblesort(void);
    void BubbleSort (std::vector<int> array_to_be_sorted);
};

bubblesort.cpp

#include "bubblesort.h"
#include <iostream>
#include <vector>


using std::vector;


bubblesort::bubblesort(void){}

bubblesort::~bubblesort(void){}

void bubblesort::BubbleSort(vector<int> array_to_be_sorted)
{
    int temp = 0;
    for (int j = 0; j < array_to_be_sorted.size() - 1; j++ )
    { 
        for (unsigned int i = 0; i < array_to_be_sorted.size() - 1; i++)
        {
            if (array_to_be_sorted[i] > array_to_be_sorted[i + 1])
            {
                temp = array_to_be_sorted[i + 1];
                array_to_be_sorted[i + 1] = array_to_be_sorted[i];
                array_to_be_sorted[i] = temp;
            }
        }
     }
     std::cout << "La sequenza ordinata è la seguente: ", array_to_be_sorted, ".\n";
}
  • I don't know what you think that `std::cout << "La sequenza ordinata è la seguente: ", array_to_be_sorted, ".\n";` will do, but it won't print what you probably think it would. Furthermore the `BubbleSort` function will only attempt to sort it's own copy of the vector, since you pass it by value. Also bubblesort can't be done using a single loop. – Some programmer dude Jan 04 '20 at 17:01
  • 1
    `bubblesort::bubblesort(void) { } bubblesort::~bubblesort(void) { }` <-- don't do that. It's inefficient. Simply declare those functions as `=default` in the class declaration (and ditch the redundant `void`: `bubblesort() = default; ~bubblesort() = default;` . – Jesper Juhl Jan 04 '20 at 17:03
  • 5
    As for your problem, I suggest you take some time to learn how to use a *debugger*. With a debugger you can step through your code, statement by statement, while monitoring variables and their values. This way you can easily see what happens and where things go wrong. – Some programmer dude Jan 04 '20 at 17:04
  • 2
    Oh, and the statement `"Tempo trascorso per il metodo ", sort.name, " : ", ending_time - starting_time,".\n";` won't do anything useful at all. The things mentioned above, this, and more, indicates that you could need to spend some more time with [a good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Jan 04 '20 at 17:05
  • @Some programmer dude thanks for the feedback, I corrected that stupid mistake in the bubblesort algorithm. The problem is really in how I passed the vector to the function. Thanks for the books suggestions! –  Jan 04 '20 at 17:27
  • Inside your loop the `sortI.ID` can not be changed so the loop will keep repeating the last selection. And since there are no breaks in the case statements if you selected case 1 it does 2 and 0 as well. – drescherjm Jan 04 '20 at 17:47
  • 2
    Note: `time_t`'s resolution is typically 1 second. To accurately measure work being done by a computer that can do billions of things a second, 1 second is usually not good enough unless you give it billions of things to do. You may find you finish sorting in under a second unless the list you're sorting is quite large. – user4581301 Jan 04 '20 at 17:47

1 Answers1

0

Your program is expecting more input, that's why it looks like stuck to you. Notice you're not breaking out of your switch case like this:

    ending_time = time(0);
    break; //Notice this line
        }
    case 2: {

Moreover as suggested in comments you should pass vector by reference like this void bubblesort::BubbleSort(vector<int> &array_to_be_sorted). Your Bubble sort algorithm needs to be fixed as well. Hint : It requires two for loops.

H S
  • 1,211
  • 6
  • 11