### Guidance Needed With Bubble Sort

Pages: 12
Hi everyone,

My current assignment is to take the source code given to us from the author and to manipulate it with a bubble sort. I have experience doing this but I'm a bit puzzled trying to incorporate my way of doing this into another person's work.

The source code given to us has the user enter 5 integers, then it will print them back out and ask the user to delete one of them. It will then re-print the list minus the deleted number and I need to sort them in numerical order. It does the same thing for an array of 5 strings after that.

My current code is this (everything I added doesn't have the commented line numbers):

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111`` ``````#include //Line 1 #include //Line 2 #include "arrayListType.h" //Line 3 using namespace std; //Line 4 // Creating the bubble sort template template void myBubbleSort(bubble a[], int n) { bool flag = 0; do { flag = 0; for(int i = 0; i < (n - 1); i++) { if(a[i] > a[i+1]) { bubble elm; elm = a[i]; a[i] = a[i + 1]; a[i + 1] = elm; flag = 1; } } } while(flag); } int main() //Line 5 { //Line 6 arrayListType intList(100); //Line 7 arrayListType stringList; //Line 8 int number; //Line 9 cout << "List 10: Enter 5 integers: "; //Line 10 for (int counter = 0; counter < 5; counter++) //Line 11 { //Line 12 cin >> number; //Line 13 intList.insertAt(counter, number); //Line 14 } //Line 15 cout << endl; //Line 16 cout<<"List 19: The list you entered is: "; //Line 17 intList.print(); //Line 18 //Line 19 cout << "Line 20: Enter the item to be deleted: "; //Line 20 cin >> number; //Line 21 intList.remove(number); //Line 22 cout << "Line 23: After removing " << number << ", the list is:" << endl; //Line 23 // Sorting the integers in numerical order myBubbleSort(intList, 5); cout << "\n"; // Printing out the integers in numerical order for(int i = 0; i < 5; i++) { cout << intList[i]; } cout << "\n"; //Line 25 string str; //Line 26 cout << "Line 27: Enter 5 strings: "; //Line 27 for (int counter = 0; counter < 5; counter++) //Line 28 { //Line 29 cin >> str; //Line 30 stringList.insertAt(counter, str); //Line 31 } //Line 32 cout << endl; //Line 33 cout << "Line 34: The list you entered is: " << endl; //Line 34 stringList.print(); //Line 35 cout << endl; //Line 36 cout << "Line 37: Enter the string to be deleted: "; //Line 37 cin >> str; //Line 38 stringList.remove(str); //Line 39 cout << "Line 40: After removing " << str << ", the list is:" << endl; //Line 40 //Line 42 // Sorting the strings in alphabetical order myBubbleSort(stringList, 5); cout << "\n"; // Printing out the strings in alphabetical order for(int i = 0; i < 5; i++) { cout << stringList[i]; } cout << "\n"; return 0; //Line 43 } //Line 44 ``````

The errors I'm getting are because my bubble sort arguments don't match the array's the author is creating here and I'm a bit stuck as to how I would make these work together. Any help would be great, thanks!
Last edited on
Your function takes a raw array of the template arg type, but the arrayListType is a custom data structure. If that data structure has a method to get a pointer to the first element, and arrayListType guarantees that it's items are stored contiguously in memory, then you could pass in the pointer to arg 1. Otherwise, you could change your function to accept an arrayListType<bubble>, instead of a raw array. I'm not sure what your instructor expects you to do.
Here is his header file with all of his arrayListType definitions. I've been messing around with my bubble sort function to accept his data structures to no avail. It's very long so I'll split it in two:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122`` ``````#ifndef H_arrayListType #define H_arrayListType //*********************************************************** // Author: D.S. Malik // // This class specifies the members to implement the basic // properties of array-based lists. //*********************************************************** #include #include using namespace std; template class arrayListType { public: const arrayListType& operator= (const arrayListType&); //Overloads the assignment operator bool isEmpty() const; //Function to determine whether the list is empty //Postcondition: Returns true if the list is empty; // otherwise, returns false. bool isFull() const; //Function to determine whether the list is full. //Postcondition: Returns true if the list is full; // otherwise, returns false. int listSize() const; //Function to determine the number of elements in the list //Postcondition: Returns the value of length. int maxListSize() const; //Function to determine the size of the list. //Postcondition: Returns the value of maxSize. void print() const; //Function to output the elements of the list //Postcondition: Elements of the list are output on the // standard output device. bool isItemAtEqual(int location, const elemType& item) const; //Function to determine whether the item is the same //as the item in the list at the position specified by //Postcondition: Returns true if the list[location] // is the same as the item; otherwise, // returns false. void insertAt(int location, const elemType& insertItem); //Function to insert an item in the list at the //position specified by location. The item to be inserted //is passed as a parameter to the function. //Postcondition: Starting at location, the elements of the // list are shifted down, list[location] = insertItem;, // and length++;. If the list is full or location is // out of range, an appropriate message is displayed. void insertEnd(const elemType& insertItem); //Function to insert an item at the end of the list. //The parameter insertItem specifies the item to be inserted. //Postcondition: list[length] = insertItem; and length++; // If the list is full, an appropriate message is // displayed. void removeAt(int location); //Function to remove the item from the list at the //position specified by location //Postcondition: The list element at list[location] is removed // and length is decremented by 1. If location is out of // range,an appropriate message is displayed. void retrieveAt(int location, elemType& retItem) const; //Function to retrieve the element from the list at the //position specified by location. //Postcondition: retItem = list[location] // If location is out of range, an appropriate message is // displayed. void replaceAt(int location, const elemType& repItem); //Function to replace the elements in the list at the //position specified by location. The item to be replaced //is specified by the parameter repItem. //Postcondition: list[location] = repItem // If location is out of range, an appropriate message is // displayed. void clearList(); //Function to remove all the elements from the list. //After this operation, the size of the list is zero. //Postcondition: length = 0; int seqSearch(const elemType& item) const; //Function to search the list for a given item. //Postcondition: If the item is found, returns the location // in the array where the item is found; otherwise, // returns -1. void insert(const elemType& insertItem); //Function to insert the item specified by the parameter //insertItem at the end of the list. However, first the //list is searched to see whether the item to be inserted //is already in the list. //Postcondition: list[length] = insertItem and length++ // If the item is already in the list or the list // is full, an appropriate message is displayed. void remove(const elemType& removeItem); //Function to remove an item from the list. The parameter //removeItem specifies the item to be removed. //Postcondition: If removeItem is found in the list, // it is removed from the list and length is // decremented by one. arrayListType(int size = 100); //constructor //Creates an array of the size specified by the //parameter size. The default array size is 100. //Postcondition: The list points to the array, length = 0, // and maxSize = size arrayListType(const arrayListType& otherList); //copy constructor ~arrayListType(); //destructor //Deallocates the memory occupied by the array. protected: elemType *list; //array to hold the list elements int length; //to store the length of the list int maxSize; //to store the maximum size of the list };``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243`` ``````template bool arrayListType::isEmpty() const { return (length == 0); } template bool arrayListType::isFull() const { return (length == maxSize); } template int arrayListType::listSize() const { return length; } template int arrayListType::maxListSize() const { return maxSize; } template void arrayListType::print() const { for (int i = 0; i < length; i++) cout << list[i] << " "; cout << endl; } template bool arrayListType::isItemAtEqual (int location, const elemType& item) const { return (list[location] == item); } template void arrayListType::insertAt (int location, const elemType& insertItem) { if (location < 0 || location >= maxSize) cerr << "The position of the item to be inserted " << "is out of range" << endl; else if (length >= maxSize) //list is full cerr << "Cannot insert in a full list" << endl; else { for (int i = length; i > location; i--) list[i] = list[i - 1]; //move the elements down list[location] = insertItem; //insert the item at the //specified position length++; //increment the length } } //end insertAt template void arrayListType::insertEnd(const elemType& insertItem) { if (length >= maxSize) //the list is full cerr << "Cannot insert in a full list" << endl; else { list[length] = insertItem; //insert the item at the end length++; //increment the length } } //end insertEnd template void arrayListType::removeAt(int location) { if (location < 0 || location >= length) cerr << "The location of the item to be removed " << "is out of range" << endl; else { for (int i = location; i < length - 1; i++) list[i] = list[i+1]; length--; } } //end removeAt template void arrayListType::retrieveAt (int location, elemType& retItem) const { if (location < 0 || location >= length) cerr << "The location of the item to be retrieved is " << "out of range." << endl; else retItem = list[location]; } //end retrieveAt template void arrayListType::replaceAt (int location, const elemType& repItem) { if (location < 0 || location >= length) cerr << "The location of the item to be replaced is " << "out of range." << endl; else list[location] = repItem; } //end replaceAt template void arrayListType::clearList() { length = 0; } //end clearList template int arrayListType::seqSearch(const elemType& item) const { int loc; bool found = false; for (loc = 0; loc < length; loc++) if (list[loc] == item) { found = true; break; } if (found) return loc; else return -1; } //end seqSearch template void arrayListType::insert(const elemType& insertItem) { int loc; if (length == 0) //list is empty list[length++] = insertItem; //insert the item and //increment the length else if (length == maxSize) cerr << "Cannot insert in a full list." << endl; else { loc = seqSearch(insertItem); if (loc == -1) //the item to be inserted //does not exist in the list list[length++] = insertItem; else cerr << "the item to be inserted is already in " << "the list. No duplicates are allowed." << endl; } } //end insert template void arrayListType::remove(const elemType& removeItem) { int loc; if (length == 0) cerr << "Cannot delete from an empty list." << endl; else { loc = seqSearch(removeItem); if (loc != -1) removeAt(loc); else cout << "The item to be deleted is not in the list." << endl; } } //end remove template arrayListType::arrayListType(int size) { if (size < 0) { cerr << "The array size must be positive. Creating " << "an array of size 100. " << endl; maxSize = 100; } else maxSize = size; length = 0; list = new elemType[maxSize]; assert(list != NULL); } template arrayListType::~arrayListType() { delete [] list; } template arrayListType::arrayListType (const arrayListType& otherList) { maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; //create the array assert(list != NULL); //terminate if unable to allocate //memory space for (int j = 0; j < length; j++) //copy otherList list [j] = otherList.list[j]; } //end copy constructor template const arrayListType& arrayListType::operator= (const arrayListType& otherList) { if (this != &otherList) //avoid self-assignment { delete [] list; maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; //create the array assert(list != NULL); //if unable to allocate memory //space, terminate the program for (int i = 0; i < length; i++) list[i] = otherList.list[i]; } return *this; } #endif ``````
your

 ``123`` `````` arrayListType intList(100); arrayListType stringList; ``````

You are passing into the bubble sort function your user defined types

arrayListType

intList and stringList are types arrayListType, they are not int or string.

Atleast that is what I think.
sorry to cut myself off.
This is what your passing in.

 ``123456789101112131415161718192021222324`` `````` void myBubbleSort(arrayListType a[], int n) { bool flag = 0; do { flag = 0; for(int i = 0; i < (n - 1); i++) { if(a[i] > a[i+1]) { arrayListType elm;//here your just declaring another object with the//default constructor elm = a[i]; a[i] = a[i + 1]; a[i + 1] = elm; flag = 1; } } } while(flag); } ``````

I think this is what you want it to look like
 ``12345678910111213141516171819202122232425`` `````` void myBubbleSort(int a[], int n) { bool flag = 0; do { flag = 0; for(int i = 0; i < (n - 1); i++) { if(a[i] > a[i+1]) { int elm; elm = a[i]; a[i] = a[i + 1]; a[i + 1] = elm; flag = 1; } } } while(flag); } ``````

[/code]
Last edited on
In order to work with an object like that you have to overload the subscript operator []. Your using the object as an array without overloading the subscript operator, I don't think you meant to do that on purpose. What you are trying to do is work with the array inside the object, you are not doing that.

I could be completely wrong though.
I revised your code, its messy but look at what I did.
I put the bubble sort function in your class and removed the global function.

check like 121 for change in this part, ill post the rest in another post

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128`` ``````#ifndef H_arrayListType #define H_arrayListType //*********************************************************** // Author: D.S. Malik // // This class specifies the members to implement the basic // properties of array-based lists. //*********************************************************** #include #include #include using namespace std; template class arrayListType { public: const arrayListType& operator= (const arrayListType&); //Overloads the assignment operator bool isEmpty() const; //Function to determine whether the list is empty //Postcondition: Returns true if the list is empty; // otherwise, returns false. bool isFull() const; //Function to determine whether the list is full. //Postcondition: Returns true if the list is full; // otherwise, returns false. int listSize() const; //Function to determine the number of elements in the list //Postcondition: Returns the value of length. int maxListSize() const; //Function to determine the size of the list. //Postcondition: Returns the value of maxSize. void print() const; //Function to output the elements of the list //Postcondition: Elements of the list are output on the // standard output device. bool isItemAtEqual(int location, const elemType& item) const; //Function to determine whether the item is the same //as the item in the list at the position specified by //Postcondition: Returns true if the list[location] // is the same as the item; otherwise, // returns false. void insertAt(int location, const elemType& insertItem); //Function to insert an item in the list at the //position specified by location. The item to be inserted //is passed as a parameter to the function. //Postcondition: Starting at location, the elements of the // list are shifted down, list[location] = insertItem;, // and length++;. If the list is full or location is // out of range, an appropriate message is displayed. void insertEnd(const elemType& insertItem); //Function to insert an item at the end of the list. //The parameter insertItem specifies the item to be inserted. //Postcondition: list[length] = insertItem; and length++; // If the list is full, an appropriate message is // displayed. void removeAt(int location); //Function to remove the item from the list at the //position specified by location //Postcondition: The list element at list[location] is removed // and length is decremented by 1. If location is out of // range,an appropriate message is displayed. void retrieveAt(int location, elemType& retItem) const; //Function to retrieve the element from the list at the //position specified by location. //Postcondition: retItem = list[location] // If location is out of range, an appropriate message is // displayed. void replaceAt(int location, const elemType& repItem); //Function to replace the elements in the list at the //position specified by location. The item to be replaced //is specified by the parameter repItem. //Postcondition: list[location] = repItem // If location is out of range, an appropriate message is // displayed. void clearList(); //Function to remove all the elements from the list. //After this operation, the size of the list is zero. //Postcondition: length = 0; int seqSearch(const elemType& item) const; //Function to search the list for a given item. //Postcondition: If the item is found, returns the location // in the array where the item is found; otherwise, // returns -1. void insert(const elemType& insertItem); //Function to insert the item specified by the parameter //insertItem at the end of the list. However, first the //list is searched to see whether the item to be inserted //is already in the list. //Postcondition: list[length] = insertItem and length++ // If the item is already in the list or the list // is full, an appropriate message is displayed. void remove(const elemType& removeItem); //Function to remove an item from the list. The parameter //removeItem specifies the item to be removed. //Postcondition: If removeItem is found in the list, // it is removed from the list and length is // decremented by one. arrayListType(int size = 100); //constructor //Creates an array of the size specified by the //parameter size. The default array size is 100. //Postcondition: The list points to the array, length = 0, // and maxSize = size arrayListType(const arrayListType& otherList); //copy constructor ~arrayListType(); //destructor //Deallocates the memory occupied by the array. void myBubbleSort( int n); protected: elemType *list; //array to hold the list elements int length; //to store the length of the list int maxSize; //to store the maximum size of the list elemType elm; };``````
Last edited on
Here is the second part. It is the first function in the code.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266`` `````` template void arrayListType::myBubbleSort( int n) { bool flag = 0; do { flag = 0; for( int i = 0; i < (n - 1); i++) { if(list[i] > list[i+1]) { elm = list[i]; list[i] = list[i + 1]; list[i + 1] = elm; flag = 1; } } } while(flag); } template bool arrayListType::isEmpty() const { return (length == 0); } template bool arrayListType::isFull() const { return (length == maxSize); } template int arrayListType::listSize() const { return length; } template int arrayListType::maxListSize() const { return maxSize; } template void arrayListType::print() const { for (int i = 0; i < length; i++) cout << list[i] << " "; cout << endl; } template bool arrayListType::isItemAtEqual (int location, const elemType& item) const { return (list[location] == item); } template void arrayListType::insertAt (int location, const elemType& insertItem) { if (location < 0 || location >= maxSize) cerr << "The position of the item to be inserted " << "is out of range" << endl; else if (length >= maxSize) //list is full cerr << "Cannot insert in a full list" << endl; else { for (int i = length; i > location; i--) list[i] = list[i - 1]; //move the elements down list[location] = insertItem; //insert the item at the //specified position length++; //increment the length } } //end insertAt template void arrayListType::insertEnd(const elemType& insertItem) { if (length >= maxSize) //the list is full cerr << "Cannot insert in a full list" << endl; else { list[length] = insertItem; //insert the item at the end length++; //increment the length } } //end insertEnd template void arrayListType::removeAt(int location) { if (location < 0 || location >= length) cerr << "The location of the item to be removed " << "is out of range" << endl; else { for (int i = location; i < length - 1; i++) list[i] = list[i+1]; length--; } } //end removeAt template void arrayListType::retrieveAt (int location, elemType& retItem) const { if (location < 0 || location >= length) cerr << "The location of the item to be retrieved is " << "out of range." << endl; else retItem = list[location]; } //end retrieveAt template void arrayListType::replaceAt (int location, const elemType& repItem) { if (location < 0 || location >= length) cerr << "The location of the item to be replaced is " << "out of range." << endl; else list[location] = repItem; } //end replaceAt template void arrayListType::clearList() { length = 0; } //end clearList template int arrayListType::seqSearch(const elemType& item) const { int loc; bool found = false; for (loc = 0; loc < length; loc++) if (list[loc] == item) { found = true; break; } if (found) return loc; else return -1; } //end seqSearch template void arrayListType::insert(const elemType& insertItem) { int loc; if (length == 0) //list is empty list[length++] = insertItem; //insert the item and //increment the length else if (length == maxSize) cerr << "Cannot insert in a full list." << endl; else { loc = seqSearch(insertItem); if (loc == -1) //the item to be inserted //does not exist in the list list[length++] = insertItem; else cerr << "the item to be inserted is already in " << "the list. No duplicates are allowed." << endl; } } //end insert template void arrayListType::remove(const elemType& removeItem) { int loc; if (length == 0) cerr << "Cannot delete from an empty list." << endl; else { loc = seqSearch(removeItem); if (loc != -1) removeAt(loc); else cout << "The item to be deleted is not in the list." << endl; } } //end remove template arrayListType::arrayListType(int size) { if (size < 0) { cerr << "The array size must be positive. Creating " << "an array of size 100. " << endl; maxSize = 100; } else maxSize = size; length = 0; list = new elemType[maxSize]; assert(list != NULL); } template arrayListType::~arrayListType() { delete [] list; } template arrayListType::arrayListType (const arrayListType& otherList) { maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; //create the array assert(list != NULL); //terminate if unable to allocate //memory space for (int j = 0; j < length; j++) //copy otherList list [j] = otherList.list[j]; } //end copy constructor template const arrayListType& arrayListType::operator= (const arrayListType& otherList) { if (this != &otherList) //avoid self-assignment { delete [] list; maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; //create the array assert(list != NULL); //if unable to allocate memory //space, terminate the program for (int i = 0; i < length; i++) list[i] = otherList.list[i]; } return *this; } #endif ``````

next ill post what main looks like
main:
Lines of code that changed
line 34, used myBubbleSort member function
line 40, removed for loop and used print function

line 71, used myBubbleSort member function
line 77, removed for loop and used print function

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include //Line 1 #include //Line 2 #include "arrayListType.h" //Line 3 using namespace std; //Line 4 int main() //Line 5 { //Line 6 arrayListType intList(100); //Line 7 arrayListType stringList; //Line 8 int number; //Line 9 cout << "List 10: Enter 5 integers: "; //Line 10 for (int counter = 0; counter < 5; counter++) //Line 11 { //Line 12 cin >> number; //Line 13 intList.insertAt(counter, number); //Line 14 } //Line 15 cout << endl; //Line 16 cout<<"List 19: The list you entered is: "; //Line 17 intList.print(); //Line 18 //Line 19 cout << "Line 20: Enter the item to be deleted: "; //Line 20 cin >> number; //Line 21 intList.remove(number); //Line 22 cout << "Line 23: After removing " << number << ", the list is:" << endl; //Line 23 // Sorting the integers in numerical order intList.myBubbleSort(5); cout << "\n"; // Printing out the integers in numerical order intList.print(); cout << "\n"; //Line 25 string str; //Line 26 cout << "Line 27: Enter 5 strings: "; //Line 27 for (int counter = 0; counter < 5; counter++) //Line 28 { //Line 29 cin >> str; //Line 30 stringList.insertAt(counter, str); //Line 31 } //Line 32 cout << endl; //Line 33 cout << "Line 34: The list you entered is: " << endl; //Line 34 stringList.print(); //Line 35 cout << endl; //Line 36 cout << "Line 37: Enter the string to be deleted: "; //Line 37 cin >> str; //Line 38 stringList.remove(str); //Line 39 cout << "Line 40: After removing " << str << ", the list is:" << endl; //Line 40 //Line 42 // Sorting the strings in alphabetical order stringList.myBubbleSort(5); cout << "\n"; // Printing out the strings in alphabetical order stringList.print(); cout << "\n"; return 0; //Line 43 } //Line 44 ``````
Gkneeus,

Thank you so much for guiding me through this. I was working all morning with trying to define my bubble sort in the header file like you did and I was slowly working my way through it when you polished everything off for me. Although what you gave me compiles and runs correctly, I incorporated it into my code but after sorting the objects it's repeating one of them.

http://i258.photobucket.com/albums/hh243/Jangoon/program_zps23c2d6ec.jpg

Like so, after deleting 8, 2 gets repeated in the output after sorting. As well as goodbye getting repeated after deleting hey.

I'm going to try and work through this in the header before checking back here to see if your code is revised at all. Again, thank you for the help!
That's weird I came across the same problem the first time i ran your program. then I took out the for loops and placed in the print functions everything was working fine after multiple tests so I didn't mention anything about that in my posts. it was late yesterday night and I was getting tired so I didn't check out the code if you need more help just post back here.
I'm not quite sure which for loops you're talking about. Do you mean replacing part of my bubble sort function with the print function that the author already has in the header?
you had to For loops on main. you had a for loop for each object you created to display the integers in numerical order, I removed the for loop and the code inside and just inserted the print function instead.

...and I'm sorry to read that you are learning using one of Malik's books.

It's trash. You should find a better book. (Don't get anything by Schildt either.)

Your sort looks okay to me. If you are seeing duplicates, they are coming from somewhere else... (Sorry, tl;dr, hopefully Gkneeus was able to help you with the loops.)

A minor suggestion on the bubble sort:
 ``12345678910111213141516171819202122`` ``````template void arrayListType::myBubbleSort( int n) { bool flag = 0; do { flag = 0; n--; for( int i = 0; i < n; i++) { if(list[i] > list[i+1]) { elm = list[i]; list[i] = list[i + 1]; list[i + 1] = elm; flag = 1; } } } while(flag); }``````

This is a basic optimization for bubble sort, since you don't need to keep looking at elements that are already in their final place.

Hope this helps.
Gkneeus, I have already replaced those for loops with just the print function and I'm still getting the same output which is why I'm confused.

Duoas, I've even used your version of my bubble sort with the same result.

I'm using original source code from the author (who I agree, is awful by the way) so I even tried creating both the header and the cpp on this computer and pasting everything in to no avail.

http://i258.photobucket.com/albums/hh243/Jangoon/output_zpsfb2f4814.jpg

Like so.
I don't know if I have the time to dink with it ATM, but if I can I'll take a look at it.

The problem isn't in the bubble sort.

(It's probably in the list implementation.)
 ``12`` `````` for (int i = location; i < length - 1; i++) list[i] = list[i + 1];``````