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

algoritm bubble sort cpp code #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions binry_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
//binary search whith class method
class BinarySearch {
public:
BinarySearch (int *array, int size) : arr(array), size(size){}
int search(int num) {
int left = 0;
int right = size -1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == num) {
return mid;
}

if (arr[mid] > num) {
right = mid - 1;
} else {
left = mid + 1;
}
}
}

private:
int *arr;
int size;
};
int main() {
int sortedArray[]= {0 ,1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int arraySize = sizeof(sortedArray) / sizeof(sortedArray[0]);
BinarySearch ob1(sortedArray, arraySize);
int num;
std::cout<<"select number for search : ";
std::cin>>num;
int answer = ob1.search(num);
std::cout << "search number ::: " << answer<< std::endl;
return 0;
}