// Demonstrates the Bubble Sort Method '
// BubSort'
#include
#include
#include
// use the strcmp function to sort alpha
// betically... (ASCII)
char name[11] [20] =
{
"Lottery",
"Computer",
"Interface",
"Memory",
"Monitor",
"Mouse",
"Network",
"Printer",
"Program",
"RAM",
"ROM",
};
void main()
{
char temp[20];
int index1, index2, resp, tmp;
cout << "Unsorted list \n\n";
for (index1 = 0; index1 < 10; index1++)
{
cout << "Word : [" << index1 << "]=" << name[index1] << " \n";
}
cout << "\nComplete\n";
do
{
cout << "\n\tEnter 1 for Ascending, 2 for Descending...:";
cin >> resp;
if (resp == 1) //Start of BubSort
{
for (index1 = 0; index1 < 10; index1++)
{
for (index2 = 0; index2 < 10; index2++)
{
tmp = strcmp(name[index2], name[index1]);
if (tmp < 0)
{
strcpy(temp, name[index1]);
strcpy(name[index1], name[index2]);
strcpy(name[index2], temp);
}
}
}
for (index1 = 0; index1 < 10; index1++)
{
cout << "\t\tWord [" << index1 << "] = " << name[index1] << " \n";
}
cout << "Sorted!!... in Ascending Order";
} // end BubSort
if (resp == 2)
{
for (index1 = 0; index1 < 10; index1++)
{
for (index2 = 0; index2 < 10; index2++)
{
tmp = strcmp(name[index2], name[index1]);
if (tmp > 0)
{
strcpy(temp, name[index1]);
strcpy(name[index1], name[index2]);
strcpy(name[index2], temp);
}
}
}
for (index1 = 0; index1 < 10; index1++)
{
cout << "\t\tWord [" << index1 << "] = " << name[index1] << " \n";
}
cout << "Sorted!! in Descending Order...";
}
}
while (resp == 1 || resp == 2);
cout << "\n\n\tFinished!....";
}