76 285
Assignments Done
98,3%
Successfully Done
In May 2019

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

Question #52537
Write a code that sorts an array of strings of names in descending order using bubble sort.
Expert's answer
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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions