Insertion Sort implementation in Java

One of the simplest (in terms of understanding :P) sorting technique is Insertion Sort.
You will find many sites giving an example of cards. Like, suppose left hand has already sorted cards where as the right hand has unsorted. You can pick the first card from right hand unsorted and try to put it in the left hand so that the sorted order preserves in the left hand.

Suppose you never played cards in your life (means you wasted your childhood 😛 ).
You can also think of 2 queues of people ( A and B). In queue A, all the people are standing in incremental order of their height and in queue B, no one gives a shit about any order. Now suppose a person P from queue B is tired of those people and wants to join queue A. So when he joins queue A, he has to start from the person who is standing last and find a position so that the order of height preserves. In this process, all the people from queue A who are taller than P, has to shift their place one step back.

Now let’s implement this in java using ArrayList.

My project Explorer:

InsertionSortProjectExplorer

InsertionSort.java

MainPractise.java

Output:

 

You can have above code from GIT.

GitHub-Mark-32px https://github.com/Niravkumar-Patel/SnippetExampleRepo.git

If you have any question email us on
snippetexample@gmail.com or Post comments or join us on Facebook.


Leave a Reply

Your email address will not be published. Required fields are marked *