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.


Comments

Popular posts from this blog

API Design Paper Summary and Review

API Design Paper Summary Introduction Is building API a solvable question, how far can we get into having good API’s and what is a good API at all? these are all very hard questions, usually you know the answers once you designed multiple APIs and got experience and then reviewed the decisions you have taken. Fortunately there are papers dealing with this problem exactly, for complex API’s used by a huge amount of people, the Qt API for example a very populate framework for desktop GUI building, and today we are going to go through a summary of that paper.

“The Little Manual of API Design” is a very nice paper written by Jasmin Blanchette has released a paper while working in trolltech, which is a Nokia company. I found it to be very clear and concise, and reassuring what we think of API design. It’s a difficult task that includes both artistic, social, programming and scientific skills. We are going to summarize this paper for you.

When you write an API you combine a set of symb…

Dev OnCall Patterns

IntroductionBeing 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 because …

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 that l…