All pastes #267830 Raw Edit

Quelqu'un

public text v1 · immutable
·published 2006-12-05 03:22 UTC
rendered paste body

#include <iostream>

#include <cstring>

 

using namespace std; //no need for adding the std namespace for all iostream functions

 

int main ()

{

        bool hasSwapped = false;

       char *arrayPtr[15] = {"Paris", "Tokyo", "Rome", "Hong Kong", "New York", "London"};

        char *temp;

 

        cout << endl;

 

        cout << "Unsorted list: \n";

        for (int i = 0; i < 6; i++)

        {

                cout << arrayPtr[i] << endl;

        }

        cout << endl;

 

       //'bubbling' mechanism

        for (int i = 0 ; i < 5 ; i++)  //start from the first to the last with the bubbling loop

        {

 

                for (int j = 0 ; j < 5 - i ; j++)  //bubble down a single variable - no need to pass on elements already bubbled down

                {

                        hasSwapped = false;  //reset the boolean value

                       if (strcmp (arrayPtr[j], arrayPtr[j + 1]) > 0 )

                        {

                                temp = arrayPtr[j + 1];

                                arrayPtr[j + 1] = arrayPtr[j];

                                arrayPtr[j] = temp;

                                hasSwapped = true;

                        }

                }

 

               if (!hasSwapped)  //if the variable doesn't swap in the inner loop at all, then it won't swap for the rest of the outer loop

                {

                        break;

                }

        }

 

        cout << "Sorted list: \n";

        for (int i = 0; i < 10; i++)

        {

                cout << arrayPtr[i] << endl;

        }

       cout << endl;

 

        arrayPtr[0];

 

        cout << endl;
        
        return 0;

}