Sunday, September 16, 2018

Sorting Algorithms

  1. Merge Sort: It is divider and conquer technique.
    1. Divide the array into two halves using MergeSort() function.
    2. Merge the two sub arrays into single in sorted order using Merge() function.
    3. Merge sort program in c#
    4. Time complexity O(n logn).
  2. Insertion Sort:
    1. Its like Playing cards., take an element, Find location / index to insert this new element, by Comparing with all existing elements.
    2. InsertionSort in C#
    3. Time complexity O(n * n).
  3. Selection Sort:
    1. Sorts an array by repeatedly finding the minimum element from unsorted part of array.
    2. Selection sort in c#
    3. Time complexity O(n * n).

Thursday, September 6, 2018

LINQ Single vs SingleOrDefault vs First vs FirstOrDefault

 Single()SingleOrDefault()First()FirstOrDefault()
DescriptionReturns a single, specific element of a sequenceReturns a single, specific element of a sequence, or a default value if that element is not foundReturns the first element of a sequenceReturns the first element of a sequence, or a default value if no element is found
Exception thrown whenThere are 0 or more than 1 elements in the resultThere is more than one element in the resultThere are no elements in the resultOnly if the source is null (they all do this)
When to useIf exactly 1 element is expected; not 0 or more than 1When 0 or 1 elements are expectedWhen more than 1 element is expected and you want only the firstWhen more than 1 element is expected and you want only the first. Also it is ok for the result to be empty