From 0414df53641c2c73f0f03f2466b00c17c6d3fc89 Mon Sep 17 00:00:00 2001 From: akshikamudgal <66902249+akshikamudgal@users.noreply.github.com> Date: Fri, 1 Oct 2021 21:23:56 +0530 Subject: [PATCH 1/6] Create BinarySearch.cpp --- Data Structures/Searching/BinarySearch.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Data Structures/Searching/BinarySearch.cpp diff --git a/Data Structures/Searching/BinarySearch.cpp b/Data Structures/Searching/BinarySearch.cpp new file mode 100644 index 0000000..c474e09 --- /dev/null +++ b/Data Structures/Searching/BinarySearch.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; +int binarySearch(int arr[],int l, int r,int x){ + if(r>=l) + { + int mid=l+(r-l)/2; + if(arr[mid]==x){ + return mid; + } + if(arr[mid]>x){ + return binarySearch(arr, mid+1,r,x); + } + if(arr[mid] Date: Tue, 5 Oct 2021 09:31:37 +0530 Subject: [PATCH 2/6] Update and rename BinarySearch.cpp to BinarySearch.md --- Data Structures/Searching/BinarySearch.cpp | 28 ---------- Data Structures/Searching/BinarySearch.md | 59 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 28 deletions(-) delete mode 100644 Data Structures/Searching/BinarySearch.cpp create mode 100644 Data Structures/Searching/BinarySearch.md diff --git a/Data Structures/Searching/BinarySearch.cpp b/Data Structures/Searching/BinarySearch.cpp deleted file mode 100644 index c474e09..0000000 --- a/Data Structures/Searching/BinarySearch.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include -using namespace std; -int binarySearch(int arr[],int l, int r,int x){ - if(r>=l) - { - int mid=l+(r-l)/2; - if(arr[mid]==x){ - return mid; - } - if(arr[mid]>x){ - return binarySearch(arr, mid+1,r,x); - } - if(arr[mid] arr[midIndex] ) + beg = midIndex + 1 + else + end = midIndex - 1 +``` +## C++ Code +``` cpp +#include +using namespace std; +int binarySearch(int arr[],int l, int r,int x){ + if(r>=l) + { + int mid=l+(r-l)/2; + if(arr[mid]==x){ + return mid; + } + if(arr[mid]>x){ + return binarySearch(arr, mid+1,r,x); + } + if(arr[mid] Date: Wed, 6 Oct 2021 21:49:34 +0530 Subject: [PATCH 3/6] Update BinarySearch.md --- Data Structures/Searching/BinarySearch.md | 25 +++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Data Structures/Searching/BinarySearch.md b/Data Structures/Searching/BinarySearch.md index be79b87..a98f6c1 100644 --- a/Data Structures/Searching/BinarySearch.md +++ b/Data Structures/Searching/BinarySearch.md @@ -1,10 +1,9 @@ ## Binary Search -Binary Serach is a seearching algorithm that finds the position of a target value within a sorted array.Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the array. +Binary Search is a searching algorithm that is used in sorted arrays to find the find a specific element's position . In binary search first we find the middle element of the array and then check whether the element is equal to middle element or not if equal return the position of middle element if not than check whether is greater than or less than the moddle element if less than then the new array is from starting element to the (middle element-1) element position and if greater than then new array is from (middle element+1) element position to the last element and this process is followed until we got the required result and if not found return not present is the array. -## Algorithm - -Binary search works on sorted arrays. Binary search begins by comparing an element in the middle of the array with the target value. If the target value matches the element, its position in the array is returned. If the target value is less than the element, the search continues in the lower half of the array. If the target value is greater than the element, the search continues in the upper half of the array. By doing this, the algorithm eliminates the half in which the target value cannot lie in each iteration +## Works On +Divide and conquer rule ## Example ![binary](https://user-images.githubusercontent.com/66902249/135956846-8939b452-80e7-4969-8634-5292e2bfa2a2.png) @@ -12,31 +11,31 @@ Binary search works on sorted arrays. Binary search begins by comparing an eleme ## Pseudocode ``` js binarySearch(arr, size) -loop until beg is not equal to end - midIndex = (beg + end)/2 +loop until left is not equal to right + midIndex = (left + right)/2 if (item == arr[midIndex] ) return midIndex else if (item > arr[midIndex] ) - beg = midIndex + 1 + left = midIndex + 1 else - end = midIndex - 1 + right = midIndex - 1 ``` ## C++ Code ``` cpp #include using namespace std; -int binarySearch(int arr[],int l, int r,int x){ - if(r>=l) +int binarySearch(int arr[],int left, int right,int x){ + if(right>=l) { - int mid=l+(r-l)/2; + int mid=left+(right-l)/2; if(arr[mid]==x){ return mid; } if(arr[mid]>x){ - return binarySearch(arr, mid+1,r,x); + return binarySearch(arr, mid+1,right,x); } if(arr[mid] Date: Mon, 11 Oct 2021 22:35:50 +0530 Subject: [PATCH 4/6] Update Selection_sort.md --- .../Sorting Algorithms/Selection_sort.md | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/Algorithms/Sorting Algorithms/Selection_sort.md b/Algorithms/Sorting Algorithms/Selection_sort.md index a1c04dd..44f4119 100644 --- a/Algorithms/Sorting Algorithms/Selection_sort.md +++ b/Algorithms/Sorting Algorithms/Selection_sort.md @@ -44,8 +44,8 @@ Step 5 - Repeat until array is sorted. Here we can observe that for 6 elements we need 5 pass(iterations) so, for n elements (n-1) passes are required. -## Code: - +## C++ Code: +``` cpp #include #include @@ -84,6 +84,45 @@ Here we can observe that for 6 elements we need 5 pass(iterations) so, for n ele printf("\n"); return 0; } + ``` + + ## Java Code: + ```java +public class selectionSort { +void sort(int arr[]) +{ + int n = arr.length; + for (int i = 0; i < n-1; i++) + { + int min= i; + for (int j = i+1; j < n; j++) + if (arr[j] < arr[min]) + min= j; + + int temp = arr[min]; + arr[min] = arr[i]; + arr[i] = temp; + } +} + +void display(int arr[]) +{ + int n = arr.length; + for (int i=0; i Date: Mon, 11 Oct 2021 22:53:50 +0530 Subject: [PATCH 5/6] Update Binary_Search.md --- .../Searching Algorithms/Binary_Search.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/Algorithms/Searching Algorithms/Binary_Search.md b/Algorithms/Searching Algorithms/Binary_Search.md index 334e088..f5e39ba 100644 --- a/Algorithms/Searching Algorithms/Binary_Search.md +++ b/Algorithms/Searching Algorithms/Binary_Search.md @@ -100,6 +100,37 @@ static int binarySearch(int[] arr, int target) { //Declaring the binary search f return 0; //when target not found return 0 } ``` +``` c++ +#include +using namespace std; +int binarySearch(int arr[],int left, int right,int x){ + if(r>=l) + { + int mid=left+(right-left)/2; + if(arr[mid]==x){ + return mid; + } + if(arr[mid]>x){ + return binarySearch(arr, mid+1,right,x); + } + if(arr[mid]Note: The while loop runs until the value of start index is less than equal to the end index. When start=end at that time mid=start=end thus arr[mid] >is the target. After that start>end which breaks the while loop. From 9d5f772f903c965311fdb315118961ee4808242d Mon Sep 17 00:00:00 2001 From: akshikamudgal <66902249+akshikamudgal@users.noreply.github.com> Date: Mon, 11 Oct 2021 22:54:31 +0530 Subject: [PATCH 6/6] Update Binary_Search.md --- Algorithms/Searching Algorithms/Binary_Search.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Algorithms/Searching Algorithms/Binary_Search.md b/Algorithms/Searching Algorithms/Binary_Search.md index f5e39ba..072e7ea 100644 --- a/Algorithms/Searching Algorithms/Binary_Search.md +++ b/Algorithms/Searching Algorithms/Binary_Search.md @@ -100,6 +100,9 @@ static int binarySearch(int[] arr, int target) { //Declaring the binary search f return 0; //when target not found return 0 } ``` + + +### Code `C++` ``` c++ #include using namespace std;