Thursday, October 30, 2014

Java Double Array



Performing Binary Search on Java double Array

import java.util.Arrays;
public class BinarySearchDoubleArrayExample
{
public static void main(String[] args)
{
double doubleArray[] = {1.23,2.10,4.74,5.34}; Arrays.sort(doubleArray); double searchValue = 4.74;
int intResult = Arrays.binarySearch(doubleArray,searchValue);
System.out.println("Result of binary search of 4.74 is : " + intResult);
searchValue = 3.33;
intResult = Arrays.binarySearch(doubleArray,searchValue);
System.out.println("Result of binary search of 3.33 is : " + intResult);
  }
}

Performing Binary Search on Java float Array

import java.util.Arrays;
public class BinarySearchFloatArrayExample
{
public static void main(String[] args)
{
float floatArray[] = {1.23f,2.10f,4.74f,5.34f}; Arrays.sort(floatArray);  float searchValue = 4.74f;
int intResult = Arrays.binarySearch(floatArray,searchValue);
System.out.println("Result of binary search of 4.74 is : " + intResult);
searchValue = 3.33f;
intResult = Arrays.binarySearch(floatArray,searchValue);
System.out.println("Result of binary search of 3.33 is : " + intResult);
  }
}

Performing Binary Search on Java int Array

import java.util.Arrays;  
public class BinarySearchIntArrayExample
{
 public static void main(String[] args)
{
int intArray[] = {1,2,4,5}; Arrays.sort(intArray);int searchValue = 2;
int intResult = Arrays.binarySearch(intArray,searchValue);
System.out.println("Result of binary search of 2 is : " + intResult);
searchValue = 3;
intResult = Arrays.binarySearch(intArray,searchValue);
System.out.println("Result of binary search of 3 is : " + intResult);
  }
}

Performing Binary Search on Java long Array

import java.util.Arrays;
public class BinarySearchLongArrayExample
{
public static void main(String[] args)
{
        long longArray[] = {1,2,4,5};  Arrays.sort(longArray);  long searchValue = 2;
long intResult = Arrays.binarySearch(longArray,searchValue);
System.out.println("Result of binary search of 2 is : " + intResult);
searchValue = 3;
intResult = Arrays.binarySearch(longArray,searchValue);
System.out.println("Result of binary search of 3 is : " + intResult);
   }
}

Performing Binary Search on Java short Array

import java.util.Arrays;   
public class BinarySearchShortArrayExample
{
public static void main(String[] args)
{
short shortArray[] = {1,2,4,5};
Arrays.sort(shortArray);
short searchValue = 2;
int intResult = Arrays.binarySearch(shortArray,searchValue);
System.out.println("Result of binary search of 2 is : " + intResult);
searchValue = 3;
intResult = Arrays.binarySearch(shortArray,searchValue);
System.out.println("Result of binary search of 3 is : " + intResult);
   }
}

Java Sort byte Array

import java.util.Arrays;  
public class SortByteArrayExample
{
public static void main(String[] args)
{
byte[] b1 = new byte[]{3,2,5,4,1};
System.out.print("Original Array :\t ");
for(int index=0; index < b1.length ; index++)
System.out.print("  "  + b1[index]);
Arrays.sort(b1);
System.out.print("\nSorted byte array :\t ");
for(int index=0; index < b1.length ; index++)
System.out.print("  "  + b1[index]);
byte[] b2 = new byte[]{5,2,3,1,4};
Arrays.sort(b2,1,4);
System.out.print("\nPartially Sorted byte array :\t ");
for(int index=0; index < b2.length ; index++)
System.out.print("  "  + b2[index]);
    }
}

Java Sort char Array

import java.util.Arrays;
public class SortCharArrayExample
{
public static void main(String[] args)
{
char[] c1 = new char[]{'d','a','f','k','e'};
System.out.print("Original Array :\t ");
for(int index=0; index < c1.length ; index++)
System.out.print("  "  + c1[index]);
Arrays.sort(c1);
System.out.print("\nSorted char array :\t ");
for(int index=0; index < c1.length ; index++)
System.out.print("  "  + c1[index]);
char[] c2 = new char[]{'d','a','f','k','e'};
Arrays.sort(c2,1,4);
System.out.print("\nPartially Sorted char array :\t ");
for(int index=0; index < c2.length ; index++)
System.out.print("  "  + c2[index]);
    }
}

No comments:

Post a Comment