Answer to Question #52537 in Programming & Computer Science for biruk

Question #52537
Write a code that sorts an array of strings of names in descending order using bubble sort.
1
Expert's answer
2015-05-14T04:27:47-0400
Solution.

#include <iostream>
#include <string>

using namespace std;

void main()
{
const int SIZE = 5;

// test array
string names[SIZE] = { "David", "John", "Robert", "Adam", "Craig" };


cout<< "Unsortedarray:"<< endl;
for (int i = 0; i < SIZE; i++)
cout<< names[i] << endl;


//Bubble Sort
bool noChange = true; // stop when a pass causes no change
for (int i = SIZE; i > 0; i--)
{
noChange= true;
for (int j = 1; j < i; j++)
{
if (names[j] > names[j - 1])
{
swap(names[j],names[j - 1]);
noChange= false;
}// end if
}// end for(j)
if (noChange)
break;
}// end for(i)


cout<< endl << "Sorted array:" << endl;
for (int i = 0; i < SIZE; i++)
cout<< names[i] << endl;

cin.get();// hold windowopen
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS