Monday, January 27, 2014

Quick Sort Demo


This is a divide and conquer approach. Take a pivot as the last element of array, and place all numbers lesser than this to the left. This is done using partitioning as seen in the code.
Complexity:
Best - O(n logn); Average - O(n logn); Worst - O(n^2)

Monday, December 30, 2013

Learning Data Structures now

Nowadays, I am learning data structures starting with simple sorting techniques, searches, linked-list, stack algorithms etc, and I will post about my experiences. I don't know how far, but I'll share so far I learn. :-)

Insertion Sorting Logic

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. -Wiki

Monday, December 16, 2013

LinkedList: Node creation and display example


package ds;

/**
 * @author Rajdeep
 * 
 * Note
 * Creating the list:
 * 1. 'start' refers to the first node and 'now' is made to refer to the latest
 *   added node.
 * 2. after creating the first linkedlist object and assigning value,
 *   'start' points to it because we are checking for start==null
 *   'start' is null in first node addition only.
 *   'now' also points to it, this being the latest node.
 * 3. Second node onwards, start is never null, so control goes to else part.
 *   'now.next' points to nothing because its of reference type.
 *   so assign 'now.next' to the newly created object after
 *   checking 'now.next==null'.
 * 4. Now 'start' pointed or the first object's (node's) 'next' part is linked
 *   to the 2nd object since now was referring to it, and then after
 *   'while' execution, 'now' points to the newly created node, referred by
 *   the local 'temp' variable.
 * 5. e.g.: start -> [10|add1] at add0 -> [20|add2] at add1 -> [30|NULL] at add2;
 *   'now' points to this add2 having info 30, and 'now.next' is NULL,
 *   so that when another node is created, 'now.next' refers to the new
 *   node, and then 'now' refers to the new node, and so on.
 * 
 * Displaying the list:
 * We start from the starting node, and display 'info' part of each node, and
 * move on to next node by checking the current node's 'next' part is not null.
 */

// This is the Linked List node
class LinkedList{
 int info;   //has value
 LinkedList next; //for pointing to next node
}

// Demonstration on creation of node and displaying the values
public class LinkedListDemo {

 static LinkedList start = null;
 static LinkedList now = null;
 
 public static void main(String[] args) {
  createNode(0);
  createNode(1);
  createNode(5);
  createNode(7);
  createNode(10);
  createNode(11);
  createNode(54);
  createNode(76);
  
  display();
 }
 
 static void createNode(int val){
  LinkedList temp = new LinkedList();
  
  temp.info = val;
  if (start==null){
   start = temp;
   now = temp;
  }
  else{
   while(now.next==null){
    now.next=temp; 
   }
   now = temp;
  }
 }
 
 static void display(){
  LinkedList temp = start;
  System.out.print("[");
  while(temp!=null){
   System.out.print(temp.info + "|");
   temp = temp.next;
  }
  System.out.println("\b]");
 }

}
The output will be:
[0|1|5|7|10|11|54|76]
This was a demo program for my understanding, and now I understand how the nodes are connected, and so the term "linked list".

Tuesday, October 1, 2013

The try-catch-finally love affair!

Ok, everybody here already know about the affair, I am just discussing some more bits of this love triangle.
Lets dig hard to the shallow level :)

try{
    //try something
}
//and if the trial goes wrong, catch the issue and handle
catch(Exception e){
    //handling the problematic part
    //or at least letting the program to continue
    //and exit gracefully and not shut at face!
}
//but
finally{
    //do this, so you are the winner!!
}

So from even the baddest of our common-sense, we smell that finally block supercedes the two others, viz., try and catch. Lets prove a point in this.