Home Theory
Category:

Theory

View theory topics

  • Theory

    When to use which data structure?

    by nikoo28
    1 minutes read

    We have discussed some very basic data structures. Sometimes, we need to decide which data structure to use at which place. Although is there is no hard and fast rule of using a data structure in a specific scenario, the below guidelines can help for general cases. For our program to be very efficient we must be aware of the sample data, our code needs to handle and a data structure should be used accordingly. Ordered Array :- If amount of data is small and predictable and search speed is…

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is actually Space Complexity ?

    by nikoo28
    1 minutes read

    The term Space Complexity is misused for Auxiliary Space at many places. Following are the correct definitions of Auxiliary Space and Space Complexity. Auxiliary Space is the extra space or temporary space used by an algorithm. Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input. We can also say that the way in which the amount of storage space required by an algorithm varies with the size of the problem…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Stability of Sorting Algorithms

    by nikoo28
    1 minutes read

    A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc. However, any given sorting algorithm which is not stable can be modified to be stable. There can be sorting algorithm specific ways to make it stable, but in general, any comparison based sorting…

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    Algorithm Analysis – Crash Course

    by nikoo28
    3 minutes read

    We have been discussing the time complexities of each of the algorithm that we have discussed. Let us try to analyze, how these time complexities are calculated. O(1) Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. For example swap() function(which interchanges two numbers) has O(1) time complexity. A loop or recursion that runs a constant number of times is also considered as O(1). For example the following loop is O(1). O(n)…

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is a wild pointer?

    by nikoo28
    1 minutes read

    Question: What is a wild pointer in a program? Uninitialized pointers are known as wild pointers. These are called so because they point to some arbitrary memory location and may cause a program to crash or behave badly. This can be understood from the below examples. Please note that if a pointer p points to a known variable then it’s not a wild pointer. In the below program, p is a wild pointer till this points to a. If we want pointer to a value (or set of values) without…

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscTheory

    What is a memory leak?

    by nikoo28
    1 minutes read

    Question:What is a memory leak in a program? We have been writing many programs where we have used the “free()” keyword. One such example can be found in the “Delete the complete list” function found in the post – Important traversals in a Linked List. The code snippet is:- This is done due to a reason. When we allocate a memory in a program, usually that memory is unallocated once the execution of the program finishes. However, there may be some cases where the program runs forever and these scenarios,…

    0 FacebookTwitterLinkedinWhatsappEmail
  • StringsTheory

    Swap strings in C

    by nikoo28
    1 minutes read

    Question:How will you swap two strings in C? Let us consider the below program. Output of the program is str1 is study, str2 is algorithms. So the above swap() function doesn’t swap strings. The function just changes local pointer variables and the changes are not reflected outside the function. Let us see the correct ways for swapping strings: Method 1(Swap Pointers):- If you are using character pointer for strings (not arrays) then change str1 and str2 to point each other’s data. i.e., swap pointers. In a function, if we want…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array with n+2 elements, all elements of the array are in range 1 to n and also all elements occur only once except 2 numbers which occur twice. Find those 2 repeating numbers. Input: arr [ ] = {6, 2, 6, 5, 2, 3, 1} Output: 6 2 We discussed the method to find the 2 repeating element using 2 loops, but that method had a great time complexity and it was not at all an optimal solution. We can optimize it using a count array. This…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Different types of loops in C

    by nikoo28
    1 minutes read

    There are 3 loops in C: for, while, do-while. Here is a bit detail about them:- A while loop will always evaluate the condition first. A do/while loop will always execute the code in the do{} block first and then evaluate the condition. A for loop allows you to initiate a counter variable, a check condition, and a way to increment your counter all in one line. At the end of the day, they are all still loops, but they offer some flexibility as to how they are executed. For…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Linear Search

    by nikoo28
    4 minutes read

    Linear Search is probably the easiest searching technique that is available in computer programming. You simply need to iterate over each element in a list sequentially and compare if it matches the element to search. Searching is a prime concept, as it gives you a starting point to your code. Even in real life, if you plan to go to a movie, a vacation or a shopping mall, the first thing you do is search for a location. Similarly, in programming you could have a search for anything. Google has…

    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