sahin
DESCRIPTION
sadsadadaTRANSCRIPT
12. Binary Searchimport java.util.Arrays;
public class BinarySearch {
private BinarySearch() { }
public static int rank(int key, int[] a) {
int lo = 0;
int hi = a.length - 1;
while (lo a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
public static void main(String[] args) {
In in = new In(args[0]);
int[] whitelist = in.readAllInts();
Arrays.sort(whitelist);
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (rank(key, whitelist) == -1)
StdOut.println(key);
}
}
}
1. Quadratic Equation
import java.util.Scanner;
public class Quadratic
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double a = 0;
double b = 0;
double c = 0;
double discriminant = 0;
double d = 0;
System.out.print("Enter the value of a : ");
a = input.nextDouble();
System.out.print("Enter the value of b : ");
b = input.nextDouble();
System.out.print("Enter the value of c : ");
c = input.nextDouble();
discriminant = (b * b - 4 * a * c);
d = Math.sqrt(discriminant);
if (discriminant >= 0.0) {
System.out.println("First root of the equation : "
+(-b + d) / (2.0*a));
System.out.println("Second root of the equation : "
+(-b - d) / (2.0*a));
}
else
{
System.out.println("The roots are not real numbers.");
}
}
}2. Print the element of an Arraypublic class List
{
public static void main(String args[])
{
int Data[]={10,20,30,40,50};
System.out,println(Data[0]= + Data[0]);
System.out,println(Data[1]= + Data[1]);System.out,println(Data[2]= + Data[2]);System.out,println(Data[3]= + Data[3]);System.out,println(Data[4]= + Data[4]);
}
}
3. Find the Cumulative Sum of the element of Array
public class Sum {
public static void main(String[] args) {
int in[] = {1,2,3,4,5,6,7,8,9};
int[] out = new int[in.length];
out[0] = in[0];
for (int i = 1; i < out.length; i++)
out[i] = out[i-1] + in[i];
for (int i = 0; i < out.length; i++)
System.out.print(out[i]+" ");
}
}
Output: 1 3 6 10 15 21 28 36 45 4. Find the Largest Number of an Array public class ArrayLargestValue{ static int i, j, res, max, temp=0,a[]; public static void main(String args[]) { res=ArrayLargestValue.max(new int []{173,29,391,41}); System.out.println ("Largest value in the given array is : "+ res "); } static int max(int a[]) { max=0; for(i=0;i=a[max]) max=i; } return (a[max]); }}5. Find the second largest number of an Arrayimport java.io.*public class SecondLargestValue{
public static void main(String args[]){
int arr[] = {277,3,444,78};
Arrays.sort(arr);
System.out.println("second largest: " + arr[arr.length-2]);
}
}
6. Find the odd and even separately of an array import java.util.Scanner;public class Even_Odd
{public static void main(String[] args)
{int n;Scanner s = new Scanner(System.in);System.out.print("Enter no. of elements you want in array:");n = s.nextInt();int a[] = new int[n];System.out.println("Enter all the elements:");for (int i = 0; i < n; i++)
{a[i] = s.nextInt();}System.out.print("Odd numbers:");for(int i = 0 ; i < n ; i++){if(a[i] % 2 != 0){System.out.print(a[i]+" ");} } System.out.println(""); System.out.print("Even numbers:"); for(int i = 0 ; i < n ; i++) { if(a[i] % 2 == 0) { System.out.print(a[i]+" "); } } }}10. Bubble Sort
public class Bubblesort{
private static int []a;
public static void main(String[] args) {
a = getArray();
prints the array
printArray();
sorts the array
System.out.println();
printArray();
}
public static void sort(){
int left = 0;
int right = a.length-1;
bubbleSort(left,right);
}
private static void bubbleSort(int left,int right){
for(int i=right;i>1;i--){
for(int j=left;j a[j+1]){
swap(j, j+1);
}
}
}
}
public static void swap(int left,int right){
int temp = a[left];
a[left] = a[right];
a[right] = temp;
public static void printArray(){
for(int i : a){
System.out.print(i+" ");
}
}
public static int[] getArray(){
int size=10;
int []array = new int[size];
int item = 0;
for(int i=0;i