Posts

Showing posts from July, 2025

Quick sort implementation in C

               Quick sort is one of the sorting methods in data structures. The algorithm follows the divide and conquer method. Logic: First, a pivot element is selected. Check the elements in the array with the pivot element. If the element is less than pivot, it goes to the left of the array. If the element is greater than the array, it goes to the right of the array. This process is repeated until the last element. Program: #include <stdio.h> void fn_swap(int *a, int *b) {     int temp = *a;     *a = *b;     *b = temp; } int fn_partition(int arr[], int v_low, int v_high) {     int pivot = arr[v_high]; // set last element as pivot     int i = v_low - 1;     for (int j = v_low; j < v_high; j++) {         if (arr[j] < pivot) {         ...

Merge Sort implementation in C

Image
            Merge sort is one of the efficient sorting methods in data structures. It uses the divide and conquers method to sort the elements. The logic is given below… Logic: The inputs are received from the user. There are two functions. One is for divide the elements into two parts. Each part is sorted. Finally, both are merged and displayed as output. Here, is the code. Code: #include <stdio.h> //merge sort function 1 void merge_process(int a[], int a_left, int a_mid, int a_right) {     int i, j, k;     int x1 = a_mid - a_left + 1;     int x2 = a_right - a_mid;     int L[x1], R[x2];     for (i = 0; i < x1; i++) L[i] = a[a_left + i];     for (j = 0; j < x2; j++) R[j] = a[a_mid + 1 + j];     i = j = 0; k = a_left;     while (i < x1 && j < x2)      ...

Binary Search Tree implementation in C

               Binary Search Tree is tree which two nodes left and right for every node. Let us implement it in C language. It has following functions. Creating node Adding elements Searching an element. In order traversal. Code: #include <stdio.h> #include <stdlib.h> struct bst_Node {     int key;     struct bst_Node* left;     struct bst_Node* right; }; // Let us Create a new node struct bst_Node* createNode(int key) {     struct bst_Node* newNode1 = (struct bst_Node*)malloc(sizeof(struct bst_Node));     newNode1->key = key;     newNode1->left = newNode1->right = NULL;     return newNode1; } // Add a node struct bst_Node* insert(struct bst_Node* root, int key) {     if (root == NULL) return createNode(key);     if (key < root->key) ...

Queue using Circular Array using C

             A circular queue is a data structure which connects the last index with the first element. This makes the queue to be handled in efficient manner. The elements of the queue are given below…. ·        Front: It is the index of front element. ·        Rear: It is the index of last element. ·        Count: it finds the queue is full or empty. ·        The array size is fixed(a_Size). ·        Member functions: ·        Enqueue(),dequeue(),print_queue() Program: #include <stdio.h> #define a_Size 5 void enqueue(int queue1[], int *q_rear, int *q_front, int value) {     if ((*q_rear + 1) % a_Size == *q_front) {         printf("The Queue is Full\n");       ...

Stack Implementation Using Linked list in C

      The stack implementation can be done by using linked list.  This handles the dynamic memory location. It has a node and pointer. The technical implementation is as follows... code: #include <stdio.h> #include <stdlib.h> struct l_Node {     int s_data;     struct l_Node* l_next; }; struct l_Node* s_top = NULL; void s_push(int s_value) {     struct l_Node* newNode = malloc(sizeof(struct l_Node));     newNode->s_data = s_value;     newNode->l_next = s_top;     s_top = newNode;     printf("Pushed %d to the stack\n", s_value); } int pop() {     if (s_top == NULL) {         printf("The Stack is Underflow\n");         return -1;     }     int s_value = s_top->s_data;     stru...