Skip to main content

Basic Sorting and Bubble Sort

Basic Sorting

When we have a few objects be it of any kind, and we want to sort them this means that we want to rearrange them somehow so that they follow some order, an order that we define.  We can take for example one of the fields, for example if we have sheep, and each sheep has a name, and we want to order the sheep by name then we could just take any sheep and first place the sheep named Avida, and then take the sheep named Guliantra and so on, lexicographically.

Respectively we could sort the sheep that we have by their age, and in this case Guliantra could end up before Avida.

In computer algorithms, sorting is the like the basic algorithms, and you will find that to solve many other problems you would start by first sorting and then progress to actually solve the problem.  So sorting could be the first step of many other problem solutions.

While there are a few types of sorting algorithms we have a few shared metrics to evaluate them all.  So when you refer me to a specific algorithm we could ask, is this sorting algorithm stable? Is it sorting in place? What is the average worst and best case running time of this sorting algorithm? Which type of data is this sorting algorithm best suited for, how much memory does this sorting algorithm require and does it need actually extra memory?

Let's go through these terms of stable, in place, running time, memory terms.

Stable Sort

Once you sort a few sheep let's say by year age, it might be the case that a few sheep have the same yearly age.

The question then is this, if the sheep are all lined up but not sorted by yearly age, and you sort them out by their yearly ages, if two sheep have the same age would they have different relative order after the sorting?

Note that we didn't say absolute order, because it might be well the case that the two oldest sheep with the same age are lined up at first on the beginning of the row, but at the end of the sort you line them up at the end of the row.

The question is just if when you sort and line up these oldest sheets at the end of the line if they preserve their original ordering, if they always do when sorted by your sorting algorithm, then we say that your sorting algorithm is relative order preserving for equal items or in other words a stable sort.

Stable sorts include: Merge sort, insertion sort, radix sort, tim sort, bubble sort.
Unstable sorts include: Heap sort, quick sort.

In Place

When we ask if a sorting algorithm is in place or not we ask about how much extra memory it's using.  As long as it's using a constant amount of memory relative to the list that you sort then we say it's in place.  You can use additional memory for your sorting, that is all fine, but the question is whether the extra memory is of constant space meaning, it's not growing relatively to the number of items you sort.

It does not matter the relation between the additional memory use to the number of items in list, any such relation means that it's not in place.  In order for a sorting algorithm to be in place it must use O(1) extra amount of memory, meaning no extra relation to the original items that you sort, you can sort as many items as you wish your sorting algorithm must only use extract constant amount of memory in order to be called in place.

Bubble Sort

Bubble sort is a stable sort, it's also in place, it's best time complexity is O(n), it's average time complexity is O(n^2), it's worst time complexity is O(n^2), and it's space complexity is O(1).

In bubble sort we compare each successive part of elements in some given input list, and we invert each such pare if they are not in order.

After we finish one iteration of scanning the whole list, comparing each two items and replace each such two if they are inverted then if we started this comparison from the beginning of the list then this would mean we bubble up the largest element to the n-1 place the end of the list.

Bubble sort is also known as sinking sort because it's repeatedly comparing each two items and swaps if they are at the wrong order, so the items are like sinking to one end of the list.

After one iteration of comparing all the pairs you are sure to have one item sorted, the largest item if you start from the beginning of the list, and after two such iterations of the whole array you have the two largest items ordered at the end of the list.

We said that the best running time of bubble sort is O(n) that is because if we scan the list once, and we see we need not swap any two items we can shortcut and end the sorting here, as we saw the list is sorted already.


Popular posts from this blog

Dev OnCall Patterns

Introduction Being On-Call is not easy. So does writing software. Being On-Call is not just a magic solution, anyone who has been On-Call can tell you that, it's a stressful, you could be woken up at the middle of the night, and be undress stress, there are way's to mitigate that. White having software developers as On-Calls has its benefits, in order to preserve the benefits you should take special measurements in order to mitigate the stress and lack of sleep missing work-life balance that comes along with it. Many software developers can tell you that even if they were not being contacted the thought of being available 24/7 had its toll on them. But on the contrary a software developer who is an On-Call's gains many insights into troubleshooting, responsibility and deeper understanding of the code that he and his peers wrote. Being an On-Call all has become a natural part of software development. Please note I do not call software development software engineering b

Containers - Quick Low Level Guide

Containers Kernel, namespace, cgroups Kernel space and user space Before we actually get to explain containers let's define what is a kernel.  Because you know there is no such thing in reality as a kernel it's only how we name things, and different people name things differently. cgroups, namespaces, UFS We are going to discuss containers, cgroups, namespace, UFS, hypervisor, user space, kernetl space and more.   When we say "kernel" we mean this.  We have the hardware, this is not the kernel, now above the hardware we have a few layers of software, imagine now two boxes. User mode is all the application you run while the kernel is the lower level is all the virtual memory management scheduling, connection to hardware devices, network drivers, it's basically the abstraction on top of the hardware + the basic services which allow this. One box is closer to the hardware and contains a few layers, the second box sits on top of the kernel box and contains

Recursion Trees Primer

Recursion trees. Controlling the fundamentals stands at the cornerstone of controlling a topic.  In our case in order to be a good developer its not enough or even not at all important to control the latest Java/JavaScript/big data technology but what's really important is the basics.  And the basics in computer science are maths, stats, algorithms and computer structure. Steve wosniak the co-founder of apple said the same, what gave him his relative advantage was his deep understanding of programming and computer structure, this is what gave him the ability to create computer's which are less costly than the competitors (not that there were many) and by the way there were 3 founders to apple company one responsible for the technical side, one for the product and sales (Steve Jobs) and the third responsible for the company structure and growth, each of the three extremely important, it was not only the two Steve's but that's a topic for another episode. And with t