Java Programming Examples

Java Programming Examples

Armstrong Number:



import java.util.Arrays;
import java.util.Random;
public class QuickSortTest {
/**
 * @param args
 */
static int[] arr = new int[20];
public static void main(String[] args) {
// TODO Auto-generated method stub
Random gen = new Random();
for(int i=0; i<arr.length ;i++){
arr[i] = gen.nextInt(20);
System.out.println(arr[i]);
}
sort(0,arr.length-1);
System.out.println("Sorted" + Arrays.toString(arr));
}
private static void sort(int low, int high) {
// TODO Auto-generated method stub
int i = low;int j = high;
int pivot = arr[low + (high-low)/2];
while(i<=j){
while(arr[i]<pivot){
i++;
}
while(arr[j]>pivot){
j--;
}
if(i<=j){
exchange(i,j);
i++;
j--;
}
}
if(low<j){
sort(low,j);
}
if(i<high){
sort(i,high);
}
}
private static void exchange(int i, int j) {
// TODO Auto-generated method stub
int temp = arr[i];
arr[i]=arr[j];
arr[j]= temp;
        
}
}








public class Factorial {
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("factorial of 5 = " + fact(5) );
}
public static int fact(int num){
if(num ==0 || num==1){
return num;
}
else{
return num*fact(num-1);
}
}
}








public class Fib {
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 8;
for(int i =0;i<num;i++)
System.out.println(fibo(i));
}
public static int fibo(int n){
if(n==0|n==1) return n;
else
return fibo(n-1)+fibo(n-2);
}
}






import java.util.Random;


public class QuickSort {

/**
* @param args
*/
static int arr[] = new int[10];
public static void main(String[] args) {
// TODO Auto-generated method stub
Random generator = new Random();

for(int i =0;i<10;i++){
arr[i] = generator.nextInt(20);
System.out.println(arr[i]);
}

sort(0,9);

System.out.println("Sorted Array");
for(int i =0;i<10;i++){

System.out.println(arr[i]);
}
}

public static void sort(int low , int high){
int i = low;int j =high;
int pivot = arr[low+(high-low)/2];

while(i<=j){
while(arr[i]<pivot){
i++;
}
while(arr[j]>pivot){
j--;
}
if(i<=j){
exchange(i,j);
i++;
j--;
}
}
if(low<j){
sort(low,j);

}
if(i<high){
sort(i,high);
}



}

private static void exchange(int i, int j) {
// TODO Auto-generated method stub
int temp = arr[i];
arr[i] =arr[j];
arr[j]=temp;
}

}



import java.util.Arrays;


public class BucketSort {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub


int[] arr = {4,6,1,8,11,23};
int[] tmparr = new int[24] ;
System.out.println("Unsorted List");
for (int i=0;i<arr.length;i++){
System.out.println(arr[i]);
tmparr[arr[i]] =1;
}
System.out.println("Sorted List");
for (int i=0;i<tmparr.length;i++){
if(tmparr[i]==1){
System.out.println(i);
}

}

bucketSort(arr);

for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}

}

public static int[] bucketSort(int[] arr) {
int i, j;
int[] count = new int[arr.length];
Arrays.fill(count, 0);

for (i = 0; i < arr.length; i++) {
count[arr[i]]++;
}

for (i = 0, j = 0; i < count.length; i++) {
for (; count[i] > 0; (count[i])--) {
arr[j++] = i;
}
}
return arr;
}
}




public class ReverseNumber {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int num = 78539;
int revNum = 0;
while(num!=0){
revNum = revNum*10 + num%10;
num = num/10;
}
System.out.println(revNum);
}

}




public class ReverseString {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String s = "WORLD";
System.out.println(reverse(s));
}

public static String reverse(String str){
if(str.length()==1)
return str;
else
return reverse(str.substring(1)) + str.charAt(0);
}
}





public class Permutation {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String str = "waffle";
permute("", str);

}

public static void permute (String prefix , String str)
{
int n = str.length();
if(n==0){
System.out.println(prefix);
}else{
for (int i =0 ;i<n;i++){
permute(prefix+str.charAt(i),str.substring(0,i)+str.substring(i+1,str.length()));
}

}
}

}



BinarySearch



public class BinarySearch
{
public static void main(String[]args)
{
System.out.println("d");
int[]data={1,1,2,3,1,1,1};
System.out.println(count(data,1));
}

public static int count(int[] a, int x)
{
int low=0;
int high = a.length-1;
int count=0;

while(low <=high)
{
int mid=((low+high)/2);         
if(x>a[mid])
low=mid+1;
if(x<a[mid])
high=mid-1;

if(x==a[mid])            
{
System.out.println(mid);
int top=findTopIndex(a,x,mid);                
int bottom=findBottomIndex(a,x,mid);
return (top-bottom);

}

}    
return 111111111;

}

public static int findTopIndex(int[] a, int x, int index)
{
int low=index;
int high = a.length-1;    
int mid;
if(x==a[high])
return high;

while(low <= high)
{            
mid=((low+high)/2);         
if(x<a[mid]&&x==a[mid-1])
return mid-1;
else if(x==a[mid])
low=mid+1;
else if(a[mid]>x && a[mid-1]!=x)
high=mid-1;


return 11111111;

}
public static int findBottomIndex(int[] a, int x, int index)
{
int low=0;
int high = index-1;    
int mid;
if(x==a[low])
return low-1;

while(low <= high)
{            
mid=((low+high)/2);         
if(x>a[mid]&&x==a[mid+1])
return mid;
else if(x==a[mid])
high=mid-1;
else if(a[mid]<x && a[mid+1]!=x)
low=mid+1;

return 111;

}

}

No comments:

Post a Comment