Posts

Showing posts from August, 2015

Binary Search in java

Time Complexity for Binary Search will be Log(n).  But the main Condition is, the inputted elements in the array must be sorted (ascending order). import java.util.*; class BSearch {   public static void main(String[] args)   {   Scanner s=new Scanner(System.in);   int n=s.nextInt();   int a[]=new int[n];     for(int i=0;i<n;i++)  a[i]=s.nextInt();     System.out.println("enter the element to be find");   int f=s.nextInt();     int k=bs(a,0,n-1,f);   if(k==-1)  System.out.println("the element not present");   else       System.out.println("the element is at index  "+(k+1));   }       static int bs(int a[],int st,int en,int f)   {  if(en>=st)  {   int mid=(st+en)/2;   if(a[mid]==f)  return mid;   else if(a[mid]<f)  return bs(a,mid+1,en,f);   else ...

Single Link List in java

import java.util.*; class Intnode {   public int data;   public Intnode next;     Intnode()   {   this.next=null;   } } public class Linklist {   public static void main(String[] args)   {    Scanner s = new Scanner(System.in);    Intnode start= new Intnode();    Intnode temp=start;       System.out.println("Please enter data in the node");     int k=s.nextInt(); start.data=k; while(true) {  System.out.println("do u want to make more such node ? press y/n");   char a= s.next().charAt(0);     if(a=='n')    break;   Intnode temp2= new Intnode();   System.out.println("Please enter data in the node");        k=s.nextInt();   temp2.data=k;   temp.next=temp2; temp=temp.next; } temp =start; while(temp!=null) {  System.out.print(temp.d...

Quick Sort in java

import java.util.*; class qsort {  public static void main(String args[])  { Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int [n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); quick_sort(a,0,n-1); for(int i=0;i<n;i++) System.out.print(a[i]+" ");  }   static void quick_sort(int a[],int st,int en)   {  if(st<en)  {        int k=pivot_ele(a,st,en);   quick_sort(a,st,k);   quick_sort(a,k+1,en);  }   }   static int pivot_ele(int a[],int st,int en)   {  int i,j,pivot; pivot=st; i=st+1; j=en; while(i<=j) { while(a[i]<a[pivot] && i<en) i++; while(a[j]>a[pivot]) j--; if(i<=j) { int t=a[i]; a[i]=a[j]; a[j]=t; i++; j--; } }  ///while close  int t=a[j]; a[j]=a[st]; a[...

MERGE_SORT in java

import java.util.*; class msort {  public static void main(String args[])  { Scanner s=new Scanner(System.in); int n=s.nextInt();         int a[]=new int [n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); merge_sort(a,0,n-1); for(int i=0;i<n;i++) System.out.print(a[i]+" ");  }   static void merge_sort(int a[],int st,int en)   {  if(st<en)  {        int k=(st+en)/2;   merge_sort(a,st,k);   merge_sort(a,k+1,en);   merge(a,st,k,en);  }   }       static void merge(int array[],int lowerIndex,int middle,int higherIndex) { int tempMergArr[]=new int[higherIndex-lowerIndex+1]; int k=0;        for (int i = lowerIndex; i <= higherIndex; i++)             tempMergArr[k++] = array[i];         int i = lowerIndex; int r=i...

Similarity between Merge_sort and Quick_sort

Image
Similarity between Merge_sort  and  Quick_sort In quick sort , the main thing is to choose the PIVOT  element. In Merge sort , the main thing is merging part.

java program for finding gcd ,lcm ,xor and addition (without using operators )

import java.util.*; class LCM {  public static void main(String args[])  {    Scanner s=new Scanner(System.in);    int k1 = s.nextInt();    int k2= s.nextInt();      int gcd=hcf(k1,k2);    System.out.println(" hcf of these two number is "+gcd);      int lcm=(k1*k2)/gcd;    System.out.println(" lcm of these two number is "+lcm);      int xor=k1^k2;    System.out.println(" xor of these two number is " +xor);      int addd=Add(k1,k2);    System.out.println("addition method without using arthemetic operators is "+addd);      }  static int hcf(int a,int b)  { int r=a%(b); int diviser=a; int divident=b; int quotient; while(r>0) {   quotient=divident/diviser; r=divident%diviser; divident=diviser; diviser=r; } return divident;  }  /*  Sum ...

3 ways to take inputs in java (Scanner , console , BufferedReader )

import java.util.*; import java.io.Console; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class Input {  public static void main(String []args) throws IOException  {   Scanner a = new Scanner(System.in); // creating object of scanner class   String s=a.nextLine();              // way to take input through Scanner(nextLine for String)   System.out.println(s);   int i=a.nextInt();                  // way to take input through Scanner(nextInt for int)   System.out.println(i);   String str=System.console().readLine();  // way to take input from console   System.out.println(str);   BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); // creating object of BufferedReader   String str1 = in.readLine();         System.out.println(str1); ...

A sample Python Program for Reference work ( Life Universe Everything )

def main ( ) : while True: w= input ( ) if w== 42 : break else : print w main ( ) -----------------------------------------------------------------------------------------============================================================================================= I. Yet another A + B codeforces def main():             a=int(input())             b=int(input())             c=int(input())                         if a+b == c:                 print("YES")             elif a+a == c:                 print("YES")             elif b+b == c: ...

A Sample PHP program for reference work( taking input ,creating arrays )

<?php fscanf(STDIN, "%d\n", $test); while($test--) { $ooo=array(); for($i=0;$i<26;$i++) $ooo[$i]=0; fscanf(STDIN, "%s\n", $raa); $qwe=0; $p=strlen($raa); for($i=0;$i<$p;$i++) { $w=ord($raa[$i]); $qwe= $w-97; $ooo[$qwe]=$ooo[$qwe]+1; } $f=0; $c=0; for($i=0;$i<26;$i++) { if($ooo[$i]%2!=0) { $f=1; $c++; } } if($f==0) { echo"Yes"; } else if($f==1 && strlen($raa)%2==1 && $c==1) { echo "Yes"; } else { echo "No"; } echo "<br>"; }