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
102 changes: 102 additions & 0 deletions Binary_Search/C/akshatraj_binary_search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Binary Search Implementation in C
* Author: Akshat Raj (github.com/AkshatRaj00)
*
* Time Complexity:
* Best Case: O(1) - Target is the middle element
* Average Case: O(log n)
* Worst Case: O(log n)
*
* Space Complexity:
* Iterative: O(1)
* Recursive: O(log n) - due to call stack
*
* Prerequisite: Array must be SORTED before applying binary search.
*
* How it works:
* Divides the search interval in half each time.
* Compares target with middle element:
* - If equal -> found
* - If smaller -> search left half
* - If larger -> search right half
*/

#include <stdio.h>

/*
* binarySearchIterative - Iterative binary search
* @arr: sorted array
* @n: number of elements
* @target: value to search
*
* Returns: index of target if found, -1 otherwise
*/
int binarySearchIterative(int arr[], int n, int target) {
int low = 0;
int high = n - 1;

while (low <= high) {
/* Avoid overflow: use low + (high - low) / 2 */
int mid = low + (high - low) / 2;

if (arr[mid] == target)
return mid; /* Target found */
else if (arr[mid] < target)
low = mid + 1; /* Search right half */
else
high = mid - 1; /* Search left half */
}

return -1; /* Target not found */
}

/*
* binarySearchRecursive - Recursive binary search
* @arr: sorted array
* @low: starting index
* @high: ending index
* @target: value to search
*
* Returns: index of target if found, -1 otherwise
*/
int binarySearchRecursive(int arr[], int low, int high, int target) {
if (low > high)
return -1; /* Base case: not found */

int mid = low + (high - low) / 2;

if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
return binarySearchRecursive(arr, mid + 1, high, target);
else
return binarySearchRecursive(arr, low, mid - 1, target);
}

int main(void) {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72, 91};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 23;

printf("Array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
printf("Searching for: %d\n\n", target);

/* Iterative */
int idx1 = binarySearchIterative(arr, n, target);
if (idx1 != -1)
printf("[Iterative] Found %d at index %d\n", target, idx1);
else
printf("[Iterative] %d not found\n", target);

/* Recursive */
int idx2 = binarySearchRecursive(arr, 0, n - 1, target);
if (idx2 != -1)
printf("[Recursive] Found %d at index %d\n", target, idx2);
else
printf("[Recursive] %d not found\n", target);

return 0;
}
76 changes: 76 additions & 0 deletions Bubble_Sort/C/akshatraj_bubble_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Bubble Sort Implementation in C
* Author: Akshat Raj (github.com/AkshatRaj00)
*
* Time Complexity:
* Best Case: O(n) - Already sorted array
* Average Case: O(n^2)
* Worst Case: O(n^2)
*
* Space Complexity: O(1) - In-place sorting
*
* How it works:
* Repeatedly compares adjacent elements and swaps them
* if they are in the wrong order. After each pass,
* the largest unsorted element "bubbles" to its correct position.
*/

#include <stdio.h>
#include <stdbool.h>

/* Swap two integers using XOR (no temp variable) */
void swap(int *a, int *b) {
if (a != b) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
}

/*
* bubbleSort - Sorts array in ascending order
* @arr: pointer to the array
* @n: number of elements
*/
void bubbleSort(int arr[], int n) {
int i, j;
bool swapped;

for (i = 0; i < n - 1; i++) {
swapped = false;

/* Last i elements are already in place */
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}

/* If no swapping occurred in this pass, array is sorted */
if (!swapped)
break;
}
}

/* Utility: print array elements */
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main(void) {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");
printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array: ");
printArray(arr, n);

return 0;
}
143 changes: 143 additions & 0 deletions Stack/C/akshatraj_stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Stack Implementation in C (using Linked List)
* Author: Akshat Raj (github.com/AkshatRaj00)
*
* Time Complexity:
* Push: O(1)
* Pop: O(1)
* Peek: O(1)
* isEmpty: O(1)
*
* Space Complexity: O(n) - n elements in stack
*
* Why Linked List over Array?
* - Dynamic size: no overflow unless RAM is full
* - No need to define max size upfront
* - Memory allocated only when needed
*
* Operations:
* push(data) - Insert element at top
* pop() - Remove and return top element
* peek() - View top element without removing
* isEmpty() - Check if stack is empty
* display() - Print all elements
*/

#include <stdio.h>
#include <stdlib.h>

/* Node structure for linked list */
typedef struct Node {
int data;
struct Node *next;
} Node;

/* Stack structure */
typedef struct Stack {
Node *top;
int size;
} Stack;

/*
* initStack - Initialize an empty stack
* @s: pointer to Stack
*/
void initStack(Stack *s) {
s->top = NULL;
s->size = 0;
}

/*
* isEmpty - Check if stack is empty
* Returns: 1 if empty, 0 otherwise
*/
int isEmpty(Stack *s) {
return s->top == NULL;
}

/*
* push - Insert element at top of stack
* @s: pointer to Stack
* @data: value to insert
*/
void push(Stack *s, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed!\n");
return;
}
newNode->data = data;
newNode->next = s->top; /* New node points to current top */
s->top = newNode; /* Update top to new node */
s->size++;
printf("Pushed: %d\n", data);
}

/*
* pop - Remove and return top element
* @s: pointer to Stack
* Returns: popped value, -1 if stack underflow
*/
int pop(Stack *s) {
if (isEmpty(s)) {
printf("Stack Underflow! Cannot pop from empty stack.\n");
return -1;
}
Node *temp = s->top;
int popped = temp->data;
s->top = s->top->next;
free(temp); /* Free memory to avoid leak */
s->size--;
printf("Popped: %d\n", popped);
return popped;
}

/*
* peek - View top element without removing
* @s: pointer to Stack
* Returns: top value, -1 if empty
*/
int peek(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty!\n");
return -1;
}
return s->top->data;
}

/* display - Print all stack elements from top to bottom */
void display(Stack *s) {
if (isEmpty(s)) {
printf("Stack is empty!\n");
return;
}
Node *curr = s->top;
printf("Stack (top -> bottom): ");
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}

int main(void) {
Stack s;
initStack(&s);

push(&s, 10);
push(&s, 20);
push(&s, 30);
push(&s, 40);

display(&s);

printf("Peek (top): %d\n", peek(&s));
printf("Stack size: %d\n", s.size);

pop(&s);
pop(&s);

display(&s);

return 0;
}