I've written an interrupted bubble sort, which prints the intermediate result after some specified number of iterations of bubble sort. But I seem to have an speed issue. Could anyone help me spot some places that need to be optimized or are just plain wrong?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ifstream;
using std::stoi;
using std::stringstream;
template<typename T>
void printVector(vector<T> vect) {
for(int i = 0; i < vect.size(); i++) {
if(i == 0) {
cout << vect[i];
} else {
cout << " " << vect[i];
}
}
cout << endl;
}
template<typename T>
void swap(T &elementOne, T &elementTwo) {
T temp = elementOne;
elementOne = elementTwo;
elementTwo = temp;
}
void bubbleSort(vector<int> numbers, long int iterations) {
for(int i = 0; i < iterations; i++) {
for(int j = 0; j < numbers.size(); j++) {
if(numbers[j] > numbers[j + 1]) {
swap(numbers[j], numbers[j + 1]);
}
}
}
printVector(numbers);
}
void parse(string line) {
size_t delimiterPos = line.find("|");
long int iterations = stol(line.substr(delimiterPos + 1));
string numbersString = line.substr(0, delimiterPos - 1);
stringstream numbersSS(numbersString);
vector<int> numbers;
int tempNum;
while(numbersSS >> tempNum) {
numbers.push_back(tempNum);
}
bubbleSort(numbers, iterations);
}
int main(int argc, const char * argv[]) {
ifstream file(argv[1]);
string line;
while(getline(file, line)) {
parse(line);
}
return 0;
}