sorting from an array, not working?

I cant seem to figure out why my program isn't sorting my array. Any help would be greatly appreciated it. Here are my three files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>

using namespace std;


// -----------------------------------------------------------------
//   ListNode Class
// ------------------------------------------------------------------
class ListNode {
private:
  string data;
  ListNode *next;
  ListNode *prev;

public:
  ListNode(string, ListNode *); 
  string getData();
  ListNode *getNext();
  void setNext(ListNode *);
};

ListNode::ListNode(string d, ListNode *n) {
  data = d;
  next = n;
}

string 
ListNode::getData() {
  return data;
}

ListNode *
ListNode::getNext() {
  return next;
}

void
ListNode::setNext(ListNode *n) {
  next = n;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "node.h"
#include <iostream>
#include <string>


// -----------------------------------------------------------------
//  List Class
// -----------------------------------------------------------------
class List {
private:
  ListNode *head;
  int nitems;

public:
  bool ins(string);
  bool del(string);
  bool empty();
  int numNodes();
  string pop();
  void display();
  List(); 
};


List::List() {
    nitems = 0;
    head = NULL;
}

/* Return the number of nodes */
int List::numNodes(){
	return nitems;
}

void List::display(){
	ListNode *current = head;

	while(current != NULL){
		cout << current->getData() << endl;
		current = current->getNext();
	}
}

string List::pop(){
    // Return the first item in the linked list, and remove it
	string data = head->getData();
	del(data);
	
	return data;
}

bool
List::empty() {
  return nitems == 0;
}

bool
List::ins(string data) {
  ListNode *node = new ListNode(data, head);
  head = node;
  nitems++;
  return true;
}


bool
List::del(string data) {

  ListNode *curr = head;
  ListNode *before = 0;

  while(curr != NULL && data != curr->getData()) {
    before = curr;
    curr = curr->getNext();
  }
  if (curr == NULL) {
    cout << "Error: " << data << " not in list" << endl;
    return false; // item not in list
  }
  if (before == NULL) {  // if curr is the head
    head = curr->getNext();
  } else {
    before->setNext(curr->getNext());
  }
  delete curr;
  nitems--;
  return true;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <fstream>
#include <string>
#include <iostream>
#include "list.h"

using namespace 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.
       else if (key < sortedArray[mid]) 
           last = mid - 1; // repeat search in bottom half.
       else
           return 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
Last edited on
> 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.
Last edited on
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.
Last edited on
Thanks again, far too little sleep. Thanks so much for your help. I got it now.
Topic archived. No new replies allowed.