• Arrays

    Write a program to reverse an array.

    by nikoo28
    2 minutes read

    Iterative way: 1) Initialize start and end indexes. start = 0, end = n-1 2) In a loop, swap arr[start] with arr[end] and change start and end as follows. start = start +1; end = end – 1 Time Complexity: O(n) Recursive Way: 1) Initialize start and end indexes start = 0, end = n-1 2) Swap arr[start] with arr[end] 3) Recursively call reverse for rest of the array. Time Complexity: O(n)

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Counting Sort

    by nikoo28
    4 minutes read

    Counting sort is not a comparison sort algorithm. The sorting techniques that work on comparison are: Selection Sort Bubble Sort Insertion Sort Merge Sort Quick Sort The best time complexity you can achieve in any of these sorting techniques is . However, is some very specific cases, you can achieve an even faster computation time of . This is made possible by an assumption. We assume that the input data set is within a finite limit. For example, if you want to sort some characters of the English alphabet. You …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Shell sort

    by nikoo28
    3 minutes read

    Shell sort (also called diminishing increment sort) was invented by Donald Shell. This sorting algorithm is a generalization of insertion sort. Insertion sort works efficiently on input that is already almost sorted. In insertion sort, comparison happens between the adjacent elements. At most 1 inversion is eliminated for each comparison done with insertion sort. The variation used in shell sort is to avoid comparing elements until the last step of the algorithm. So, the last step of shell sort is to avoid comparing adjacent elements until the last step of …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    How to initialize an array to 0 in C ?

    by nikoo28
    2 minutes read

    In C, we declare an array as:- But by simply doing this declaration, the array is assigned all junk values. There can be many cases and situations when we need to initialize all the elements to ZERO (0) before we can make any further computations. The most naive technique to initialize is to loop through all the elements and make them 0. We have 3 other simple techniques as:- 1.>  Global variables and static variables are automatically initialized to zero. If we have an array at global scope it will …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: We are given an input string, and we need to find the character with the maximum frequency. Input:- “This is an sample string.” Output:- The character with maximum frequency is :- i The logic of this problem is very simple, we need to scan the string and store the frequency of each of the character. For this we can use the ASCII code of the characters which is a separate integer. Note that character are not only the English alphabets. !@#$ are also characters. To extend our program to …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Quick Sort

    by nikoo28
    6 minutes read

    This algorithm is one of the specific ones people generally have a problem to understand. We will try out best to simplify it and understand it.Basically quick sort comprises of these steps:- Choose a pivot element (it can be any random element in array) In one iteration keep all the numbers smaller to it on the left side and larger to it on the right side.(smaller numbers)_ _ _[PIVOT]_ _ _(larger numbers) Now we have the left sub-array of (small numbers) and the right sub-array (large numbers) Repeat steps 1 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Print reverse of a string using recursion.

    by nikoo28
    2 minutes read

    Question: Write a program in C to reverse a string using recursion Input: Hello Everyone Output: enoyrevE olleH We will use the simple concept of recursion to print the string in reversed order. Note that, we will not store the reversed string in any variable, we are just printing the string in reversed order. Recursive function (reversePrint) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way, when pointer reaches ‘\0′, all functions accumulated in stack print char at passed …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More