Tuesday, 12 July 2016

Bubble Sort

Prerequisite:
Algorithm, Sorting, Asymptotic complexity(time complexity, space complexity)

Introduction:
Bubble sort may look same as insertion sort, because the outcome of the bubble sort looks same as insertion sort. In case of sorting in ascending order, Insertion sort will keep taking out the min value at end of the each iteration, whereas bubble sort will take out the max value. So both insertion and bubble sort look same.But there are few differences, when you look at the implementation.

Code:

 public int[] doBubbleSort(int[] ip) {  
      boolean isSwapped = false;  
      for(int i=1;i<ip.length;i++){  
           for(int j=0;j<ip.length-i;j++){  
                if(ip[j]>ip[j+1]){  
                     swap(ip, j, j+1);  
                     isSwapped = true;  
                }  
           }  
           if(!isSwapped){  
                break;  
           }  
      }  
      return ip;  
 }  

Complexity:
The time complexity of this sorting is O(n2) in a worst case scenario. In a best case scenario where the list is already sorted, the time complexity is O(n). The auxiliary memory/space is taken by this algorithm will never increase based on the number of elements. It always use the constant extra space. So the space complexity is O(1).

When/where to use:
Even though it has the interesting implementation and the fancy name, it will not be suitable for any case. We can say It's suitable for smaller array which has 10 or 15 elements, because of easy implementation. You can also use this algorithm, if the array is already sorted where the complexity is O(n). But in such cases, insertion sort is a better choice than this.

Sunday, 10 July 2016

JMS

(The main objective of this article is to identify the places where JMS is needed)

JMS(Java messaging service) is a widely used technology. It has two different mechanism called Queue and Topic(publish-subscribe). A queue may have multiple listeners. But when u push a message, it will be picked up by only one listener whereas in topic, message will be received by all the subscribers who subscribed to the topic.

So now the question is where does it need?

Let's consider a case, you have a centralised database which has to be updated by the multiple applications. But you don't want other application to access this database. So here, you can have a queue and all the applications will just push their data to this queue. You will take care of dequeue them, process them & store them in the DB.

In another case, you need to send a mail and call a web service, before Storing the user data into your application database, when user submit a request.

Here, there's no guarantee that the email service and the web service will always be up and running. So the entire flow will fail or we have to skip the mailing part and/or calling web service, if they are down. Even though both the servers are up, there'll be some delay in storing data and sending the response back to the user with respect to the time taken by the external services.

To avoid such issues, we can have a queue where we push the messages and go ahead with the storing data in DB. A separate JMS listener component will take care of sending emails and calling/updating the web service.

You may think that we can store the same data which we are pushing to the queue, in DB and later, we can fetch them from DB and process them.Yes, you can also use DB. If you are ok to expose your database to other components or if you want to store those data permanently or if the size of the message is large, you can go for DB instead of queues.

If your application needs to send a message to multiple applications/components, you may go for the topic. As we told before, if a message comes into the topic , it will be received by more than one subscribers which is actually an another application or component. Queue and Topic both can be used as a middleware which use to connect components/applications written in various languages.

Thursday, 7 July 2016

Insertion sort

Prerequisite: 
Algorithm, Sorting, Asymptotic complexity(time complexity, space complexity)

Introduction:
Insertion sort will take elements from 0 to n and compare & swap it with the left part of the array which is already sorted among the elements, until it find the right place where the element has to be. You may be clear, when look at the below code,

Code:
public int[] doInsertionSort(int[] ip) {
 for(int i =1;i<ip.length;i++) {
  for(int j=i;j>0;j--) {
   if(ip[j]<ip[j-1]){
    swap(ip, j, j-1);
   }else {
    break;
   }
  }
 }
 return ip;
}

Performance:
Time complexity of this sorting is O(n2) in a worst case scenario, O(n) in a best case scenario. O(n), when the array is already sorted. Worst case space complexity is O(1).

Where/when to use:
Even though it took more time or steps to sort the list than the other algorithms like Heap & merge sort, we still can use it in some places.It's easy to implement as selection sort, so we can use this for smaller arrays. But it may do more swaps than selection sort.
It's good to use this sort in a place where the element are all already sorted.Let's consider you already have a sorted list & a new element is added to the list. So now u need to place the last element in right place to make the list sorted again.

Tuesday, 5 July 2016

Design Patterns

          A programmer may write a better program to solve a problem. He may consider the memory management, Asympotatic complexity. 
          An Object oriented programmer will see everything as an object. Once he got the requirements, he can easily see all the objects or entities involved in it & he can write the services of the objects. 
          An application designer will figure out the relationship between the Objects and wire them by any design. A well designed application will help us in easy maintenance.
We can say that a better programmer would have all roles which We mentioned above.

There are several design issues have already been resolved by programmers. Below are the names of those design patterns,

  1. Observer pattern
  2. Decorator pattern
  3. Factory pattern
  4. Singleton pattern
  5. Command Pattern
  6. Adapter & Facade pattern
  7. Template method pattern
  8. Iterator & Composite pattern
  9. State pattern
  10. Proxy pattern
  11. Immutable Objects Pattern
  12. Mediator Pattern

Monday, 4 July 2016

Selection Sort

Prerequisite: 
Algorithm , Sorting , Asymptotic complexity(Time & Space Complexity)

Introduction :
If someone gives you a list to sort and we are not aware of various sorting algorithm, most of us will write the below code, 

Code:
 public int[] doSelectionSort(int[] ip) {  
      int temp = 0;  
      for(int i=0;i<ip.length-1;i++) {  
           temp = i;  
           for(int j=i+1;j<ip.length;j++) {  
                if(ip[j]<ip[temp]) {  
                     temp = j;  
                }  
           }  
           if(i!=temp) {  
                swap(ip, i, temp);  
           }  
      }  
      return ip;  
 }  
Performance: 
Time complexity of this sorting is O(n2) regardless of worst or best case. It requires O(1) auxiliary space regardless of any case. It has relatively poor performance than others. 

Where/When to use:
The only use of this algorithm is that it is easy to implement. But we may use it in few places. for instance, we have less than 10 elements in the list or we need only top 5 records out of 1000 or more records.
You may get some confusion that why i said we can use this sorting to find the top 5 record out of 1000 elements. Because we will be having 5*1000 comparison & we can skip the remaining comparisons in this sorting whereas insertion sort and merge sort has to execute all the comparisons with the time complexity O(n2) and O(n log n) respectively. You may think that bubble sort will also have the same 5*1000 comparison.But it needs more swap than the selection sort in a worst case scenario.