Posts

Showing posts from March, 2021

Binary Search - Time Complexity

Image
Time Complexity of Binary Search Algorithm Algorithm:       Int BinarySearch(int Array[], int value, int left, int right){                Int mid=(left+right)/2     If(Array[mid]== value){ Return mid; }elif(left>right){ Return -1; }elif(Array[mid] > value){ BinarySearch(Array,left,mid-1); }else{   BinarySearch(Array, mid+1,right); }   End if   } Let N be the Size of the Binary tree   T(N) =  C +  T(N/2)   ------ 1 T(N/2) = C + T (N/4) ------2 Substitute 2 à 1 T(N) = 2C + T(N/4)    -------3 T(N/4) = C + T(N/8)  --------4 Substitute 4 à 3   T(N) = 3C + T(N/8) ------5   pattern   T(N) = iC + T(N/2 i )        where I is an integer     At some point , as N/2 i diminishes , we reach only 1 element,   So     ...

Quick Sort - Time Complexity

Image
 Time Complexity of Quick Sort Algorithm:                            QuickSort(left, right){                                   If(left < right){                                              Pi= partition(left,right)                                                QuickSort(left,pi)                                         ...

Merge Sort - Time Complexity

Image
 Time Complexity of Merge Sort Algorithm:                       MergeSort(left, right){                              If(left < right){                                         Mid=(left+right)/2                                            MergeSort(left,mid)                                          MergeSort(mid+1, right)         ...

Insertion Sort - Time Complexity

Image
 Time Complexity of Insertion Sort Selection Sort is using two for loops to do one iteration. The first loop (Outer loop ) travels through an array starting from index 0, while the inner loop runs from index i+1 to index less than i. The following diagram shows the Number of execution rounds it performs. when i is 0,, j is 1,  the inner for loop runs n-1 time. when i is 1, j is 2, the inner for loop runs n-2 times. Likewise, when i is n, j is n+1,  the inner for loop runs n times. So the total number of Execution rounds can be calculated by adding all Execution times in each i values.  Time Complexity =    n-1 (n-1)                                              2            ...

Selection Sort - Time Complextity

Image
  Time Complexity of Selection Sort  Selection sort  is using two for loops to do one iteration. The first loop (Outer loop ) travels through an array starting from index 1, while the inner loop runs from index 1 to index i-1. The following diagram shows the Number of execution rounds it performs. when i is 1, the inner for loop runs only one time. when i is 2, inner for loop runs 2 times. Likewise when i is   n   the inner for loop runs n times. So the total number of Execution rounds can be calculated by adding all Execution times in each i values.     Time Complexity       =    n (n+1)                                         2                     ...