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
64 changes: 64 additions & 0 deletions BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.*;

public class BinarySearch {
// Iterative Binary Search
public static int Iterative(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target)
return mid; // Element found
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1; // Element not found
}

// Recursive Binary Search
public static int Recursive(int[] arr, int low, int high, int target) {
if (low > high)
return -1;
int mid = low + (high - low) / 2;
if (arr[mid] == target)
return mid;
else if (arr[mid] > target)
return Recursive(arr, low, mid - 1, target);
else
return Recursive(arr, mid + 1, high, target);
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();

int[] arr = new int[n];
System.out.println("Enter elements in sorted order:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

System.out.print("Enter target element to search: ");
int target = sc.nextInt();

// Iterative version
int resultIterative = Iterative(arr, target);

// Recursive version
int resultRecursive = Recursive(arr, 0, n - 1, target);

if (resultIterative != -1)
System.out.println("Element found at index (iterative): " + resultIterative);
else
System.out.println("Element not found (iterative).");

if (resultRecursive != -1)
System.out.println("Element found at index (recursive): " + resultRecursive);
else
System.out.println("Element not found (recursive).");

sc.close();
}
}
28 changes: 28 additions & 0 deletions Insertion_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;
vector<int> arr(n);
cout << "Enter elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
insertionSort(arr);
cout << "Sorted array: ";
for (auto x : arr) cout << x << " ";
cout << endl;
return 0;
}