Page Count

Monday, November 30, 2015

Insertion Sort With Time Complexity

package com.sbs.sort;

/**
 * @author Bhargava
 *
 * Time Complexity of Insertion Sort is O(n power 2) Worst Case  O(n) best case

 * Recommended for Smallest array of elements.

 * If you observe Array.sort() method in java.util api, if the array length is smaller than 7 our api is following     * insertion sort.


 */
public class InsertionSort {

    /**
     * @param args
     */
    public static void main(String[] args) {

        long intialTime = System.currentTimeMillis();
        int[] array = { 24, 13, 9, 64, 7, 23, 34, 47 };
      
        System.out.print(" Befor Sorting [ ");
        for (int j = 0; j < array.length; j++) {
            System.out.print(array[j] + " ");
        }
      
        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            int temp = 0;
            while (j >= 0) {
                if (array[j + 1] < array[j]) {
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
                j--;
            }

        }

        System.out.print(" After Sorting [ ");
        for (int j = 0; j < array.length; j++) {
            System.out.print(array[j] + " ");
        }
        System.out.print("]");

        System.out.println(" Time Lapse :"
                + (System.currentTimeMillis() - intialTime) + " ms ");
    }

}

String Pool Vs String Definition (Using 'new' Operator)

Strings can be defined in Java in two ways.

1. String Literals
2  Traditional way(using 'new' operator)

String Literals

If a String is created by using String literal notation, memory will be allocated directly in string pool.
String pool is subset of Heap memory (Where objects will be created).

e.g. String companyName = "Surimenus";
     String empName= "Bhargav";
     
Using 'New' Operator

If a String is created using new operator, memory will be allocated in Heap Memory not in String pool.

e.g.

String companyName = new String("Surimenus");
String empName= new String("Bhargav");


String Pool vs Using 'new' Operator

String which are created in String pool will re-reference by reference which contains the same content.


For example consider the following.

String cn1 = "Surimenu";
String cn2 = "Surimenu";

In the above scenario cn1 and cn2 references having the same content and these are created in String Pool. So for only one Object memory will be allocated for the two references (cn1 and cn2)


For suppose if you create String Objects as follows.

String cn1 = new String("Surimenu");
String cn2 = new String("Surimenu");

In the above scenario cn1 and cn2 references having the same content and these are created in Heap memory. So memory will be allocated for two Objects for two references (cn1 and cn2).



















Sunday, November 29, 2015

Self Signed Certificates Vs Signed Certificates (CA Certificates)

Certificates

Certificates basically two categories.
Self Signed Certificates  - will create by self
CA Certificates  - will be  provided by Third party vendor with robust algorithms

Depends on the location of installing the certificate these are two types
1. Public Key Certificates (Client Side)
2. Private Key Certificates (Server Side)

Self Signed Certificates

 
If any one is using self signed certificates in their applications
they have to make sure both server side and client side certificates are in sync.
Other wise we should be ready to face SSLHandShake Exceptions. These will be preferable mostly for lower environments not for production.

CA certificates 


If you install CA certificates on server side, client side certificates are installed automatically whenever they access the server. So in production for CA certificates there is no need to install the client side certificates.

We can generate a Self Signed Certificate using Java Key tool
JAVA_HOME/bin/keytool.exe








Connection Time out Vs Socket Time out

Time out
Any client(or source) which is unable to connect to the server (or Destination) in a specified time, then requests will automatically gets time outs.

Port
Port numbers allow different applications on the same computer to utilize network resources without interfering with each other. Port numbers most commonly appear in network programming, particularly socket programming. Sometimes, though, port numbers are made visible to the casual user. For example, some Web sites a person visits on the Internet use a URL like the following:

http://www.appdomain.in:80/ In this example, the number 80 refers to the port number used by the Web browser to connect to the Web server. Normally, a Web site uses port number 80 and this number need not be included with the URL (although it can be).

Port 80 is the default port for HTTP

Socket
Each and every communication from one application to another application should happen through sockets. Socket is gateway to send/receive information from one application to another.

A TCP socket is not a connection, it is the endpoint of a specific connection.
Every connection between a client and server requires a unique socket.
Applications can create multiple sockets for communicating with each other.
Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data

A socket contains following parameters

    An IP address
    A transport protocol
    A port number

A socket = Transport protocol + IP Address + a port (numeric address)   


   
Connection Time out Vs Socket Time out

Connection time outs will occur when requests are not able to reach remote machine on the specified
Host name/IP on specified port number under specified time.

So this Connection time outs can occur or can be simulated
1.By providing the incorrect host names or Port numbers.
2.By providing the time out period which is less than the actually it takes.
  This has to set at the code level while we creating the connection.

Socket time out will occur when requests are not able to reach the application which is running in local or remote machines.

So this socket time outs
1. By providing the time out period which is less than the requests actually takes.
2. Shut down the application which client is trying to connect.