Skip to content

Commit

Permalink
binary_search.c: Add BinarySearch Algorithm
Browse files Browse the repository at this point in the history
This adds Binary Search Algorithm which finds the position of a target value within a sorted array. It compares the target value to the middle element of the array.

Closes http://github.com/NITSkmOS/Algorithms/issues/44

binary_search.c: Add Binary Search Algorithm

This adds Binary Search Algorithm which finds the position of a target value within a sorted array. It compares the target value to the middle element of the array

Closes http://github.com/NITSkmOS/Algorithms/issues/44

binary_search.c: Add Binary Search Algorithm

This adds Binary Search Algorithm which finds the position of a target value within a sorted array. It compares the target value to the middle element of the array

Closes http://github.com/NITSkmOS/Algorithms/issues/44

binary_search.c: Add Binary Search Algorithm

This adds Binary Search Algorithm which finds the position of a target value within a sorted array. It compares the target value to the middle element of the array.

Closes NITSkmOS#44

Update README.md

Co-authored-by: Sangam Kumar <[email protected]>

binary_search.c: Add BinarySearch Algorithm

This adds Binary Search Algorithm which finds the position of a target value within a sorted array. It compares the target value to the middle element of the array

Closes NITSkmOS#44
  • Loading branch information
varunkmr19 committed Oct 19, 2020
1 parent 967f5a1 commit ff6b741
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
63 changes: 32 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@ This repository contains examples of various algorithms written on different pro

## Implemented Algorithms

