Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Algorithms/Searching Algorithms/Binary_Search.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,40 @@ static int binarySearch(int[] arr, int target) { //Declaring the binary search f
return 0; //when target not found return 0
}
```


### Code `C++`
``` c++
#include<iostream>
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]<x){
return binarySearch(arr,left,mid-1,x);
}
}
return -1;
}
int main(void)
{
int arr[] = {1,3,5,7,8};
int x=5;
int n = sizeof(arr)/sizeof(arr[0]);
int result= binarySearch(arr,0,n-1,x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result+1;
return -1;
}
```

>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.

Expand Down
43 changes: 41 additions & 2 deletions Algorithms/Sorting Algorithms/Selection_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<stdio.h>
#include<stdlib.h>

Expand Down Expand Up @@ -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<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String args[])
{
selectionSort array = new selectionSort();
int arr[] = {64,25,12,22,11};
array.sort(arr);
System.out.println("Sorted array");
array.display(arr);
}

}
```

### Output: 3 5 7 9 10 11 12 13 16 24

Expand Down
58 changes: 58 additions & 0 deletions Data Structures/Searching/BinarySearch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## Binary Search

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.

## Works On
Divide and conquer rule

## Example
![binary](https://user-images.githubusercontent.com/66902249/135956846-8939b452-80e7-4969-8634-5292e2bfa2a2.png)

## Pseudocode
``` js
binarySearch(arr, size)
loop until left is not equal to right
midIndex = (left + right)/2
if (item == arr[midIndex] )
return midIndex
else if (item > arr[midIndex] )
left = midIndex + 1
else
right = midIndex - 1
```
## C++ Code
``` cpp
#include<iostream>
using namespace std;
int binarySearch(int arr[],int left, int right,int x){
if(right>=l)
{
int mid=left+(right-l)/2;
if(arr[mid]==x){
return mid;
}
if(arr[mid]>x){
return binarySearch(arr, mid+1,right,x);
}
if(arr[mid]<x){
return binarySearch(arr,left,mid-1,x);
}
}
return -1;
}
int main(void)
{
int arr[] = {1,3,5,7,8};
int x=5;
int n = sizeof(arr)/sizeof(arr[0]);
int result= binarySearch(arr,0,n-1,x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result+1;
return -1;
}
```
## Complexity
`Best Case - O(1)`
`Worst Case - O(log n)`
`Average Case - O(log n)`