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.
#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;
}
Comments
Leave a comment