| Algorithm | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | [:octocat:](merge_sort/Cpp) | [:octocat:](merge_sort/Java) | [:octocat:](merge_sort/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | [:octocat:](quicksort/Java) | [:octocat:](quicksort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | [:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:octocat:](radix_sort/Cpp) | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | [:octocat:](binary_search/Cpp) | | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | [:octocat:](bubble_sort/Java) | [:octocat:](bubble_sort/Python) |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) |
| [Maximum Subarray Problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | | | | [:octocat:](/maximum_subarray/Python) |
| [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python) |
| [Selecton Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | [:octocat:](selection_sort/Cpp) | | |
| Algorithm | C | CPP | Java | Python |
| :--------------------------------------------------------------------------------- | :---------------------------: | :-----------------------------: | :-----------------------------: | :-----------------------------------: |
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) |
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | [:octocat:](merge_sort/Cpp) | [:octocat:](merge_sort/Java) | [:octocat:](merge_sort/Python) |
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | [:octocat:](quicksort/Java) | [:octocat:](quicksort/Python) |
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | [:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:octocat:](radix_sort/Cpp) | | [:octocat:](radix_sort/Python) |
| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | [:octocat:](binary_search/C) | [:octocat:](binary_search/Cpp) | [:octocat:](binary_search/C) | [:octocat:](binary_search/Python) |
| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | [:octocat:](bubble_sort/C) | [:octocat:](bubble_sort/Cpp) | [:octocat:](bubble_sort/Java) | [:octocat:](bubble_sort/Python) |
| [Shell Sort](https://en.wikipedia.org/wiki/Shellsort) | [:octocat:](shell_sort/C) | | | [:octocat:](shell_sort/Python) |
| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | | | [:octocat:](heap_sort/python) |
| [Maximum Subarray Problem](https://en.wikipedia.org/wiki/Maximum_subarray_problem) | | | | [:octocat:](/maximum_subarray/Python) |
| [Knapsack Problem](https://en.wikipedia.org/wiki/Knapsack_problem) | | | | [:octocat:](knapsack_problem/Python) |
| [Selecton Sort](https://en.wikipedia.org/wiki/Selection_sort) | [:octocat:](selection_sort/C) | [:octocat:](selection_sort/Cpp) | | |

## Implemented Data Structures

| Data Structure | C | CPP | Java | Python |
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | [:octocat:](queue/Cpp) | | |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:octocat:](stack/C ) | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | [:octocat:](linked_list/Cpp) | [:octocat:](linked_list/Java) | [:octocat:](linked_list/Python) |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | [:octocat:](avl_tree/Cpp) | [:octocat:](avl_tree/Java) | [:octocat:](avl_tree/Python) |
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | [:octocat:](binary_search_tree/Cpp) | | |
| [Fenwick Tree](https://en.wikipedia.org/wiki/Fenwick_tree) | | [:octocat:](fenwick_tree/Cpp) | [:octocat:](fenwick_tree/java) | [:octocat:](fenwick_tree/Python) |
| [Doubly Linked List](https://en.wikipedia.org/wiki/Doubly_linked_list) | [:octocat:](doubly_linked_list/c) | | | |
| Data Structure | C | CPP | Java | Python |
| :--------------------------------------------------------------------- | :-------------------------------: | :---------------------------------: | :----------------------------: | :------------------------------: |
| [Queue](<https://en.wikipedia.org/wiki/Queue_(abstract_data_type)>) | | [:octocat:](queue/Cpp) | | |
| [Stack](<https://en.wikipedia.org/wiki/Stack_(abstract_data_type)>) | [:octocat:](stack/C) | | [:octocat:](stack/Java) | [:octocat:](stack/Python) |
| [Linear Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:octocat:](linked_list/C) | [:octocat:](linked_list/Cpp) | [:octocat:](linked_list/Java) | [:octocat:](linked_list/Python) |
| [AVL Tree](https://en.wikipedia.org/wiki/AVL_tree) | [:octocat:](avl_tree/C) | [:octocat:](avl_tree/Cpp) | [:octocat:](avl_tree/Java) | [:octocat:](avl_tree/Python) |
| [Binary Search Tree](https://en.wikipedia.org/wiki/Binary_search_tree) | | [:octocat:](binary_search_tree/Cpp) | | |
| [Fenwick Tree](https://en.wikipedia.org/wiki/Fenwick_tree) | | [:octocat:](fenwick_tree/Cpp) | [:octocat:](fenwick_tree/java) | [:octocat:](fenwick_tree/Python) |
| [Doubly Linked List](https://en.wikipedia.org/wiki/Doubly_linked_list) | [:octocat:](doubly_linked_list/c) | | | |

## Sample Run

| Language | Steps |
| --------------- | ---------------------------------------------------------------------- |
| JavaScript | <pre>node [filename.js]</pre> |
| Python | <pre>python [filename.py]</pre> |
| C | <pre>gcc [filename.c]<br>./a.out # unix<br>a.exe # windows</pre> |
| CPP | <pre>g++ [filename.cpp]<br>./a.out # unix<br>a.exe # windows</pre> |
| Java | <pre>javac [filename.java]<br>java [filename]</pre> |
| Language | Steps |
| ---------- | ------------------------------------------------------------------ |
| JavaScript | <pre>node [filename.js]</pre> |
| Python | <pre>python [filename.py]</pre> |
| C | <pre>gcc [filename.c]<br>./a.out # unix<br>a.exe # windows</pre> |
| CPP | <pre>g++ [filename.cpp]<br>./a.out # unix<br>a.exe # windows</pre> |
| Java | <pre>javac [filename.java]<br>java [filename]</pre> |

## Contributing

Expand All @@ -58,6 +58,7 @@ This repository contains examples of various algorithms written on different pro
4. Commit your changes: `git commit -am 'Add some feature'`.

Go through [Commit Messages guidelines](CONTRIBUTING.md#write-good-commit-messages)

5. Push to the branch: `git push origin my-new-feature`
6. Submit a pull request :smile:

Expand Down
24 changes: 24 additions & 0 deletions binary_search/C/binary_search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>

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

int main(void) {
int arr[] = {-1, 0, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int target = -1;
int result = binary_search(arr, 0, n - 1, target);
printf("Index of %d in array is: %d \n", target, result);
return 0;
}

0 comments on commit ff6b741

Please sign in to comment.