Answer to Question #346505 in C++ for annika

Question #346505

write a C++ program.An array my_Array[] consisting of ‘a’ s, ‘b’ s and ‘c’s. The task is to write a function that

arranges the array such that all ‘a’s are placed first, then all ‘b’s and then all ‘c’s in last.


1
Expert's answer
2022-06-01T12:54:50-0400
#include <iostream>
using namespace std;
void bubbleSort(char arr[], int n) {
    int i, j;
    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - i - 1; j++)
            if (arr[j] > arr[j + 1])
                swap(arr[j], arr[j + 1]);       
         
}
 
// Function to print an array
void printArray(char arr[], int size) {
    int i;
    for (i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}
 
int main() {
    const int N=20;
    char my_Array[N] = {'a','c','c','a','b','b','b','a','a','c','c','a','c','c','a','b','b','b','a','a'};
    int i, n;
 
    printArray(my_Array, N);
    cout<<endl;
    
     bubbleSort(my_Array, N);
    cout << "Sorted array: \n";
    printArray(my_Array, N);
    cout<<endl;
    
return 0;
}

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
New on Blog
APPROVED BY CLIENTS