Sunday, May 13, 2012

Selection Sort in Java


public class SelectionSort {
    public static void main(String[] args) {
       int[] array = {6,7,3,8,1,2,3,4,5};
       new SelectionSort().selectionSort(array);

       for(int i=0; i < array.length  ; i++)
           System.out.println(array[i]);        
    }

    public void selectionSort(int[] array ){
       int min = 0;
       int length = array.length;

       for (int i = 0; i <= length - 2; i++){
           min = i;
           for(int j = i+1 ; j <= length - 1 ; j++){
                 if(array[min] > array[j]){
                    min = j;
                 }
       }

            if(min != i){     // This if condition is not there in original algorithm. It helps unnecessary swapping of same value in to same location again (min = i) 
                int temp = array[i];
                array[i] = array[min];
                array[min] = temp;
            }
        }
      }
}

Bubble Sort in Java


public class BubbleSort {
    public static void main(String[] args){
       int[] array = {1,2,3,4,5};
       new BubbleSort().bubbleSort(array);

       for(int i=0; i < array.length  ; i++)
           System.out.println(array[i]);        
    }

    public void bubbleSort(int[] array){
       int length = array.length;
       int count = 0; //counts if no swapping is done

       for(int i = 0 ; i <= length-2 ; i++){
           count = 0;
           for(int j = 0 ; j <= length-i-2 ; j++){
                 if(array[j] > array[j+1]){
                     int temp = array[j];
                     array[j] = array[j+1];
                     array[j+1] = temp;
                     count ++ ;
                   }                      
               }
            if(count == 0){  // This if is not there in actual algorithm. If there are no swaps in the pass that means the array is sorted
                  System.out.println("broke on i = " + i);
                  break;                 
            }
         }
    }  
}

Insertion Sort in Java


public class InsertionSort {
    public static void main(String[] args){
       int[] array = {6,7,3,8,1,2,3,4,5};
       new InsertionSort().insertionSort(array);

       for(int i=0; i < array.length  ; i++)
           System.out.println(array[i]);        
    }

    public void insertionSort(int[] array){
       for(int j=1; j <= array.length-1; j++){
     insert(array, j);
       }      
    }
    
    //insert array[index] into sorted sequence array[0]...array[index-1]
    private void insert(int[] array, int indexToInsert){
     int key = array[indexToInsert];
     int i = indexToInsert - 1;
     
     while(i >=0 && array[i] > key){
           array[i+1] = array[i];
                        i--;
     }
     array[i + 1] = key;
    }
}

Saturday, May 12, 2012

Doubly linked list implementation in java

I used generics here to write this.

public class DLLNode {
   private E data = null;
   private DLLNode prevNode = null;
   private DLLNode nextNode = null;

   public E getData() {
     return data;
   }

   public void setData(E data) {
     this.data = data;
   }

   public DLLNode getPrevNode() {
     return prevNode;
   }

   public void setPrevNode(DLLNode prevNode) {
     this.prevNode = prevNode;
   }

   public DLLNode getNextNode() {
     return nextNode;
   }

   public void setNextNode(DLLNode nextNode) {
     this.nextNode = nextNode;
   }
}


public class DoublyLinkedList {
  private DLLNode headNode = null;
  private DLLNode tailNode = null;
  private int nodeCount = 0;

  public void addFirst(E data){
    DLLNode newNode = new DLLNode();
    newNode.setData(data);

    if(headNode == null && tailNode == null){
       headNode = tailNode = newNode;  //new node becomes both head and tail
    }
 
    else{            
       newNode.setNextNode(headNode);
       headNode.setPrevNode(newNode);
       headNode = newNode;
    }

    nodeCount++;     
 }

 public void addLast(E data){
    DLLNode newNode = new DLLNode();
    newNode.setData(data);

    if(headNode == null && tailNode == null){
       headNode = tailNode = newNode;  //new node becomes both head and tail
    }

    else{            
       tailNode.setNextNode(newNode);
       newNode.setPrevNode(tailNode);
       tailNode = newNode;
    }

    nodeCount++;     
  }

  public boolean delete(E data){
     DLLNode curNode = headNode;
     DLLNode prevNode = headNode;

     while(curNode != null){
        if(curNode.getData().equals(data)){   //find the node which has the data
              prevNode = curNode.getPrevNode();

              if(nodeCount == 1){ // if there is only node in the list, and that one node contains the data to be deleted
                  headNode = tailNode = null; // now both head and tail points nowhere                             
               }   
  
              else if(curNode == headNode){
                  headNode = headNode.getNextNode(); // move the head node to next node
                  headNode.setPrevNode(null); // make new head node's  prev node value as null
                  curNode.setNextNode(null); // set the next node of the previous head node (cur node) to null                                      
               }

         else if(curNode == tailNode){
                  tailNode = prevNode;       // make the tail node as previous node
                  tailNode.setNextNode(null); //set the next node of tail node as null, as this is the tail node
                  curNode.setPrevNode(null);  //make cur node not point to prev node                       
                }

               else{
                  prevNode.setNextNode(curNode.getNextNode());  // make the prev node point to next node of cur node
                  curNode.getNextNode().setPrevNode(prevNode);  //make next node point to pren node
                  curNode.setNextNode(null); // make cur node not point to next node
                  curNode.setPrevNode(null); // make cur node not point to prev node                       
                }

                nodeCount--;
                return true;  //return true to say that data is found and is deleted 
             }           
             curNode = curNode.getNextNode();
      }
      return false;  // //return false to say that data is not found and is not deleted
  }


  public void displayLinkedListFromHead(){
      DLLNode node = headNode;
      System.out.println("Node Count: " + nodeCount);
      if(headNode != null){
          System.out.println("Head Node: " + headNode.getData());
          System.out.println("Tail Node: " + tailNode.getData());
      }

      while(node != null){
          System.out.println(node.getData().toString());
          node = node.getNextNode();
      }
 }    

  public void displayLinkedListFromTail(){
      DLLNode node = tailNode;
      System.out.println("Node Count: " + nodeCount);
      if(headNode != null){
           System.out.println("Head Node: " + headNode.getData());
           System.out.println("Tail Node: " + tailNode.getData());
      }

      while(node != null){
           System.out.println(node.getData().toString());
           node = node.getPrevNode();
      }

    }    
}

public class TestDLL {
    public static void main(String[] args){
       DoublyLinkedList list = new DoublyLinkedList();
       list.addFirst(1);
       list.addFirst(2);
       list.addLast(3);
       list.addLast(4);

       //list.delete(2);
       //list.delete(3);
       //list.delete(2);
       list.delete(4);
  
       list.displayLinkedListFromHead();
       list.displayLinkedListFromTail();
      }
}

Singly linked list implementation in java

I used generics here to write this.

public class Node {
   private E data = null;
   private Node nextNode = null;

   public Node(E data){   
     this.data = data;
     this.nextNode = null;
   }

   public Node(){   
   }

   public E getData() {
       return data;
   }

   public void setData(E data) {
      this.data = data;
   }

   public Node getNextNode() {
       return nextNode;
   }

   public void setNextNode(Node nextNode) {
        this.nextNode = nextNode;
   }
}


public class SinglyLinkedList {
     private Node headNode = null;
     private Node tailNode = null;
     private int nodeCount = 0;    

     public void addFirst(E data){  // add to the head
       Node newNode = new Node(data);      

       if(headNode == null && tailNode == null){ // if list is empty, newNode is both the head and tail
           headNode = tailNode = newNode;                        
       }

       else{  // make newly added node as head node
           newNode.setNextNode(headNode);
           headNode = newNode;                             
       }

       nodeCount++;  
     }

    

     public void addLast(E data){ // add to the tail
       Node newNode = new Node(data);

       if(headNode == null && tailNode == null){ // if list is empty, newNode is both the head and tail
          headNode = tailNode = newNode;                        
       }

       else{ // make newly added node as tail node
          tailNode.setNextNode(newNode);
          tailNode = newNode;                 
       }
       nodeCount++;
     }    

     public boolean delete(E data){
       Node curNode = headNode;
       Node prevNode = headNode;

       while(curNode != null){
             if(curNode.getData().equals(data)){   //find the node which has the data
                  if(nodeCount == 1){ // if there is only node in the list, and that one node contains the data to be deleted
                     headNode = tailNode = null; // now both head and tail points nowhere                             
                   }   
                  else if(curNode == headNode){
                     headNode = headNode.getNextNode(); // move the head node to next node             
                     curNode.setNextNode(null); // set the next node of the previous head node (cur node) to null null                                      
                   }   

                  else if(curNode == tailNode){
                    tailNode = prevNode;       // make the tail node as previous node
                    tailNode.setNextNode(null); //set the next node of tail node as null, as this is the tail node
                  }
                  
                  else{
                    prevNode.setNextNode(curNode.getNextNode());  // make the prev node point to next node of cur node
                    curNode.setNextNode(null); // make cur node not point to next node                             
                  }
 
                  nodeCount--;
                  return true;  //return true to say that data is found and is deleted 
             }

             prevNode = curNode;
             curNode = curNode.getNextNode();
       }
       return false;  // //return false to say that data is not found and is not deleted
     }

    
     public void displayLinkedList(){
       Node node = headNode;
       System.out.println("Node Count: " + nodeCount);     

       if(headNode != null){
             System.out.println("Head Node: " + headNode.getData());
             System.out.println("Tail Node: " + tailNode.getData());
       }

       while(node != null){
             System.out.println(node.getData().toString());
             node = node.getNextNode();
       }
     }   
}


public class TestLinkedList {
   public static void main(String [] args){
      SinglyLinkedList list = new SinglyLinkedList();
      
      list.addFirst(1);
      list.addFirst(2);
      list.addLast(3);
      list.addLast(4);
      list.addFirst(5);
  
      list.displayLinkedList();

      list.delete(5);
      list.delete(4);  

      list.displayLinkedList();

   }
}