Monday, October 27, 2014

Java Bubble Sort Descending Ordera

Jva Bubble Sort Descending Ordera

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!");
  }
}
Get Current Thread
public class GetCurrentThreadExample
{
 public static void main(String[] args)
{
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
   }
}
Pause Thread Using Sleep Method
public class PauseThreadUsingSleep
{
public static void main(String[] args)
{
System.out.println("Print number after pausing for 1000 milliseconds");
try
{
 for(int i=0; i< 5; i++)
{
 System.out.println(i);
 Thread.sleep(1000);
   }
}
catch(InterruptedException ie)
{
System.out.println("Thread interrupted !" + ie);
                           }
    }
}
Set Thread Name
public class SetThreadNameExample
{
public static void main(String[] args)
{
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
currentThread.setName("Set Thread Name Example");
System.out.println("Thread Name : "+ currentThread.getName());
   }
}
Java continue statement
public class JavaContinueExample
{
public static void main(String[] args)
{
int intArray[] = new int[]{1,2,3,4,5};
System.out.println("All numbers except for 3 are :");
for(int i=0; i < intArray.length; i++)
{
if(intArray[i] == 3)
continue;
else
System.out.println(intArray[i]);
    }
  }
}



Java continue statement with label
public class JavaContinueWithLabelExample
{
public static void main(String[] args)
{
int intArray[][] = new int[][]{{1,2},{2,3}};
for(int i=0; i < intArray.length; i++)
{
for(int j=0; j < intArray[i].length ; j++)
{
if(intArray[i][j] == 3)
continue Outer;
System.out.println("Element is : " + intArray[i][j]);
          }
        }
     }

}

No comments:

Post a Comment