Skip to content

Commit

Permalink
Merge pull request #320 from Goheljay/issue#252
Browse files Browse the repository at this point in the history
Adding Binary Serch Alogrithams
  • Loading branch information
VaibhavPaliwal0007 authored Oct 19, 2021
2 parents 65ba4c7 + 7e1f544 commit 097ce10
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 0 deletions.
33 changes: 33 additions & 0 deletions DSA/Java/Searching Algoritham/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jay.practice;

public class BinarySearch {
//Simple Binary Serch Program Using you can finding the target Element
public static void main(String[] args) {
int arr[] = {2,4,6,8,9,11, 12, 14, 20,36,48}; // Sorted Array
int target = 12; // Target Element
System.out.println(SimpleBinary(arr, target));
}

//Created Function for finding target Element
static int SimpleBinary(int arr[], int tar){

// Required Start Posinter And Ending Pointer
int start =0;
int end = arr.length-1;

while(start <= end) { // check if Start and End Pointer is less or equal but not grater
int mid = (start+end)/2; // if lessthan End then finding the mid pointer of the given Array

//Usingthis if elese statment you can finding the target element graterthan or lessthan or equal to mid?
// If answer found than return the element position or not found then return the -1
if (arr[mid] > tar) {
end = mid-1;
}else if (arr[mid] < tar) {
start =mid+1;
}else {
return mid;
}
}
return -1;
}
}
95 changes: 95 additions & 0 deletions DSA/Java/Searching Algoritham/CirecularArrayBS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.jay.leetCode.medium;

public class CirecularArrayBS {
public static void main(String[] args) {
//Find the target element in Circular Array usng Binary Search

int[] arr= {4,5,6,7,0,1,2};// Sorted Circular Array
int tar = 6; // Target elemnt

//1. Find the Pivot Lelment
//2. if not found Pivot elemnet then find target using normal Binary Serch
//3. Let's Get Started

System.out.println(findPivot(arr));
System.out.println(serch(arr, tar));

}

// now time to check target element
static int serch(int arr[], int tar) {
int pivot = findPivot(arr); // first find then pivot elemnet

if (pivot == -1) { //if pivot element return -1 then finding the array in simple Binarry serch and return answer
System.out.println(SimpleBinary(arr, tar, 0, arr.length-1));
}

if(pivot == tar) { // if target element double equal to pivot element then return the pivot as answer
return pivot;
}

if(tar >= arr[0]) { // if target element grater than arr[0] then array serch in simple binary serch return the answer
return SimpleBinary(arr, tar, 0, pivot-1);
}

return SimpleBinary(arr, tar, pivot+1, arr.length-1);

}


// finding Pivot element "Pivot element menas Maximum value"
static int findPivot(int[] arr) {
int start = 0; // Required two pointer Start and End pointer
int end = arr.length-1;

while(start<=end) { // check if Start and End Pointer is less or equal but not grater
int mid = (start+end)/2; // if lessthan End then finding the mid pointer of the given Array


// Now check tke mid alway leassthan End && Arr[mid] graterthan Arr[mid+1] then return Mid poiter
// any one condistion is false then check the next conditon
if (mid < end && arr[mid] > arr[mid+1]) {
return mid;
}

// Now check tke mid alway graterthan End && Arr[mid] lessthan Arr[mid-1] then return Mid poiter
// any one condistion is false then check the next conditon
if (mid > start && arr[mid] < arr[mid-1]) {
return mid-1;
}


if(arr[mid] <= arr[start]) {
end = mid-1;
}else {
start = mid+1;
}
}

//if all condition flase then return -1 it menas pivot element not found
return -1;
}

// Finding Binary search
static int SimpleBinary(int arr[], int tar, int start, int end){
// simple binary serch required 4 arguments
//1. array
//2. Traget Ekement
//3. start point
//4. End point

while(start <= end) {
int mid = (start+end)/2;
if (arr[mid] > tar) {
end = mid-1;
}else if (arr[mid] < tar) {
start =mid+1;
}else {
return mid;
}
}
return -1;
}


}
33 changes: 33 additions & 0 deletions DSA/Java/Searching Algoritham/MoutainArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jay.practice;

public class MoutainArray {
//Peack the largets index value in given array using Binary Serch
public static void main(String[] args) {
int arr[]= {1,3,5,6,4,2,1};
System.out.println(peakIndexInMountaineArray(arr));

}
//Created Function for finding Largest Element
static int peakIndexInMountaineArray(int arr[]) {

// Required Start Posinter And Ending Pointer
int start =0;
int end = arr.length-1;

while(start <= end) { // check if Start and End Pointer is less or equal but not grater
int mid = (start+end)/2; // if lessthan End then finding the mid pointer of the given Array

//Usingthis if elese statment you can finding the Largest element graterthan or lessthan or equal to mid?
// If answer found than return the element position or not found then return the start value
if (arr[mid]> arr[mid+1]) {
// you in decreasing part
end = mid;
}else {
// you in Increasing part
start = mid+1;

}
}
return start; //or else you can return end because both are same
}
}
50 changes: 50 additions & 0 deletions DSA/Java/Searching Algoritham/orderAgnostic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.jay.practice;

public class orderAgnostic {
//Finding the Order of then array and ten after find Target Element
public static void main(String[] args) {

int arr[] = {12,11,9,8,7,6,5,4,3,2,1}; // Sorted Array
int target = 12;// Target Element
System.out.println(orderAgnosticBS(arr, target));

}

//Created Function for finding orer of array
static int orderAgnosticBS(int arr[], int tar) {
// Required Start Posinter And Ending Pointer
int start = 0;
int end = arr.length-1;

// check wether Asseding or descending
boolean isAsc;
if(arr[start] < arr[end]) {
isAsc = true;
}else {
isAsc = false;
}

while(start <= end) {
// find mid and target same or not
int mid = (start+end)/2;
if (arr[mid] == tar) {
return mid;
}
if(isAsc) {
if (arr[mid] > tar) {
end = mid-1;
}else {
start =mid+1;
}
}else {
if (arr[mid] < tar) {
end = mid-1;
}else {
start =mid+1;
}
}
}

return -1;
}
}

0 comments on commit 097ce10

Please sign in to comment.