Saturday, May 10, 2014

Sample Programs for Java Interviews-Series 1

Bubble Sort:

Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.

Sample Program:


package sortings.BubbleSort;  
 public class BubbleSortExample {  
      public static void main(String a[]) {  
           int i;  
           int array[] = { 12, 9, 4, 99, 120, 1, 3, 10 };  
           bubble_srt(array, array.length);  
           System.out.print("Values after sorting: ");  
           for (i = 0; i < array.length; i++)  
                System.out.print(array[i] + " ");  
      }  
      public static void bubble_srt(int a[], int n) {  
           int i, j, t = 0;  
           for (i = 0; i < n; i++) {  
                for (j = 1; j < (n - i); j++) {  
                     if (a[j - 1] > a[j]) {  
                          t = a[j - 1];  
                          a[j - 1] = a[j];  
                          a[j] = t;  
                     }  
                }  
           }  
      }  
 }  


Output:

Values after sorting: 1  3  4  9  10  12  99  120


Fibonacci Series:

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. If the Fibonacci sequence is denoted F ( n ), where n is the first term in the sequence, the following equation obtains for n = 0, where the first two terms are defined as 0 and 1 by convention


F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

Sample Program:


package ramesh.programming;  
 public class FibonocciExample {  
      /**  
       * @param args  
       */  
      public static void main(String[] args) {  
           int n = 15;  
           int f1, f2 = 0, f3 = 1;  
           System.out.println(f2);  
           for (int i = 1; i <= n; i++) {  
                System.out.print(" "+f3);  
                f1 = f2;  
                f2 = f3;  
                f3 = f1 + f2;  
           }  
      }  
 }  

Output:  1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

Fibonacci using recursive method:


package ramesh.programming;  
 public class FibonocciRecursive {  
      /**  
       * @param args  
       */  
      public static long fib(long num) {  
           if (num <= 1) {  
                return num;  
           }  
           return fib(num - 1) + fib(num - 2);  
      }  
      public static void main(String[] args) {  
           long n = 15;  
           for (int i = 1; i <= n; i++) {  
                System.out.print(fib(i)+"");  
           }  
      }  
 }  

Output: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 

Reverse a Number:

This is a sample program to reverse a given integer number



package ramesh.misc;  
 /**  
  * @author RameshM  
  * A sample program for reversing a given integer number  
  */  
 public class ReverseIntegerNumber {  
      public int reverseNumber(int number) {  
           int reverse = 0;  
           while (number != 0) {  
                reverse = (reverse * 10);  
                reverse = reverse + (number % 10);  
                number = number / 10;  
           }  
           return reverse;  
      }  
      public static void main(String a[]) {  
           ReverseIntegerNumber reverse = new ReverseIntegerNumber();  
           // Not handling the cases where the number is less than or equal to zero  
           // for brevity purposes.  
           System.out.println("Number after reversing: "  
                     + reverse.reverseNumber(14789));  
      }  
 }  


Output:

98741


Thanks for visiting my blog!!!!!!!!!