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";
}