Skip to content
Merged
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
93 changes: 55 additions & 38 deletions CPP/algorithms/searching/Seacrh/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,73 @@
#include<iostream>
using namespace std;
#include <stdio.h>

/*
======================================================
Binary Search Algorithm
======================================================
Description:
Binary Search is an efficient algorithm to find the position of a target value
in a sorted array. It repeatedly divides the search interval in half.

Key Idea:
1. Start with the entire array as the search interval.
2. Find the middle element.
3. If the middle element is equal to the target, return its index.
4. If the target is smaller, search the left half.
5. If the target is larger, search the right half.
6. Repeat until the target is found or interval is empty.

Time Complexity: O(log n)
Space Complexity: O(1) for iterative implementation
======================================================
*/

int binarysearch(int arr[], int size, int key){
int s = 0; //startingpoint
int e = size-1; //ending point
while(s<size){
int mid = (s+(e-1))/2;
if(arr[mid]==key){
return mid;
// Iterative Binary Search function
int binarySearch(int arr[], int size, int target) {
int left = 0;
int right = size - 1;

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

if (arr[mid] == target) {
return mid; // Target found
}
else if(arr[mid] > key){
e = mid-1;
else if (arr[mid] < target) {
left = mid + 1; // Search in right half
}
else{
s = mid+1;
else {
right = mid - 1; // Search in left half
}
}
//if not work then we ll return -1
return -1;
}




return -1; // Target not found
}

int main() {
int n, target;

// Input the size of the array
printf("Enter size of array: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements in sorted order: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int main (){
// Input the target value
printf("Enter the target value: ");
scanf("%d", &target);

int size;
cout<<"enter the size of the array : ";
cin>>size;
int key;
cout<<"enter the key : ";
cin>>key;
// Call Binary Search
int result = binarySearch(arr, n, target);

int arr[size];
cout<<"Enter the elements of array : "<<endl;
for(int i = 0; i<size; i++){
cin>>arr[i];
}
int result = binarysearch(arr,size,key);
if(result == -1){
cout<<"Element is not present in the array"<<endl;
}
else{
cout<<"Element is present at index "<<result<<endl;
if (result != -1) {
printf("Target %d found at index %d.\n", target, result);
} else {
printf("Target %d not found in the array.\n", target);
}

return 0;
}
}
91 changes: 91 additions & 0 deletions CPP/data_structures/graphs/graph/prism.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <bits/stdc++.h>
using namespace std;

/*
======================================================
Prim's Algorithm: Minimum Spanning Tree (MST)
======================================================
Description:
Prim's algorithm finds the Minimum Spanning Tree (MST) of a weighted
undirected graph. The MST is a subset of edges connecting all vertices
with the minimum total edge weight, without forming cycles.

Approach:
1. Start with a single vertex (we start with vertex 0).
2. Pick the smallest edge connecting a vertex in MST to a vertex outside MST.
3. Include that edge and repeat until all vertices are included in MST.

Time Complexity:
- O(V^2), where V is the number of vertices (using adjacency matrix).
- Using priority queues (min-heap) can reduce it to O(E log V).

Space Complexity:
- O(V^2) for adjacency matrix.
======================================================
*/

const int INF = 1e9; // Infinity value for initialization

int main() {
int n; // Number of vertices
cout << "Enter number of vertices: ";
cin >> n;

// Input adjacency matrix
vector<vector<int>> graph(n, vector<int>(n));
cout << "Enter adjacency matrix (0 for no edge):\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j];
}
}

// key[i] -> Minimum weight edge to include vertex i in MST
vector<int> key(n, INF);

// inMST[i] -> True if vertex i is included in MST
vector<bool> inMST(n, false);

// parent[i] -> Stores the parent of vertex i in MST
vector<int> parent(n, -1);

// Start from vertex 0
key[0] = 0;

// MST will have n vertices
for (int count = 0; count < n - 1; count++) {
// Step 1: Pick the minimum key vertex from the set of vertices not yet included in MST
int u = -1;
int minKey = INF;

for (int v = 0; v < n; v++) {
if (!inMST[v] && key[v] < minKey) {
minKey = key[v];
u = v;
}
}

// Step 2: Include this vertex in MST
inMST[u] = true;

// Step 3: Update key and parent for adjacent vertices
for (int v = 0; v < n; v++) {
// If there is an edge u-v and v is not in MST and weight is smaller
if (graph[u][v] != 0 && !inMST[v] && graph[u][v] < key[v]) {
key[v] = graph[u][v];
parent[v] = u;
}
}
}

// Print the MST
cout << "\nEdges in the Minimum Spanning Tree (MST):\n";
cout << "Edge \tWeight\n";
for (int i = 1; i < n; i++) {
cout << parent[i] << " - " << i << "\t" << graph[i][parent[i]] << "\n";
}

return 0;
}

//added to prism branch