Found 1401 Articles for C

A sorting algorithm that slightly improves on selection sort?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

214 Views

Here we will see some improvements on selection sort. As we know that the selection sort works by taking either the minimum or maximum element from the array and place that element at correct position. In this approach, we want to sort the array in both ways. Here we will take the max and min simultaneously, then sort the array from two end. Let us see the algorithm to get better idea.AlgorithmtwoWaySelectionSort(arr, n)begin    for i := 0, and j := n-1, increase i by 1, and decrease j by 1, until i>=j, do       min := minimum ... Read More

A Program to check if strings are rotations of each other or not?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

663 Views

Here we will see one program that can tell whether two strings are rotation of each other or not. The rotation of strings is like −Suppose two strings are S1 = ‘HELLO’, and S2 = ‘LOHEL’ So they are rotation of each other. By rotating HELLO three position to the left it will be LOHEL.To solve this problem, we will concatenate the first string with itself, then check whether the second one is present in the concatenated string or not. So for HELLO, it will be HELLOHELLO. Then this concatenated string contains the LOHEL. [HELLOHELLO].AlgorithmisRotation(str1, str2)begin    if lengths of ... Read More

A Problem in Many Binary Search Implementations?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

67 Views

We know that the binary search algorithm is better than the linear search algorithm. This algorithm takes O(log n) amount of time to execute. Though most of the cases the implemented code has some problem. Let us consider one binary search algorithm function like below −Exampleint binarySearch(int array[], int start, int end, int key){    if(start key)          return binarySearch(array, start, mid-1, key);          return binarySearch(array, mid+1, end, key);    }    return -1; }This algorithm will work fine until the start and end reaches a large number. If the (start + end) ... Read More

A permutation where each element indicates either number of elements before or after it?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

59 Views

In this section we will see one problem. Here n elements are given in an array. We have to check whether there is a permutation of that array exists, such that each element indicates the number of elements present either before or after it.Suppose the array elements are {2, 1, 3, 3}. The appropriate permutation is like {3, 1, 2, 3}. Here the first 3 is indicating there are three elements next of it, the 1 indicates there is only one element before this. The 2 indicates there are two elements before it and the last 3 indicates that there ... Read More

A backtracking approach to generate n bit Gray Codes ?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

351 Views

In this section we will see how we can generate the gray codes of n bits using backtracking approach? The n bit gray code is basically bit patterns from 0 to 2^n – 1 such that successive patterns differ by one bit. So for n = 2, the gray codes are (00, 01, 11, 10) and decimal equivalent is (0, 1, 3, 2). The program will generate the decimal equivalent of the gray code values.AlgorithmgenerateGray(arr, n, num)begin    if n = 0, then       insert num into arr       return    end if    generateGray(arr, n-1, ... Read More

What is a function specifier in C?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

425 Views

In C and C++ there are some function specifiers. The function specifiers are used to specify the functions property. C++ has inline function specifier. In C there is _Noreturn function specifier. This is used to denote that one function will not return anything.Example Live Demo#include int myAdd(int a, int b){    return a + b; } main() {    int x = 10, y = 20;    printf("The value is: %d", myAdd(x, y)); }OutputThe value is: 30If the _Noreturn is used it will display some warning and the program will be terminated with some error.Example#include _Noreturn int myAdd(int a, int b){ ... Read More

Generic keyword in C ?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

223 Views

As we know that the Macros are used in C or C++, but there is no facility for type checking. The macros can take any type of argument in it. The following example will show this case clearly.Example Live Demo#include #define INCREMENT(X) ++X main() {    int x = 5; float y = 2.56; char z = 'A';    printf("Integer Increment: %d", INCREMENT(x));    printf("Float Increment: %f", INCREMENT(y));    printf("Character Increment: %c", INCREMENT(z)); }OutputInteger Increment: 6 Float Increment: 3.560000 Character Increment: BThat is the problem of macro. In the later version of C, we can use macro by using ‘_Generic’ keyword. ... Read More

C Program for Egg Dropping Puzzle - DP-11?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

240 Views

This is a famous puzzle problem. Suppose there is a building with n floors, if we have m eggs, then how can we find the minimum number of drops needed to find a floor from which it is safe to drop an egg without breaking it.There some important points to remember −When an egg does not break from a given floor, then it will not break for any lower floor also.If an egg breaks from a given floor, then it will break for all upper floors.When an egg breaks, it must be discarded, otherwise we can use it again.Input - The ... Read More

C Program for Basic Euclidean algorithms?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

178 Views

Here we will see the Euclidean algorithm to find the GCD of two numbers. The GCD (Greatest Common Divisor) can easily be found using Euclidean algorithm. There are two different approach. One is iterative, another one is recursive. Here we are going to use the recursive Euclidean algorithm.AlgorithmEuclideanAlgorithm(a, b)begin    if a is 0, then       return b    end if    return gcd(b mod a, a) endExample Live Demo#include using namespace std; int euclideanAlgorithm(int a, int b) {    if (a == 0)       return b;    return euclideanAlgorithm(b%a, a); } main() {    int a, b;    cout > a >> b;    cout

C/C++ Program for Largest Sum Contiguous Subarray?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:26

349 Views

An array of integers is given. We have to find sum of all elements which are contiguous. Whose sum is largest, that will be sent as output.Using dynamic programming we will store the maximum sum up to current term. It will help to find sum for contiguous elements in the array.Input: An array of integers. {-2, -3, 4, -1, -2, 1, 5, -3} Output: Maximum Sum of the Subarray is : 7AlgorithmmaxSum(array, n)Input − The main array, the size of the array.Output − maximum sum.Begin    tempMax := array[0]    currentMax = tempMax    for i := 1 to n-1, ... Read More

Advertisements