Wednesday, November 5, 2014

BEST JAVA PROGRAM



Java Bubble Sort
public class BubbleSort
{
public static void main(String[] args)
{
int intArray[] = new int[]{5,90,35,45,150,3};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
}
bubbleSort(intArray);
System.out.println("");
System.out.println("Array After Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
  }
}
private static void bubbleSort(int[] intArray)
{
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(intArray[j-1] > intArray[j])
{
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
           }
        }
     }
   }
}
Java Bubble Sort Descending Order
public class BubbleSortDescendingOrder
{
public static void main(String[] args)
{
int intArray[] = new int[]{5,90,35,45,150,3};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
}
bubbleSort(intArray);  System.out.println("");
System.out.println("Array After Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
}
}
private static void bubbleSort(int[] intArray)
{
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(intArray[j-1] < intArray[j])
{
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
                 }
            }
        }
   
    }
}

Create New Thread Using Runnable


public class CreateThreadRunnableExample implements Runnable
{
public void run()
{
for(int i=0; i < 5; i++)
{
System.out.println("Child Thread : " + i);
try
{
Thread.sleep(50);
}
catch(InterruptedException ie)
{
System.out.println("Child thread interrupted! " + ie);
}
}
System.out.println("Child thread finished!");
}
public static void main(String[] args)
{
 Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread");
 t.start();
                 
for(int i=0; i < 5; i++)
{
System.out.println("Main thread : " + i);
                       
try
{
Thread.sleep(100);
}
catch(InterruptedException ie)
{
System.out.println("Child thread interrupted! " + ie);
  }
}
System.out.println("Main thread finished!");
  }
}

No comments:

Post a Comment