Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add binary search in c #593

Closed
wants to merge 1 commit into from
Closed

Conversation

varunkmr19
Copy link

For short term contributors: we understand that getting your commits well
defined like we require is a hard task and takes some learning. If you
look to help without wanting to contribute long term there's no need
for you to learn this. Just drop us a message and we'll take care of brushing
up your stuff for merge!

Fixes #44

By submitting this pull request I confirm I've read and complied with the
below declarations.

  • I have read the Contribution guidelines and I am confident that my PR reflects them.
  • I have followed the commit guidelines for this project.
  • My code follows the standard code structure.
  • This pull request has a descriptive title. For example, {Tag}: Add {Algorithm/DS name} [{Language}], not Update README.md or Added new code.
  • This pull request will be closed if I fail to update it even once in a continuous-time span of 7 days.
  • This pull request shall only be reviewed and merged once all the checks pass. No maintainer or supporter shall be obliged to review it before this condition is met.
  • I have mentioned the issue number correctly (with hyperlink) in this pull request description.

After you submit your pull request, DO NOT click the 'Update Branch' button.

@varunkmr19
Copy link
Author

I went through the logs of Travis CI. It says there are some extra whitespaces in my source code. So, should I fix it or is it good enough for a merge?

@sangamcse
Copy link
Member

Yes @varunkmr19, the fix is needed. Also, it would be great if you go through https://github.com/NITSkmOS/Algorithms/blob/master/CONTRIBUTING.md#write-good-commit-messages and do the necessary changes. Also, can you add entry in README.md?

@sameerchoubey
Copy link
Contributor

sameerchoubey commented Oct 7, 2020

@varunkmr19 There are few things that need to be sorted for this PR to get merged.

a) At line 5, we cannot obtain the size of an array after passing it inside a function because only the first address of the array is passed inside the function.
When we pass an array to a function, it decays into a pointer to the first element, at which point knowledge of its size is not known.
So, it would be something like this, after the change,
int main(){
int end = sizeof(arr)/sizeof(arr[0]);
int ans = binary_search(arr, end, target);
}

b) Also, update the README file and remove the extra spacing.

Everything else looks good.

@varunkmr19
Copy link
Author

Don't know why It's not passing all the checks. Even after I fixed the code and removed the extra spacing as well.

README.md Outdated Show resolved Hide resolved
@@ -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:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in this file is redundant. Kindly change only line that is required.

@varunkmr19
Copy link
Author

Finally, I found out what the issue was. I had installed prettier(a VScode extension) which was messing with the alignment.

@sameerchoubey
Copy link
Contributor

That's great. But, please squash your commits.

@sangamcse
Copy link
Member

@varunkmr19 can you please go through the review comments and do the changes that are requested?

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
Copy link
Member

@sangamcse sangamcse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added few suggestions that needs to be done and make sure to do git commit --amend instead of git commit. And in the commit message, add line break after 80 characters so that each line just consists 80 chars. For ref, https://github.com/NITSkmOS/Algorithms/blob/master/CONTRIBUTING.md#commit-body.

Rest looks good to me.

Comment on lines +33 to +41
| 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) | | | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not do unnecessary changes

Suggested change
| 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) | | | |

Comment on lines +45 to +51
| 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> |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| 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> |

| [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) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have listed you file at 2 places in this line, one in C column and other in Java column. I hope you understand what you are doing here

Comment on lines +15 to +29
| 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) | | |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| 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) | | |
| 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/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) | | |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Algo] Binary Search [C]
3 participants