Skip to content
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
554 changes: 74 additions & 480 deletions Readme.md

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions Searching 🔎/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
using namespace std;

// Iterative
int binarySearchIter(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) return mid;
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}

// Recursive
int binarySearchRec(int arr[], int low, int high, int key) {
if (low > high) return -1;
int mid = low + (high - low) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] < key) return binarySearchRec(arr, mid + 1, high, key);
return binarySearchRec(arr, low, mid - 1, key);
}

int main() {
int arr[] = {1, 3, 5, 7, 9, 11};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 7;
cout << "Iterative: " << binarySearchIter(arr, n, key) << endl;
cout << "Recursive: " << binarySearchRec(arr, 0, n - 1, key) << endl;
return 0;
}
Binary file added Searching 🔎/BinarySearch.exe
Binary file not shown.
38 changes: 38 additions & 0 deletions Searching 🔎/ExponentialSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <algorithm>
using namespace std;

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

int exponentialSearch(int arr[], int n, int key) {
if (n == 0) return -1;
if (arr[0] == key) return 0;

int i = 1;
while (i < n && arr[i] <= key) {
i *= 2;
}
return binarySearch(arr, i / 2, min(i, n - 1), key);
}

int main() {
int arr[] = {2, 3, 4, 10, 40, 50, 70};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 10;

int result = exponentialSearch(arr, n, key);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found" << endl;

return 0;
}
40 changes: 40 additions & 0 deletions Searching 🔎/JumpSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <cmath>
using namespace std;

int jumpSearch(int arr[], int n, int key)
{
int step = sqrt(n);
int prev = 0;

while (arr[min(step, n) - 1] < key)
{
prev = step;
step += sqrt(n);
if (prev >= n)
return -1;
}

while (arr[prev] < key)
{
prev++;
if (prev == min(step, n))
return -1;
}

return (arr[prev] == key) ? prev : -1;
}

int main(){
int arr[] = {2, 3, 4, 10, 40, 50, 70};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 10;

int result = jumpSearch(arr, n, key);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found" << endl;

return 0;
}
Binary file added Searching 🔎/JumpSearch.exe
Binary file not shown.
18 changes: 18 additions & 0 deletions Searching 🔎/LinearSearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
using namespace std;

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

int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
int result = linearSearch(arr, n, key);
cout << (result != -1 ? "Found at index " + to_string(result) : "Not found");
return 0;
}
Binary file added Searching 🔎/LinearSearch.exe
Binary file not shown.
37 changes: 37 additions & 0 deletions Searching 🔎/TernarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
using namespace std;

int ternarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;

if (arr[mid1] == key) return mid1;
if (arr[mid2] == key) return mid2;

if (key < arr[mid1]) {
high = mid1 - 1;
} else if (key > arr[mid2]) {
low = mid2 + 1;
} else {
low = mid1 + 1;
high = mid2 - 1;
}
}
return -1;
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 5;

int result = ternarySearch(arr, n, key);
if (result != -1)
cout << "Element found at index " << result << endl;
else
cout << "Element not found" << endl;

return 0;
}
Binary file added Searching 🔎/TernarySearch.exe
Binary file not shown.