Posts

Selection Sort implementation in C

              Selection sort is one of the simple algorithm which deals with comparison. It finds the minimum element and makes that element to sit in the front. It keeps on the same process for entire elements. Let us the discuss the process. Selection sort:               It makes the array into two parts. One is sorted and other one is unsorted. First, the sorted part doesn’t have a value. The unsorted part has the entire elements. The process starts now. Each iteration, repeat the process. The minimum element is searched in the unsorted part. Once, it found, swap it with the first element of the unsorted side. Continue this until last element. C implementation: #include <stdio.h>   void selection_Sort(int a[], int n) {     int i, j, min_Index, t;     for (i = 0; i < n-1; i++) {      ...

Bubble sort implementation in C

       Sorting is the process of arranging elements in order. Bubble sort is one of the basic sorting methods which compares adjacent elements. It swaps if the elements are not sorted. Otherwise, it keeps the array elements and moves to the next adjacent elements. 👉Implementation: This implementation starts with including the built-in header file. Next, a function to swap the elements is defined. It uses a temporary variable to exchange the values. The function ‘bubble_Sort()’ gets the input as array and its size. It checks the adjacent elements value. If these are sorted already, it keeps the values. Else, it swaps the values. ‘main()’ function gets the array size and elements from the user. It calls the ‘bubble_Sort()’ function and gets the output. Finally,it prints the sorted array. ##C Code: #include <stdio.h> // Function to swap two elements void b_swap(int *x, int *y) {     int temp = *x;     *x...

Binary search implementation in C

              Searching is a process of finding an element from a group of elements. Binary search is one of the efficient search algorithms . Let us implement it in C language . Implementation: This algorithm follows a method as follows. First, find the middle element. Check the element to be searched with middle element. If it is small, search the element in the left child of the array. If it is large, go for right side. Repeat this process to find the exact position. C Code: #include <stdio.h> int binarySearch(int a[], int n, int key) {     int low = 0, high = n - 1;     while (low <= high) {         int mid = (low + high) / 2;         if (a[mid] == key)             return mid;   // yes ,it is Found         else if (...

How to insert and delete elements in an array using C

       Array is one of the important data structures widely used in programming. This blog post includes the insertion and deletion of elements in array. Insertion:               An array is declared and inputs are read from the user. To insert an element, the position and data are the inputs. Using loop , we insert the element and displayed the output after insertion. C Program: #include <stdio.h> int main() {     int a[10];     int n,i,j,k;     printf("Enter the number of elements:");     scanf("%d",&n);     printf("Enter the elements:");     for(i=0;i<n;i++)     {         scanf("%d",&a[i]);     }     printf("Enter the position to insert an element:");     scanf("%d...

Hash table implementation in C

               A hash table has an array of buckets. It has a key and value. Key is to find the position of the bucket. Value is to be stored on that location. It can be done by two ways as follows. ·        Using linked list ·        Linear probing Here, we use linked list to implement the hash table. Code: #include <stdio.h> #include <stdlib.h> #include <string.h> //The size of the hash table is defined as 4. #define SIZE 4 //create a node for hash table. it has a key, value and a pointer to point the next node struct h_Node {     int h_key;     int h_value;     struct h_Node* h_next; }; // create a hash table struct h_Node* hashTable[SIZE];   // Hash function to find the hash code int hashCode(int h_key) {     return h_key % SIZE; } // this function adds the ...

Binary Tree implementation in C

               Tree is a non-linear data structure which deals with root element and child nodes.Root is the main node which connects the entire data structure. It has childs which is divided into 2 types. The two categories are Left and right. Let us create a binary tree which has exactly two child is given below… C implementation of binary Tree using Linked list: Here, the binary tree is created using linked list . A node is created as root. It has two nodes and data. ‘createNode()’ creates a node with memory and data. There are three traversal to print the node data. Pre-order traversal : It follows ‘Root-left child-right child’ format. Post – order traversal :it uses ‘left child – right child- root’ format. In-order traversal :It has ‘left child- root- right child’ format. Code: #include <stdio.h> #include <stdlib.h> // create the tree as linked list struct T_Node {     int data; ...

Graph implementation in C

                 Graph is a non-linear one which has vertices and edges. Vertices are the nodes and the edges are links which connect the nodes. General representation of G is represented by G = (V,E) Where, V represents by vertices and E represents by edges. Let us implement the graph in two different ways as follows.. 1.       Adjacency Matrix 2.       Adjacency List 1.Adjacency Matrix implementation of Graph:               This method follows a two-dimensional Matrix to implement the graph. It creates a 2D matrix to get the graph structure. It reads the edges from the user and assigns the value. Finally displays the graph. C Program: #include <stdio.h> #define V 4 void printIt(int adjM_graph[V][V]) {     for (int i = 0; i < V; i++) {         f...