From ff6b741058f2ecc7d0984623af555312ee3b996f Mon Sep 17 00:00:00 2001 From: varun Date: Sat, 3 Oct 2020 16:01:24 +0530 Subject: [PATCH] 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 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 https://github.com/NITSkmOS/Algorithms/issues/44 Update README.md Co-authored-by: Sangam Kumar 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 https://github.com/NITSkmOS/Algorithms/issues/44 --- README.md | 63 +++++++++++++++++---------------- binary_search/C/binary_search.c | 24 +++++++++++++ 2 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 binary_search/C/binary_search.c diff --git a/README.md b/README.md index d0009fb1..203b1486 100644 --- a/README.md +++ b/README.md @@ -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]() | | [:octocat:](queue/Cpp) | | | +| [Stack]() | [: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 |
node [filename.js]
| -| Python |
python [filename.py]
| -| C |
gcc [filename.c]
./a.out # unix
a.exe # windows
| -| CPP |
g++ [filename.cpp]
./a.out # unix
a.exe # windows
| -| Java |
javac [filename.java]
java [filename]
| +| Language | Steps | +| ---------- | ------------------------------------------------------------------ | +| JavaScript |
node [filename.js]
| +| Python |
python [filename.py]
| +| C |
gcc [filename.c]
./a.out # unix
a.exe # windows
| +| CPP |
g++ [filename.cpp]
./a.out # unix
a.exe # windows
| +| Java |
javac [filename.java]
java [filename]
| ## Contributing @@ -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: diff --git a/binary_search/C/binary_search.c b/binary_search/C/binary_search.c new file mode 100644 index 00000000..82dd69c0 --- /dev/null +++ b/binary_search/C/binary_search.c @@ -0,0 +1,24 @@ +#include + +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; +}