Sorting Algorithm: How to Sort abcdefghijklm
In the process of learning a programming language, you might encounter a problem of sorting something alphabetically such as abcdefghijklm. For a human, sorting the alphabet is easy. We know which letter comes first and which comes later. Somehow, computers see things differently. They treat the letter as a common character and does not care about alphabetical order.
As someone who writes the code, we need to tell the computer to sort it specifically. Luckily, every character has a value called ASCII or American Standard Code for Information Interchange. With that in mind, we can tell the computer to sort something based on its ASCII. If we tell the computer to sort alphabets from the smallest ASCII to the largest, it will be sorted alphabetically.
There are many sorting algorithms available. Each has different stability and time complexities when applied. Here are some examples of how to sort abcdefghijklm for you.
This is one of the simplest sorting algorithms. Imagine you are holding cards and you want to sort it in a desirable order. You are doing an insertion sort. This Insertion sort works efficiently for a small list or list that is almost sorted. It works by picking elements from that list you want to sort each one as well as inserting the elements into their right and correct position in a new list.
For example, you have a list consists of letters bcdefgaijklmh. If we want to sort it using insertion sort, here is what we tell to the computer:
- Tell the computer to do an iteration for the length of bcdefgaijklmh time. In this case, the iteration will runs 13 times.
- Compare the value of each letter in bcdefgaijklmh. If one letter’s value is smaller than the later, we put the letter to the leftmost side of the new list.
- Since bcdefg is already in the correct order, we will get it in our iteration.
- When the code reaches letter a, it will acknowledge that g’s value is larger than a. Then it will compare a with f, e, d, and so forth until the code realizes that ‘a’ has the smallest value. The list will become abcdefg.
- Next, ijklm is also already in the correct order, so the list will say abcdefgijklm.
- The last step deals with letter h where it will be treated the same way as the code treats letter a. The code will acknowledge that letter h’s value is between letter g and letter I. So, the list will have sorted list abcdefghijklm.
Bubble sort is another sorting algorithm that can efficiently sort abcdefghijklm. It is another simple sorting algorithm that works best for shortlist. It works by comparing the first two elements of the list, if the first is greater than the second, it will be swapped. After reaches the last element, it will start over until no elements are swapped.
Here is the example of how Bubble sort works. Supposed we have a list of bcadefghijkml. The Bubble sort will work like these:
- Compare the value of the first two elements, b and c. Since b’s value is not greater than c, no swap happened.
- Compare the next two elements, c and a. The value of c is greater than ‘a’. Therefore, a swap happens. The list will become bacdefghijkml.
- Compare the next two elements until it reaches the last two elements, m and l. Since the value of m is greater than l, swap happens. Therefore, our list after the first iteration will be bacdefghijklm.
- The second iteration will be started by comparing the value of b and a. Since b’s value is greater than a, there is a swap. All elements left are already ordered. So, no swap will happen until the last two elements compared.
- After two iterations, we will get an alphabetical order of abcdefghijklm.
Those are two samples of a simple sorting algorithm. If we want to sort a larger set of data, we may choose another algorithm. If we master the core concept of sorting algorithm, we can pick which is the most efficient algorithm to do the sorting. Stability and time complexity are the things that we have to consider when picking sorting algorithm.
A simple algorithm is efficient enough to sort abcdefghijklm alphabetically. With Insertion sort and Bubble sort, we can do the job efficiently. For that, we can sort abcdefghijklm without breaking a sweat.