Tuesday 30 August 2022

Fastest Method to Convert Binary Number into Decimal Number

 




















#codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net #binarytodecimal #numbersystem







Sunday 21 August 2022

Increment & Decrement Operators

  • Increment (++) operator is used to increase the value of a variable by 1.

  • Decrement (--) operator is used to decrease the value of a variable by 1.

  • Both the operators are unary i.e. they only require one operand.

  • There are two forms in which ++ and - can be used:
    1.  Prefix
    2. Postfix

  • Prefix: In this form operator is written before the operand: ++a, --b.
  • Postfix: In this form operator is written after the operand: at+, b-.
  • If there is no assignment then prefix and postfix will give same result.
    Eg:




















In both cases a = 11 and b = 19


  • If there is assignment then prefix and postfix may give different result.





    x = 11 a = 11
    y = 21 b = 21

    x = 10 a = 11
    y = 20 b = 21



    Prefix


    int a =10, x;

    x=++a;

    It is calculated as a two step process:

    Step 1:a=a+1:// a will become 11

    Step 2:x=a;// x will become 11

    In case of prefix rule is:

    First increment/decrement is performed then assignment is performed.


    Postfix

    int a=10, x;

    x=a++;

    It is calculated as a two step process:


    Step 1:x=a;//
    x will become 10

    Step 2:a=a+1;//
    a will become 11

    In case of postfix rule is:

    First assignment is performed then 
    increment/decrement is performed.

    In case of following for loops both postfix & prefix will give same result:




     
    Both loops will print 1 to 5


    In case of printf functions result may vary:


    It will print a = 11
    It will print a = 10
    Finally a will be 11
    in both case.



    #codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net

Sunday 14 August 2022

Arithmetic Operators in C Programming

  • Arithmetic operators are used to perform basic arithmetic operations.

  • There are five Arithmetic Operators used in C Language:

    Addition(+), Subtraction(-), Multiplication(*), Division(/) & Modulus(%).

  • Modulus gives us remainder of two operands and can only be applied on integer data.

  • Addition, subtraction, multiplication and division can be applied on both integer as well as real data.

  • Arithmetic operators are binary in nature i.e. they require at least two operands to perform operation.

  • Arithmetic Operators can be Applied on both +ve as well as -ve numbers.

  • Symbols used for Arithmetic operators are:




  • Arithmetic Operation Eg:






  • Applying '/' & '%' on -ve numbers :





  • On applying '%' to -ve numbers, sign of numerator will be assigned to the result.





Program


Output















#codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net

Friday 12 August 2022

Selection Sort Technique, Complexity & Program

  •  Suppose we want to arrange numbers of a list in ascending order.


  • We will divide our list in two parts: Sorted List & Unsorted List.

  • Initially all the elements are considered as Unsorted.


  • Now we will find minimum element from the unsorted list & swap it with the first element of unsorted list.








  • Now "1" will become part of Sorted List & we will repeat our process for remaining unsorted part.









  • Again we will find minimum element from unsorted list & swap it with the first element of unsorted list.











  • "2" will become part of sorted list & process will be repeated for remaining unsorted list.










  • Again we will find minimum element from unsorted list & swap it with first element of unsorted list.










  • "3" will become part of sorted list & we will repeat our process for remaining unsorted list.










  • Again we will find minimum element from unsorted list & swap it with first element of unsorted list.











  • As "5" is minimum element & it is already at its correct position so, no swap is required.



  • Similarly the remaining elements of unsorted list will become part of sorted list.







    Program

    //Selection Sort
    #include<iostream.h>
    #include<conio.h>
    int selection_sort(int a[],int n)
    {
    int i,j,t,u;
    for(i=0;i<n-1;i++)
    {
    u=i;//index of first element of unsorted list
    for(j=i+1;j<n;j++)
    {
    if(a[j]<a[u])
    u=j;//updated when
       //element less than minimum is found
    }
    if(i!=u)//no need to swap
    {       //if element is already at correct position
    t=a[i];
    a[i]=a[u];
    a[u]=t;
    }
    }
    }
    void main()
    {
    int a[]={12,5,8,2,1,7,3},i;
    clrscr();
    selection_sort(a,7);
    cout<<"Sorted Array"<<endl;
    for(i=0;i<7;i++)
    {
    cout<<a[i]<<endl;
    }

    getch();
    }








    Selection Sort technique, complexity & program with minimum number of swaps. https://www.instagram.com/reel/Cg6So4Uhwza/?igshid=YmMyMTA2M2Y= #datastructure #sorting #searching #minimumelement #bestcoding #goodcode #leetcode #aws #python #cprogram #cprogramming #bca #mca #it #cs #complexity #clanguage #java #javascripSot #DeveloperMode Selection Sort Program Algorithm to sort integers Sorting of Integer numbers

Monday 1 August 2022

Binary Search : Technique, Program & Complexity

