-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fibonacci Search in C++ #2265
Fibonacci Search in C++ #2265
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good. Stylistic changes requested 👍
2. | ||
Enter Size Of Arrary: | ||
4 | ||
Enter Elements In Array | ||
1 2 3 4 | ||
Enter The Element To Be Searched | ||
5 | ||
Element Not Found | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this. One sample I/O does the trick
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay
int n; /* size of array */ | ||
cout<<"Enter Size Of Arrary:"<<"\n"; | ||
cin>>n; | ||
|
||
int arr[n]; /* array declaration */ | ||
cout<<"Enter Elements In Array"<<"\n"; | ||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
cin>>arr[i]; | ||
} | ||
|
||
int element; /* Element that user want's to search */ | ||
cout<<"Enter The Element To Be Searched"<<"\n"; | ||
cin>>element; | ||
|
||
int index = fibonacciSearch(arr,n,element); /* Call fibonacci search function */ | ||
|
||
if(index==-1) | ||
cout<<"Element Not Found"<<"\n"; /* if element is not present */ | ||
else | ||
cout<<"Element Found At Array Index : "<<index; /* if Present print index*/ | ||
|
||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indent the Code. In fact, run a formatter on the whole code
/* if found return index */ | ||
else return i; | ||
} | ||
/* check for last array element */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use // .......
for single line comments only
fibM1 = fibM1 - fibM2; | ||
fibM2 = fibM - fibM1; | ||
} | ||
/* if found return index */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use // .......
for single line comments only
|
||
int fibonacciSearch(int arr[],int n,int element) | ||
{ | ||
/* Initializing fibonacci no */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use // .......
for single line comments only
Time Complexity : O(log(n)) | ||
Auxiliary Space : O(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep complexities at the end of the code
Sure! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use redundant comments. We don't need to explain what each variable or console statement do
Time Complexity : O(log(n)) | ||
Auxiliary Space : O(1) | ||
|
||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a new line at the EOF
if (index == -1) | ||
cout << "Element Not Found" << "\n"; // if element is not present | ||
else | ||
cout << "Element Found At Array Index : " << index; // if Present print index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use comments above the line of code not beside it.
int index = fibonacciSearch(arr, n, element); // Call fibonacci search function | ||
|
||
if (index == -1) | ||
cout << "Element Not Found" << "\n"; // if element is not present |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use comments above the line of code not beside it.
cout << "Enter The Element To Be Searched" << "\n"; | ||
cin >> element; | ||
|
||
int index = fibonacciSearch(arr, n, element); // Call fibonacci search function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use comments above the line of code not beside it.
cin >> arr[i]; | ||
} | ||
|
||
int element; // Element that user want's to search |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use comments above the line of code not beside it.
} | ||
|
||
int main() { | ||
int n; // size of array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use comments above the line of code not beside it.
cout << "Enter Size Of Arrary:" << "\n"; | ||
cin >> n; | ||
|
||
int arr[n]; // array declaration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use comments above the line of code not beside it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Thank you for your contribution. 👍
Have you read the Contributing Guidelines on Pull Requests?
YES
Description
I have added Fibonacci search under the search section in c++.
Checklist
README.md
and link to my code.Related Issues or Pull Requests
#2242