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

Added Linear and Binary Search in C++ #80

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
61 changes: 61 additions & 0 deletions basic_algo/bubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
using namespace std;

void printArray(int *arr, int length)
{
for (int i = 0; i < length; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}

void bubbleSort(int *arr, int length)
{
for (int i = 0; i < length - 1; i++)
{
for (int j = 0; j < length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void bubbleSortAdaptive(int *arr, int length)
{
int isSorted = 0;
for (int i = 0; i < length - 1; i++)
{
isSorted = 1;
for (int j = 0; j < length - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSorted = 0;
}
}
if (isSorted)
{
return;
}
}
}

int main()
{
int arr[] = {12, 54, 65, 7, 23, 9};
int length = sizeof(arr) / sizeof(int);
printArray(arr, length);
bubbleSortAdaptive(arr, length);
printArray(arr, length);

return 0;
}
37 changes: 37 additions & 0 deletions searching/binary-search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Implementing binary search in C++

#include <iostream>
using namespace std;

int binarySearch(int arr[], int size, int element){
int low, mid, high;
low = 0;
high = size -1;
while(low<=high){
mid = (low + high)/2;
if(arr[mid]==element){
return mid;
}
if(arr[mid]<element){
low = mid + 1;
}
else{
high = mid -1;
}
}
return -1;
}

int main()
{
int arr[] = {1,2, 14, 19, 23, 34, 45, 56, 67, 78, 90};
int size = sizeof(arr)/sizeof(int);
int element;
cout<<"Enter the element you want to search : ";
cin>>element;

int index = binarySearch(arr, size, element);
cout<<"The element "<<element<<" is found at index "<<index;

return 0;
}
28 changes: 28 additions & 0 deletions searching/linear-search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Implementing linear search in C++

#include <iostream>
using namespace std;

int linearSearch(int arr[], int size, int element){
for (int i = 0; i < size; i++)
{
if(arr[i]==element){
return i;
}
}
return -1;
}

int main()
{
int arr[] = {12,21,32,41,52, 63, 76, 54, 23, 54, 27};
int size = sizeof(arr)/sizeof(int);
int element;
cout<<"Enter the element you want to search : ";
cin>>element;

int index = linearSearch(arr, size, element);
cout<<"The element "<<element<<" is found at index "<<index;

return 0;
}