Technique

  • Let us say we have a book of "600" pages and we want to read page number "338", one method is to apply linear search and turn pages one by one which is very time consuming and inefficient.




  • Another way is that we can randomly open a page from book which may be near to 338, suppose we opened page number 420. Now we will only search our page in the left part of the book i.e. between 1 to 420.




  • Again say we got to page number 330 so, now we will search between page 330 to 420.

  • This way we can reach our page in a much efficient way.

  • Binary Search will work in a similar manner, which can only be applied on sorted data.

  • In this method we will compare the item to be searched with the item at middle index of array.





  • Say "l" is lower bound & "r" is upper bound of array, so middle will be calculated as        mid = ( l + r ) / 2.

  • Then we will compare our item with "a[mid]" element of our Array as follows:

            if ( item == a[mid] )

            return mid;

            else if ( item > a[mid] ) 

            l = mid + 1;

            else 

            r = mid - 1;

  • If item == a[mid] i.e. item is found then, we will return "mid" and our search is successful.



  • If  item > a[mid] i.e. "item" is greater than "a[mid]" then we set "l" to the element on the right of "mid" & "r" remains at its position.

  • It means item exist between "mid + 1" & "r".




  • Now, we will again calculate "mid" & repeat the process.

  • If item < a[mid] i.e. "item" is less than "a[mid]" then we set "r" to the element on the left of "mid". "l" remains at its position.
  • It means item exist between "l" & "mid - 1".




  • We will stop our search when "l" becomes greater than "r". It means search is unsuccessful and we will return -1.

Program

Example

  • Suppose we want to find location of element "17" in Array given below:




  • We will take three variables "l" , "r", & "mid". Initially we will take l=0 & r=6. (Lower Bound=0 & Upper Bound=6)

  • Then we will calculate index of middle element of our Array as ( l + r ) / 2 and store the result in mid.
    mid=( l + r ) / 2 = ( 0 + 6 ) / 2 = 3.


  • Now we will compare our item "17" with a[mid] i.e. a[3], Both are not equal.




  • So, we will check whether our item "17" is greater than a[3] or not. As "17" is greater than "10". So, "17" might exist between "mid + 1" & "r".

  • It will reduce our search bracket. Now we will set "l = mid + 1" & r will remain unchanged, i.e. "l = 4" & "r = 6".


  • Now again we will calculate mid = ( l + r )/2 = ( 4 + 6 )/2 = 5.





  • Now we will compare our item "17" with a[mid] i.e. a[5], Both are equal and our search is successful so, our algorithm will return "mid" i.e. "5"

Let us take another example

  • Suppose we want to find location of item "2" in Array given below:


  • Again we will set l=0 & r=6. (Lower Bound=0 & Upper Bound=6)

  • Set mid = ( l + r ) / 2 = ( 0 + 6 ) / 2 = 3.



  • Now we will compare our item "2" with a[mid] i.e. a[3], Both are not equal.





  • So, we will check whether our item "2" is greater than a[3] or not. As "2" is not greater than "10". So, "2" is less than a[mid] and "2" might exist between "l" & "mid-1".

  • It will reduce our search bracket. Now we will set "r = mid - 1" & "l" will remain unchanged, i.e. "l = 0" & "r = 2".



  • Now again we will calculate mid=( l + r )/2 = ( 0 + 2 )/2 = 1.




  • We will compare our item "2" with a[mid] i.e. a[1]. Again both are not equal.

  • So, we will check whether our item "2" is greater than a[1] or not. Again "2" is not greater than "5". It means "2" is less than a[mid] and "2" might exist between "l" & "mid-1".



  • It will reduce our search bracket. Now we will set "r = mid - 1" & "l" will remain unchanged, i.e. "l = 0" & "r = 0".




  • We will calculate mid = ( l + r ) / 2 = ( 0 + 0 ) / 2 = 0.

We will compare our item "2" with a[mid] i.e. a[0]. As both are equal so, our algorithm will return mid i.e. 0.





Binary Search Complexity
  • In Best case Binary Search will find the location of item in given array in only 1 comparison so, it is O(1). .
  • In Worst case at each iteration of Binary Search the array size will be reduced to its half.
  • At iteration 1, size of array = n
  • At iteration 2, size of array = n/2
  • At iteration 3, size of array = n/4
  • At iteration k, size of array = 1 .i.e. n/2^k = 1 => n = 2^k => k=log(n)
  • So, in Worst Case Complexity of Binary Search is O(log(n).
  • In Average case of Binary Search item could be found in: 1 comparison or 2 comparisons or 3 comparisons.. or log(n) comparisons where log(n) = m.
  • So, Total comparisons (1+2+3+..+m)
  • Total no. of cases = m
  • Average Comparisons = (1+2+3+...+m)/m where m = log(n). = m ( m + 1 ) / 2m = ( m + 1 ) / 2 = ( log(n) + 1 ) / 2 .
  • So, Average Complexity of Binary Search is O(log(n)).