Hello!
So I wanted to create a bubble sort algorithm and I have it working properly, I was just wondering if anyone could take a look at my code and give me any advice/suggestions on making it more efficient or if I did anything I should avoid doing. I would really appreciate any tips! I know that typically the bubble sort algorithm shouldn't print out each pass thru or maybe it shouldn't display anything at all, but I just wanted to add those print statements for testing purposes and so that I could see that the algorithm was working properly
///////////////////////////////////
// //
// Bubble Sort Algorithm //
// --------------------- //
// //
// Bubble Sort Algorithm //
// using C++ //
// //
// Date: 5/12/16 //
// //
///////////////////////////////////
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
//==========================================================================================
template <typename T>
void displayList(std::vector<T> theList) {
if (theList.size() == 0)
std::cout << "*** Cannot display empty list ***\n\n";
else {
int counter = 1;
std::vector<T>::iterator listIter;
for (listIter = theList.begin(); listIter != theList.end(); listIter++) {
// checks if a comma should be placed after an item
if (listIter != theList.end()) {
if (counter == theList.size())
std::cout << *listIter << "\n";
else
std::cout << *listIter << ", ";
}
counter++;
}
}
}
//==========================================================================================
template <typename T>
void bubbleSort(std::vector<T> & theList) {
if (theList.size() == 0) {
std::cout << "BubbleSort Begin:\n"
<< "----------------------------\n"
<< "| *Error: List is empty\n"
<< "----------------------------\n\n";
}
else {
bool sorted = false;
bool altered_list;
int passthru_counter = 1;
int counter = 0;
std::vector<T>::iterator listIter;
std::cout << "Intial List Order: ";
displayList(theList);
std::cout << "\nBubbleSort Begin:\n-------------------------------------------------\n";
while (sorted == false) {
counter = 0;
altered_list = false;
for (listIter = theList.begin(); listIter != theList.end(); listIter++) {
// if the list was stepped thru and no items were out of place
if (counter + 1 < theList.size()) {
if (theList[counter] > theList[counter + 1]) {
T temp = theList[counter];
theList[counter] = theList[counter + 1];
theList[counter + 1] = temp;
altered_list = true;
std::cout << passthru_counter << " pass thru:\t";
displayList(theList);
passthru_counter++;
break;
}
else
counter++;
}
else {
if (altered_list == false) {
sorted = true;
break;
}
}
}
}
std::cout << "-------------------------------------------------\n";
std::cout << "*** The list is sorted\n";
std::cout << "*** # of pass thrus to sort = " << passthru_counter << "\n";
std::cout << "-------------------------------------------------\n\n";
}
}
//==========================================================================================
int main() {
std::vector<std::string> testList;
// test values
testList.push_back("lol");
testList.push_back("omg");
testList.push_back("wtf");
testList.push_back("kek");
testList.push_back("btw");
testList.push_back("fyi");
bubbleSort(testList);
system("PAUSE");
return 0;
}