How to Sort Array Elements Alphabetically in C++

Hello, today we would be learning how to sort elements in an array alphabetically in C++.
So read on…

Sort Array Elements Alphabetically in C++

We sort elements in a data set to help us get access to it in a minimal amount of time.

The process of arranging data is known as sorting and the process of searching for a key element is known as searching.

Why is sorting important?
Imagine you have books lying on a particular shelf.
If it is in a random order, it would take more time finding the book you need.
If it is in a particular order such as alphabetically or numerically, you know where to look when finding the book you need.
Hence sorting is important.

We have many different styles of sorting most popular being, bubble sort, quick sort, merge sort, insertion sort, and selection sort.

For this problem, I would be using bubble sort.

What is Bubble Sort?

Bubble sort is the simplest and the oldest sorting technique and also, the slowest.

In Bubble sort, we take 2 adjacent elements, compare them, swap if necessary and move on.

Have a look at this example:
I have numbers: 8,6,2,4 and I want to sort them in ascending order.
So in bubble sort, we compare the first 2 elements and swap if necessary i.e. 8 and 6.
Then we compare the next two elements and swap in necessary i.e. 8 and 2
Hence the order is

8,6,2,4  (Original)
6,8,2,4  (Swap 8,6)
6,2,8,4  (Swap 8,2)
6,2,4,8  (Swap 8,4)
2,6,4,8  (Swap 6,2)
2,4,6,8  (Swap 6,4)
2,4,6,8  (Check 2,4. Since true, don't swap)
2,4,6,8  (Acsending Order)

Hence since we are comparing every element, Time complexity is O(n^2) at its worst case.


for i=0 to i<n
for j=1 to j<n-i
if a[j-1]>a[j]

C++ Code: Sort array elements alphabetically

Take a look at the following code:

#include <iostream>
using namespace std;
int main()
    char a[5]={'e','c','d','a','b'};
    char temp;
    int n=5;
    for(int i=0;i<n;i++)
        for(int j=1;j<n-i;j++)
    for(int i=0;i<5;i++)

Let us take a look at it line by line:

  1. char a[5]={'e','c','d','a','b'};

    Here we are defining a character array having 5 elements.
    Notice the are unsorted.

  2. char temp;
    int n=5;

    Here we are defining a variable temp of type char used to swap the array elements and a variable n of type int used to store the max size of the array.

  3. for(int i=0;i<n;i++)
            for(int j=1;j<n-i;j++)

    Here we are simply using the algorithm and implementing bubble sort.
    We are iterating a loop n times for variable i.
    And a loop n-i times for variable j. n-i because the last element is always in its correct position.
    Then we are comparing j-1 and the jth element.
    If the condition is not true we are swapping them.

  4. for(int i=0;i<5;i++) 

    Lastly here, we are printing our character array.

My input was “ecdab”.
Hence my output is:


Dry Run:

ecdab  (Original)
cedab  (swap c,e)
cdeab  (swap d,e)
cdaeb  (swap a,e)
cdabe  (swap b,e)
cdabe  (c,d not swapped)
cadbe  (swap a,d)
cabde  (swap b,d)
acbde  (swap a,c)
abcde  (swap b,c)
abcde  (a,b not swapped)
abcde  (ascending)

And Hence we have learned how to sort elements alphabetically in C++.
I hope you have understood the logic and were able to execute the code by yourself.
If you have any doubts, feel free to ask.

Leave a Reply

Your email address will not be published. Required fields are marked *