Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programming Write a program that will sort an input string in ascending and

ID: 3678710 • Letter: C

Question

C++ programming

Write a program that will sort an input string in ascending and descending order. The program will contain functions for the Bubble Sort and the Selection Sort that are able to process string types. Use the bubble sort to sort the string in descending order and the selection sort to sort the same input string in ascending order. The main program should prompt the user to input a string of characters of any length. Count the number of comparisons used by each sort. Print the sorted strings and comparison counts to the screen. Any sequence of characters typed in will be OK. Not just letters and numbers.

Explanation / Answer


/*The C++ program that sorts the string array of size
in descendig and ascending order using bubble sort and selection
sort. Then print the sorting before and after sorting and
print to console.*/
#include<iostream>
#include<string>
using namespace std;
//function prototypes
int bubbleSort(string names[],int size);
int selectionSort(string names[],int size);
void print(string names[],int size);

int main()
{

   const int size=5;
   string names[5]={"java","csharp","perl","python","javascript"};

   string names2[5]={"java","csharp","perl","python","javascript"};;

   cout<<"BUBBLE SORT "<<endl<<endl;
   cout<<"Before sorting in descending order "<<endl;
   print(names,size);  
   int comparisions=bubbleSort(names, size);  
   cout<<"After sorting in descending order "<<endl;
   print(names,size);
   cout<<"Number of comparsions to sort : "<<comparisions<<endl;


   cout<<endl<<"SELECTION SORT "<<endl<<endl;
   cout<<"Before sorting in ascending order "<<endl;
   print(names2,size);  
   comparisions=selectionSort(names2,size);
   cout<<"After sorting in ascending order "<<endl;
   print(names2,size);
   cout<<"Number of comparsions to sort : "<<comparisions<<endl;

   //pause the program output on console
   system("pause");
   return 0;

}
//Bubble sort that takes string array and size
//and sorts the elements in descending order
int bubbleSort(string names[],int size)
{
   int count=0;
   for (int i = 0 ; i < ( size - 1 ); i++)
   {
       for (int j = 0 ; j < size - i - 1; j++)
       {
           count++;
           if (strcmp(names[j].c_str(),names[j+1].c_str())<0) /* For decsending order use < */
           {
               string swap       = names[j];
               names[j]   = names[j+1];
               names[j+1] = swap;
           }
       }
   }

   //return count
   return count;

}
//Selection sort that takes string array and size
//and sorts the elements in ascending order
int selectionSort(string names[],int size)
{
   int i, j, min_idx;
   int count=0;
   for (i = 0; i < size-1; i++)
   {
       //increment the count by one
       count++;
       // Find the minimum element in unsorted array
       min_idx = i;
       for (j = i+1; j < size; j++)
       {

           if (strcmp(names[j].c_str() ,names[min_idx].c_str())<0)
               min_idx = j;
       }

       // Swap the found minimum element with the first
       // element
       string temp = names[min_idx];
       names[min_idx] = names[i];
       names[i] = temp;
   }

   //return count
   return count;
}

//print elements to console
void print(string names[],int size)
{
   for (int i = 0 ; i < size ; i++)  
       cout<<names[i]<<",";

   cout<<endl;

}

----------------------------------------------------------------------------------------------------------------

Sample output:

BUBBLE SORT

Before sorting in descending order
java,csharp,perl,python,javascript,
After sorting in descending order
python,perl,javascript,java,csharp,
Number of comparsions to sort : 10

SELECTION SORT

Before sorting in ascending order
java,csharp,perl,python,javascript,
After sorting in ascending order
csharp,java,javascript,perl,python,
Number of comparsions to sort : 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote