Friday, June 29, 2012

Selection Sort in Java



import java.util.Comparator;

public class SelectionSort {
      /**
       * This method will sort the int type array in ascending order
       * @param array
       */
      public static void selectionSort(int[] a) {
            int length = a.length - 1;
            for (int x = 0; x < length; x++) {
                  int min = x;
                  for (int y = x + 1; y <= length; y++) {
                        if (a[y] < a[min]) {
                              min = y;
                        }
                  }
                  int tmp = a[x];
                  a[x] = a[min];
                  a[min] = tmp;
            }
      }
      /**
       * This method will sorts the array of object in ascending order which implements Comparable.
       * like String, Byte, Short, Integer, Long, Float, Double, Character,
       * Boolean, BigInteger, BigDecimal
       * @param <T>
       * @param array
       */
      public static <T extends Comparable<T>> void selectionSort(T[] a) {
            int length = a.length - 1;
            for (int x = 0; x < length; x++) {
                  int min = x;
                  for (int y = x + 1; y <= length; y++) {
                        if (a[y].compareTo(a[min]) < 0) {
                              min = y;
                        }
                  }
                  T tmp = a[x];
                  a[x] = a[min];
                  a[min] = tmp;
            }
      }
      /**
       * This method will sorts the array of objects based on provided comparator in ascending order.
       * @param <T>
       * @param array
       */
      public static <T> void selectionSort(T[] a, Comparator<T> c) {
            int length = a.length - 1;
            for (int x = 0; x < length; x++) {
                  int min = x;
                  for (int y = x + 1; y <= length; y++) {
                        if (c.compare(a[y], a[min]) < 0) {
                              min = y;
                        }
                  }
                  T tmp = a[x];
                  a[x] = a[min];
                  a[min] = tmp;
            }
      }
}

Insertion Sort in Java


import java.util.Comparator;

public class InsertionSort {
     
      /**
       * This method will sort the int type array in ascending order
       * @param array
       */
      public static void insertionSort(int array[]) {
            int n = array.length;
            for (int i = 1; i < n; i++) {
                  int j = i;
                  int value = array[i];
                  while ((j > 0) && (array[j - 1] > value)) {
                        array[j] = array[j - 1];
                        j--;       
                  }
                  array[j] = value;
            }
      }
      /**
       * This method will sorts the array of object in ascending order which implements Comparable.
       * like String, Byte, Short, Integer, Long, Float, Double, Character,
       * Boolean, BigInteger, BigDecimal
       * @param <T>
       * @param array
       */
      public static <T extends Comparable<T>> void insertionSort(T[] array) {
            int n = array.length;
            for (int i = 1; i < n; i++) {
                  int j = i;
                  T value = array[i];
                  while ((j > 0) && (array[j-1].compareTo(value)>0)) {
                        array[j] = array[j - 1];
                        j--;       
                  }
                  array[j] = value;
            }
      }
     
      /**
       * This method will sorts the array of objects based on provided comparator in ascending order.
       * @param <T>
       * @param array
       */
      public static <T> void selectionSort(T[] array, Comparator<T> comparator) {
            int n = array.length;
            for (int i = 1; i < n; i++) {
                  int j = i;
                  T value = array[i];
                  while ((j > 0) && (comparator.compare(array[j-1], value)>0)) {
                        array[j] = array[j - 1];
                        j--;       
                  }
                  array[j] = value;
            }
      }
}

Thursday, June 28, 2012

Stack Example in Java


Stack is a collection to store data and follows the LIFO (Last In First Out) to remove the elements from it. Following is the implementation of Stack in java.

import java.util.Stack;

public class StackDemo {
      public static void main(String[] args) {       
            Stack<String> stack = new Stack<String>();
            for(int i=0; i<10; i++){
                  stack.push(String.valueOf((i*7)%10));
            }
            System.out.print("\nElements present in Stack (Last In First Out): ");
            for(String str: stack){
                  System.out.print(str + " ");
            }
            System.out.print("\nRemoving Elements from Stack (Last In First Out):");
            while(!stack.isEmpty()){
                  System.out.print(stack.pop() + " ");
            }
      }
}

Queue Example in Java


Queue is a collection to store the data and follows the FIFO (First In First Out) pattern to remove the elements from it. Following is the implementation of Queue in java.

import java.util.LinkedList;
import java.util.Queue;

public class QueueDemo {
      public static void main(String[] args) {
            Queue<String> queue = new LinkedList<String>();
            for(int i=0; i<10; i++){
                  queue.offer(String.valueOf((i*7)%10));
            }
            System.out.print("\nElements present in Queue (First In First Out): ");
            for(String str: queue){
                  System.out.print(str + " ");
            }
            System.out.print("\nRemoving Elements from Queue (First In First Out):");
            while(!queue.isEmpty()){
                  System.out.print(queue.poll() + " ");
            }
      }
}

Thursday, June 21, 2012

Magic Square



public class Magic {
      private int size = 3;

      public static void main(String[] args) {
            Magic magic = new Magic(1);
            magic.magic(1);
      }

      public Magic (int size) {
            setSize(2 * size + 1);
      }
     
      public void magic(int num) {
            int limit = getLimit();
            int current = getSize() / 2;

            int[] a = new int[limit];
            a[current] = num;
            int next = 0;

            for (int i = num + 1; i < num + limit; i++) {
                  next = next(current);
                  if (a[next] == 0) {
                        a[next] = i;
                  } else {
                        next = down(current);
                        a[next] = i;
                  }
                  current = next;
            }
            display(a);
      }
     
      public void display(int[] a){
            for(int i=0; i
                  if(i%getSize()==0){
                        System.out.println();
                  }
                  System.out.printf("%5d", a[i]);
            }
                 
      }

      public int next(int i) {
            return right(up(i));
      }

      public int up(int i) {
            i = i - getSize();
            if (i < 0) {
                  i += getLimit();
            }
            return i;
      }

      public int down(int i) {
            i = i + getSize();
            if (i > getLimit()) {
                  i -= getLimit();
            }
            return i;
      }

      public int right(int i) {
            i = i + 1;
            if (i % getSize() == 0) {
                  i = i - getSize();
            }
            return i;
      }

      public int left(int i) {
            if (i % getSize() == 0) {
                  i = i + getSize() - 1;
            } else {
                  i = i - 1;
            }
            return i;
      }

      public int getSize() {
            return size;
      }

      public void setSize(int size) {
            this.size = size;
      }

      public int getLimit() {
            return getSize() * getSize();
      }
}