From d79c9a99f5a1a254c66a7813e842151c6acb850c Mon Sep 17 00:00:00 2001 From: Sarveshwar Shukla Date: Fri, 10 Dec 2021 10:43:58 +0530 Subject: [PATCH 1/3] Added linear search algorithm in C++ --- searching/linear-search.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 searching/linear-search.cpp diff --git a/searching/linear-search.cpp b/searching/linear-search.cpp new file mode 100644 index 0000000..5985079 --- /dev/null +++ b/searching/linear-search.cpp @@ -0,0 +1,28 @@ +// Implementing linear search in C++ + +#include +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 "< Date: Fri, 10 Dec 2021 11:01:38 +0530 Subject: [PATCH 2/3] Added binary search algorithm in C++ --- searching/binary-search.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 searching/binary-search.cpp diff --git a/searching/binary-search.cpp b/searching/binary-search.cpp new file mode 100644 index 0000000..deeec4e --- /dev/null +++ b/searching/binary-search.cpp @@ -0,0 +1,37 @@ +// Implementing binary search in C++ + +#include +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; + + int index = binarySearch(arr, size, element); + cout<<"The element "< Date: Sat, 1 Jan 2022 18:46:40 +0530 Subject: [PATCH 3/3] Added BubbleSort using C++ --- basic_algo/bubbleSort.cpp | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 basic_algo/bubbleSort.cpp diff --git a/basic_algo/bubbleSort.cpp b/basic_algo/bubbleSort.cpp new file mode 100644 index 0000000..6c0e620 --- /dev/null +++ b/basic_algo/bubbleSort.cpp @@ -0,0 +1,61 @@ +#include +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; +}