Tuesday, December 16, 2014

Insertion Sort Algorithm in Java to Sort Array

Insertion sort is another simple sorting algorithm, which can be used to sort any linear data structure like array and linked list. On simplicity this is next to bubble sort, and it’s also pretty close to how humans manually sort something (for example, a hand of playing cards). As name suggest, Insertion sort is based upon insertion of element in a sorted list. To start, we assume that first element is already sorted. Then we pick next element and put it on second place, we compare this number with first element and if they are not in sorted order, we swap them. This gives a new sorted list of 2 elements. Now we pick third element and put it on 3rd place and compare it with 2nd placed number, if they are not in sorted order, we swap them, if all three elements are still not in sorted order then we again swap 1st and 2nd element, now we have a sorted list of three numbers. So in best case, we just need one comparison and no swapping, while In worst case we need to compare new number with each number in the sorted list i.e. k comparison, and k -1 swapping, where k is the length of sorted list. I think Insertion sort is pretty easy to understand, isn't it; if you still don't get remember how you arrange your hand of cards, first you pick one card, then pick next card and put it after first card if it is bigger or before first card if it is smaller; then you pick another card and insert it to its proper position.  One more real world example of insertion sort is how tailors arrange shirts in cupboard, they always keep sort in sorted order of size and insert new shirt at right position. By the way if you notice trend then you can see that as length of our sorted list increased, so as number of comparison and swapping, which means this this sorting algorithm is not suitable for large data set. Indeed, it’s not as good as quicksort, heap sort or merge sort for sorting large arrays, but it’s good enough to sort small integer arrays where cost of comparison is small. By the way this algorithm is not just bound to numbers or integers, you can also sort String array with insertion sort or any list of Employee, depending they implement Comparator or Comparable to provide comparison logic. Insertion sort can also be used to sort any List of elements in Java. We will see more advantages and disadvantage of insertion sort in last section for now let's see pseudo code for insertion sort.

Wednesday, December 10, 2014

9 Things about Null in Java

Java and null are uniquely bonded. There is hardly a Java programmer, who is not troubled by null pointer exception, it is the most infamous fact about. Even inventor of null concept has called it his billion dollar mistake, then why Java kept it? Null was there from long time and I believe Java designer knows that null creates more problem than it solves, but still they went with it. It surprise me even more because Java's design philosophy was to simplify things, that's why they didn't bothered with pointers, operator overloading and multiple inheritance of implementation, they why null. Well I really don't know the answer of that question, what I know is that, doesn't matter how much null is criticized by Java developers and open source community, we have to live with that. Instead of ruing about null it's better to learn more about it and make sure we use it correct. Why you should learn about null in Java? because If you don't pay attention to null, Java will make sure that you will suffer from dreaded java.lang.NullPointerException and you will learn your lesson hard way. Robust programming is an art and your team, customer and user will appreciate that. In my experience, one of the main reasons of NullPointerException are not enough knowledge about null in Java. Many of you already familiar with null but for those, who are not, can learn some old and new things about null keyword. Let's revisit or learn some important things about null in Java.

Friday, December 5, 2014

How to do static import in Eclipse - Java

Do you know what is shortcut of doing static import in Eclipse? Well I didn't know before, but today I come to know that shortcut Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports but It can also help with static import in Java program.  Suppose you are using lots of static variable from a utility class e.g. TimeUnit by referring them with class name, just like we refer static variable. In Eclipse IDE, you can select the whole reference variable and press Ctrl+Shift+M and it will automatically import that static element using static import in Java.
For example, if you have following code in your class, you can select TimeUnit.SECONDS and then use shortcut Ctrl+Shift+M to statically import SECONDS variable in your program, as shown in first and second screenshot.