Question: Bubble sort function isn't sorting properly? I have a list of words I'm reading from a text file, which I then sort in alphabetical order.

Bubble sort function isn't sorting properly?

I have a list of words I'm reading from a text file, which I then sort in alphabetical order. It is sorting the words in no particular order. I believe I need another condition to be true like another for loop outside of the other ones but I'm not sure what that condition would be. Any suggestions? Thank you!

#define _CRTDBG_MAP_ALLOC #define _CRT_SECURE_NO_WARNINGS #include #include #include #include

using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream;

// input and output files

const char SORT_IN[] = "sortin.txt"; const char SORT_OUT[] = "sortout.txt";

const char MAX_ENTRIES = 100;

int readFile(int& entries, char**& list); void sortFile(int entries, char**& list); void writeFile(int entries, char* list[]);

int main() { ifstream input(SORT_IN); ofstream output(SORT_OUT); int entries = 0; char** list = nullptr;

if (input.is_open()) { //load the file readFile(entries, list); //bubble sort the file sortFile(entries, list); //save the newly sorted file writeFile(entries, list); } else cout << "Error opening file!";

for (int i = 0; i < entries; i++) { delete[] list[i]; } delete list; input.close(); return 0; }

// read the list of words from sortin.txt (input) int readFile(int& entries, char**& list) { ifstream input(SORT_IN);

entries = 0; list = nullptr; char** temp = nullptr; char buffer[256];

input >> buffer; // priming read

if (input.is_open()) // if the file is open { while (!input.eof()) // while it is not end of file { temp = new char* [entries + 1];

for (int i = 0; i < entries; i++) { temp[i] = list[i]; // copy list to temporary listay }

temp[entries] = new char[strlen(buffer) + 1];

strcpy(temp[entries], buffer); // copy buffer into temp variable

delete[] list; list = temp; entries++; // increment entries input >> buffer; // read in entry } }

return entries; // return entries }

void sortFile(int entries, char**& list) { char* temp; bool swap = true;

for (int i = 0; i < entries - 1; i++) { for (int j = 0; j < entries - i - 1; j++) { //checking if previous value is greater than the next value if (list[j] > list[j + 1]) { // temp will temporarly store the value of list[j] temp = list[j]; // swap values list[j] = list[j + 1]; list[j + 1] = temp; }

} }

cout << "Array after bubble sort:" << endl; for (int i = 0; i < entries; ++i) cout << " " << list[i] << endl; }

//write list of words to file (output) void writeFile(int entries, char* list[]) { ofstream output(SORT_OUT);

// making sure the file is open if (output.is_open()) { // for each entry for (int i = 0; i < entries; i++) { // output the list output << list[i] << endl; } } output.close(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!