Posts

Binary Search

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 import java.util.Scanner ; class HelloBinarySearch { public static void main ( String args []) { int c , first , last , middle , n , search , array []; Scanner in = new Scanner ( System . in ); System . out . println ( "Enter number of elements : " ); n = in . nextInt (); array = new int [ n ]; System . out . println ( "Enter " + n + " integers" ); for ( c = 0 ; c < n ; c ++) array [ c ] = in . nextInt (); System . out . println ( "Enter value to find : " ); search = in . nextInt (); first = 0 ; last = n - 1 ; middle = ( first + last )/ 2 ; while ( first <= last ) { if ( array [ middle ] < search ) first = middle + 1 ; else if (...

Linear Search

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.util.Scanner ; class HelloLinearSearch { public static void main ( String args []) { int c , n , search , array []; Scanner in = new Scanner ( System . in ); System . out . println ( "Enter number of Elements : " ); n = in . nextInt (); array = new int [ n ]; System . out . println ( "Enter " + n + " integers" ); for ( c = 0 ; c < n ; c ++) array [ c ] = in . nextInt (); System . out . println ( "Enter value to Search : " ); search = in . nextInt (); for ( c = 0 ; c < n ; c ++) { if ( array [ c ] == search ) /* Searching element is available */ { System . out . println ( search + " is present at location " + ( c + 1 ) + "." ); break ; } ...

Create Daemon Thread

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class HelloDaemonThread extends Thread { public HelloDaemonThread (){ setDaemon ( true ); } public void run (){ System . out . println ( "Is it a Daemon thread ? - " + isDaemon ()); } public static void main ( String a []){ HelloDaemonThread dt = new HelloDaemonThread (); dt . start (); } }

Create Thread using Thread class

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class HelloThread extends Thread { public static int myCount = 0 ; public void run (){ while ( HelloThread . myCount <= 10 ){ try { System . out . println ( "Thread: " +(++ HelloThread . myCount )); Thread . sleep ( 100 ); } catch ( InterruptedException iex ) { System . out . println ( "Exception in thread: " + iex . getMessage ()); } } } } public class MainClass { public static void main ( String a []){ HelloThread mst = new HelloThread (); mst . start (); while ( HelloThread . myCount <= 10 ){ try { System . out . println ( "Main Thread: " +(++ HelloThread . myCount )); Thread . sleep ( 100 ); } catch ( Interrupt...

Thread using Runnable Interface

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 class RunnableThread implements Runnable { public static int myCount = 0 ; public RunnableThread (){ } public void run () { while ( RunnableThread . myCount <= 10 ){ try { System . out . println ( " Thread: " +(++ RunnableThread . myCount )); Thread . sleep ( 100 ); } catch ( InterruptedException iex ) { System . out . println ( "Exception in thread: " + iex . getMessage ()); } } } } public class RunnableExample { public static void main ( String a []){ System . out . println ( "Main Thread Started" ); RunnableThread mrt = new RunnableThread (); Thread t = new Thread ( mrt ); t . start (); while ( RunnableThread . myCount <=...

Create Basic Annotation

1 2 3 4 5 6 7 8 9 10 11 12 13 public @interface SampleAnnotation { String name (); String desc (); } class MyAnnotation { @MySampleAnn ( name = "sparrow" , desc = "It is a bird" ) public void myTestMethod (){ //method implementation } }

Longest Sub-String without repeating characters

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 import java.util.HashSet ; import java.util.Set ; public class MyLongestSubstr { private Set < String > subStrList = new HashSet < String >(); private int finalSubStrSize = 0 ; public Set < String > getLongestSubstr ( String input ) { // reset instance variables subStrList . clear (); finalSubStrSize = 0 ; // have a boolean flag on each character ascii value boolean [] flag = new boolean [ 256 ]; int j = 0 ; char [] inputCharArr = input . toCharArray (); for ( int i = 0 ; i < inputCharArr . length ; i ++) { char c = inputCharArr [ i ]; if ( flag [ c ]) { extractSubString ( inputCharArr , j , i ); for ( int k = j ; k < i ; k ++) { if ( inputCharArr [ k ] == c ) { j =...