#include <fstream>
#include <string>
#include <iostream>
#include "list.h"
usingnamespace std;
void bubbleSort(string x[], int n) {
for (int pass=1; pass < n; pass++) { // count how many times
// This next loop becomes shorter and shorter
for (int i=0; i < n-pass; i++) {
if (x[i] > x[i+1]) {
// exchange elements
string temp = x[i]; x[i] = x[i+1]; x[i+1] = temp;
}
}
}
}
int binarySearch(int sortedArray[], int first, int last, int key)
{
// function:
// Searches sortedArray[first]..sortedArray[last] for key.
// returns: index of the matching element if it finds key,
// otherwise -(index where it could be inserted)-1.
// parameters:
// sortedArray in array of sorted (ascending) values.
// first, last in lower and upper subscript bounds
// key in value to search for.
// returns:
// index of key, or -insertion_position -1 if key is not
// in the array. This value can easily be
// transformed into the position to insert it.
while (first <= last) {
int mid = (first + last) / 2; // compute mid point.
if (key > sortedArray[mid])
first = mid + 1; // repeat search in top half.
elseif (key < sortedArray[mid])
last = mid - 1; // repeat search in bottom half.
elsereturn mid; // found it. return position /////
}
return -(first + 1); // failed to find key
}
int main()
{
string cppstr;
List myList;
int itemsInLinkedList = 0;
short data = 0;
ifstream myfile("input1.txt");
if(myfile.is_open()){
while(! myfile.eof()){
getline(myfile, cppstr);
myList.ins(cppstr);
}
myfile.close();
} else {
cout << "Could not open file..." << endl;
}
myfile.clear();
itemsInLinkedList = myList.numNodes();
cout << "\nSize of list: " << itemsInLinkedList << "\n" << endl;
//myList.display();
string *myString = new string[itemsInLinkedList];
for(int x = 0; x < itemsInLinkedList; x++){
myString[x] = myList.pop();
}
bubbleSort(myString, myList.numNodes()); // should sort the array....
for(int x = 0; x < itemsInLinkedList; x++){
cout << myString[x] << endl;
}
delete[] myString;
char temp;
cin >> temp;
return 0;
}
thanks for the help in advance
also, this is the input file
john doe
ralph nayer
charlie
Ray
Michael Johnson
mickey mouse and the bunch
Do ray me fa so la, do si do
> bubbleSort(myString, myList.numNodes()); // should sort the array....
Correct call is "bubbleSort(myString, itemsInLinkedList);". Then it works, even though your code is very cruel to read. But remember: Words/Lines that start with a capital letter are lexicographically lesser than those that start with a small letter.
Magnus, Thanks so much for the response. I completely understand what you are saying. My code is cruel. I am still learning. Once, the program is functional I will make it more readable. Sorry for that, However, I still can't get it to print the new sorted list. Any help with that would be great. If I print out itemsInLinkedList, I only get 7's.
Your for-loop already _does_ print out the string array. And if you replace the call of bubbleSort(...); as I described above, you will get a lexicographically sorted array.