Thread: Practice...
View Single Post
Old 02-11-2005, 07:56 PM   #5 (permalink)
ALF_Bass
Learning C++
 
Join Date: Feb 2005
Location: Ottawa
Posts: 10
ALF_Bass is on a distinguished road
Here's a basic bubble sort of the sLeaderTypes you wanted, I added a few more to make sure my sort was working correctly. Which it seems to be....
Code:
#include <string>
#include <iostream>
using namespace std;
#define SIZE 100

void bubbleSort(string array[8]);
void swap(string *, string *);

int main()
{
	int i=0, count=9;
	string  sLeaderTypes[] = {"Emperor","King","Tyrant","President","Warlord","Prince","Baron","Queen","Dictator"};
   
	
   cout << endl << "Data items in original order" << endl;
   for (i = 0; i < count; i++)
      cout <<sLeaderTypes[i] << endl;


   bubbleSort(sLeaderTypes); // sort the array
   cout <<"Sorted list:";
   for (i = 0; i < count; i++)
      cout <<endl << sLeaderTypes[i];
   return 0;
}

void bubbleSort(string array[8])
{
   int pass, size = 9;

   for (pass = 1; pass <= size - 1; pass++)

      for (int j = 0; j <= size - 2; j++)

        if ((array[j].compare(array[j + 1]) > 0))
            swap(array[j], array[j + 1]);
}

void swap(string *element1Ptr, string *element2Ptr)
{
   string temp;

   temp = *element1Ptr;
   *element1Ptr = *element2Ptr;
   *element2Ptr = temp;
}
ALF_Bass is offline   Reply With Quote