Skip to content

Instantly share code, notes, and snippets.

@sbsatter
sbsatter / BinarySearch.java
Created August 14, 2016 08:59
Binary Search in java. Add array as a series of arguments, reserving the last argument for the element to find.
import java.util.Arrays;
class BinarySearch{
public static void main(String ... args){
int [] array;
if(args.length!=0){
array= new int [args.length-1];
for(int i=0; i<args.length-1; i++){
array[i]=Integer.parseInt(args[i]);
import java.util.Scanner;
class SelectionSort{
public static void main(String ... args){
int [] unsorted;
if(args.length!=0){
unsorted= new int [args.length];
int i=0;
for(String s: args){
unsorted[i++]=Integer.parseInt(s);
}
class BubbleSort{
public static void main(String [] args){
int [] unsorted= {9,8,7,6,5,4,3,2,1};
if(args.length!=0){
unsorted= new int [args.length];
for(int i=0; i<args.length; i++){
unsorted[i]=Integer.parseInt(args[i]);
}
}
System.out.println("STARTING SORTING");
@sbsatter
sbsatter / RecursiveBinarySearch.java
Created August 14, 2016 15:33
Recursively search for a given element in an array using binary search. The element to find is passed as the last argument and the array is the preceding series of arguments from command prompt!
import java.util.Arrays;
class RecursiveBinarySearch{
static int elem;
static int passNumber=1;
public static void main(String ... args){
elem=Integer.parseInt(args[args.length-1]);
int [] array;
if(args.length!=0){
array= new int [args.length-1];
@sbsatter
sbsatter / Fibonacci.java
Created August 18, 2016 21:17
nth Fibonacci term and the sum till that term
import java.util.Scanner;
class Fibonacci{
public static void main(String ... args){
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number of terms you are interested in");
int n=sc.nextInt();
do{
System.out.println(n+"th term is "+fib(n));
System.out.println("And the sum is "+sum(n));
@sbsatter
sbsatter / IntegerBaseConversion.java
Created August 19, 2016 11:57
Convert an integer to any base
class IntegerBaseConversion{
static String numbers= "0123456789abcdef";
public static void main(String ... args){
if(args.length!=2){
int n= Integer.parseInt(args[0]);
int base= Integer.parseInt(args[1]);
System.out.println(convertBase(n, base));
}
}
@sbsatter
sbsatter / triangle.java
Created August 19, 2016 12:48
CODINGBAT RECURSION 1 PROBLEM- JAVA- TRIANGLE We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows. triangle(0) → 0 triangle(1) → 1 triangle(2)…
public int triangle(int rows) {
if(rows==0)
return 0;
if(rows==1){
return 1;
}
return triangle(rows-1)+rows;
}
public boolean array220(int[] nums, int index) {
if(index>=nums.length-1)
return false;
for(int i=index+1; i<nums.length; i++){
if((nums[index]*10)==nums[i]){
return true;
}
}
return array220(nums,index+1);
@sbsatter
sbsatter / PairStar.java
Created August 19, 2016 14:00
CodingBat: Recursion-1 > pairStar prev | next | chance Given a string, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*". pairStar("hello") → "hel*lo" pairStar("xxyy") → "x*xy*y" pairStar("aaaa") → "a*a*a*a"
class PairStar{
public static void main(String ... args){
System.out.println(pairStar(args[0]));
}
public static String pairStar(String str) {
System.out.println(str);
if(str.length()<=1){
return str;
}
int mid= str.length()/2;
@sbsatter
sbsatter / StrCount.java
Created August 19, 2016 14:48
Given a string and a non-empty substring sub, compute recursively the number of times that sub appears in the string, without the sub strings overlapping. strCount("catcowcat", "cat") → 2 strCount("catcowcat", "cow") → 1 strCount("catcowcat", "dog") → 0
public int strCount(String str, String sub) {
int index=str.indexOf(sub);
int l= sub.length();
if(index==-1){
return 0;
}
return 1+ strCount(str.substring(0,index)
+str.substring(index+l, str.length()), sub);
}