Introduction to Storing in C++ Programming Language

 Sorting in C++ Programming Language


Sorting in C++
Sorting in C++


Introduction:

In this article, I will discuss the different sorting algorithms and their applications. Also, we will consider a simple algorithm in which we sort numbers using linear time. Then we'll go back to consider another version of such a problem where you have to sort in O(N) O(N log N), and a third one is where we sort like O(N log n) O(n log n). This article is for those who are just starting with computer science and want to learn these concepts. We will be covering three important algorithms - heap, bubble sort, and heapsort.


Heap Sort

Heap Sort is a classic order-based sorting algorithm first invented by Ronald Lippman (1928). It states that "the easiest way of all ordering problems". 


Heap sorting in C++
Heap Sorting in C++


The following graph gives us an intuition about it:

Pseudocode of heap sort:

There are two cases for heap sort

The inputs are sorted in ascending or descending order. Both inputs are compared when they are being processed until each input has been processed at least once. When these cases are combined, then the output is decided at the end by adding up all possible outputs and if no comparison is made, then we will return an empty array. But don't consider that there must be a minimum value to compare the input values. For example, if there is only a single number, then the final sorted array will be empty because there will not be any number that can be less than 1.

If that's the case, then we need to reverse the sorting algorithm, but this will get exponentially very expensive, as we will be searching from the last element of the sorted array. On the other hand, there will still be some information about that algorithm. So we'll use it only after all sorted numbers are put into the list.

For the sake of discussion, let's take the same example again. Let x = [1, 2], y = [3, 4], z = [5].

This method takes the following steps:

We first sort x in ascending order of 1 to 5. After sorting, we find out the last element in ascending order of a sorted array. Next, we sort y from 3 to 5, and lastly z from 5 to 1:

The pseudocode becomes:

This method does very little work and takes O(N log 2N), depending on how many elements are in the array. If you check some more examples in our textbook, we come across another version of this algorithm called Bubble sort, where it takes O(N log N) and O(N log 2N) time. So, why do different versions differ?


Bubble sort

A Bubble sort consists of splitting the data into blocks, and then applying this algorithm to each block, and adding them all up. Because each block is not big enough to stay in memory the chunks get written to the stack. You will see the bubble sort algorithm here called the min-heap sort.


Bubble Sorting in C++
Bubble Sorting in C++


So, in the case of a Heap Sort, if we decide to make it, we have to store the smallest element of the array in the beginning first and continue to store the elements of the array in the following way, increasing the index size to keep the data in the memory. Hence, it takes O(N log N) O(N log N) time. Here, the term “N” refers to the number of elements you want to store in your array. Let's take an example again to understand better:

Let x = [1, 2, 3], y = [4, 5, 6], z = [7, 8, 9].

This algorithm divides into four phases

1. First, it sorts x in ascending or descent order. Now, the current element is stored in the head of the queue(first the element in the line). If you put the first element in the front then it will become the top node. Otherwise, it will move to the tail. At this point, the elements are at an odd position. In particular, the largest element will be placed as the result in decreasing order.

2. Then after that, it starts dividing the data into multiple segments. First, start putting the tail. And then start writing the new element at the beginning in descending order from the original elements, taking them into account. So, if the last element is greater than the original elements, then it will also be shown to be the next item to be added to the queue, followed by the tail. These first two steps take O(N) time. So, if you want to reduce the time, you can increase the priority of the new items.

3. Finally, it adds all the items to a single result item. After this phase, it stores the entire result in the memory as an object. So, it takes O(N) time.

At first glance, these methods may seem that heap sort takes a lot of time because it goes through the whole array with every addition. But if we dig deeper, we find that when you divide the input array into blocks, then it makes sure that the remaining portion of the algorithm in the middle, which saves us time during recursion, doesn’t consume much space. There is another reason why it performs better than the previous algorithms and not other algorithms: while iterating over the sequence, it uses pointers for keeping track of the items. And since all the elements get added at once, we can save time.


Difference between Bubble & Min-heap Sort

Now that we have compared what happens in both algorithms, let's find out their similarities. As you already know, min-heap is faster than bubble sort, but they're similar from the implementation standpoint. They both begin with an initial state (first element of the array) and compare all its elements later in the course of operations. Only min-heap sort starts with sparing a set of elements at the beginning. Both Bubble sort and Min-heap Sort are greedy algorithms, meaning that the algorithm tries to add the latest element that comes from the head of the queue to its right and returns the newest one from the left. That means that this approach consumes O(N log N) O(N log N) time.

And finally, they both return a new array with the most recent elements added to it, except in bubbles it will be returned before returning the old array, but in min-heap sort, it will return the results to be added to the waiting one, so it will consume lower time at the cost of slower performance.

So with both bubble and min-heap sort, the time required to run this code is almost identical. However, the time it requires to generate an empty array for the bubble sort is lower, because it doesn’t do any additional calculations. While the time required by min-heap sort is higher because it needs to calculate the average time taken to insert the elements into the queue. In either case, the running time will take away the ability of this algorithm to scale to bigger sizes.

Pseudo-code for Bubble sort:

The pseudo-code is divided into three parts

1. First, we start a variable counter to count the time for each iteration of the algorithm. Each time we process an item, we push it to the front counter, and that pushes it onto this item from the beginning (leftmost) of the array for processing.

2. Second, we push all the items to the beginning of the loop. Finally, we push the last item to the tail.

3. Thus, our results should look like the following picture:

We found out that bubble sort took O(N log N) O(N log N) time. In such a situation, it doesn't matter if you are choosing between min-heap or bubble and min-heap. As long as the time taken to finish the task is lesser than two times the original sum of the time, then it should be the best choice for you.


Conclusion:

To conclude, here we have covered different kinds of sorting algorithms. Each kind of algorithm has advantages and disadvantages. For example, heap sort works great for objects that take a long to fill the memory, or need random access to an element. On the contrary, the min-heap sort is good for small arrays and is a good option for beginners who are learning data structures. Bubble Sort tends to consume significantly less time due to the constant re-assigning of the elements in the array when the element gets removed from the array. Bubble Sort is used in various types of situations and is suitable for solving many common problems, while min-heap sort is effective for smaller tasks and might be better to solve problems that require a complex mathematical operation. Remember to remember the data structure and algorithm!


Post a Comment

0 Comments