Skip to main content

Posts

Showing posts from December, 2015

BitWise Operators Explained in Short

Bitwise Operators :    Bitwise Operators do not operate on decimal value of the Number. They first convert the numbers into binary value & then they operate on it. Bitwise opeartors are not used with floating type variables. Bitwise AND :-   & Bitwise OR    :-   | Bitwise XOR :-   ^ Bitwise NOT :-   ~ LEFT SHIFT :- << RIGHT SHIFT :- >> Bitwise AND  & Example : N1 = 10 , N2 = 15 N1 & N2 In Binary  :                                                  0 0 1 0 1 0                                                   0 0 1 1 1 1  ...

Find a number is divisible by 3 or not

Method 1 : #include<stdio.h> int main() {     int num =0,sum=0;     printf("Enter A Number\n");     scanf("%d",&num);     while(num!=0){         sum+=num%10;         num/=10;     }     printf("Sum = %d\n",sum);     if(sum%3==0)         printf("divisible by 3\n");     else         printf("Not Divisible by 3\n");     return 0; }

Find Median of two Sorted Arrays in C

#include<stdio.h> int main() {     int A[]={1, 5, 7, 10, 13};     int B[]={11, 15, 23, 30, 45};     int n=sizeof(A)/sizeof(int);     int n1=0,n2=0,k=0,i=0,j=0;     while(k<n+1){         if(A[i]<B[j]){             if(k==n-1){                 n1=A[i];             }             if(k==n){                 n2=A[i];             }             i++;             k++;       ...

Activity Selection Problem in Short

Activity Selection Problem is used extensively. Suppose a person is given n activities with their start & finish time. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. Greedy Approach:  set n equal to the max finish time  initialize set of selected activity with a 1    set k=1 loop m=2 to n    if start time of current activity is greater than the finish time of last activity            add the current activity to selected activity           set k=m

Kruskal's Algorithm Steps in Short

Kruskal's Algorithm Steps in Short  to find out Minimum Spanning Tree 1. Sort the Edges in The Non-Decreasing Order. 2. Create as many Disjoint Set as many Vertices intially. 3. Pick one edge at a time in non-decreasing order & check both the ends of this edge if they are in the same disjoint set or different, if they are in the different disjoint set , merge them together and save the edge, else ignore that edge.

Merge Sort in C

#include<stdio.h> void merge(int arr[],int l,int mid,int r){     int n1=mid-l+1;     int n2=r-mid;     int L[n1],R[n2];     int i=0,j=0,k=0;     for(i=0;i<n1;i++){         L[i]=arr[l+i];     }     for(j=0;j<n2;j++){         R[j]=arr[mid+1+j];     }     i=0;     j=0;     k=l;     while(i<n1&&j<n2){         if(L[i]<=R[j]){             arr[k]=L[i];             i++;         }         else{             arr[k]=R[j];       ...

Insertion Sort in C

#include<stdio.h> int main() {     int arr[]={8,5,7,4,3,0};     int n = sizeof(arr)/sizeof(int);     int key=0,i=0,j=0;     for(i=1;i<n;i++){         key=arr[i];         j=i-1;         while(j>=0 && arr[j]>key){             arr[j+1]=arr[j];             j--;         }         arr[j+1]=key;     }      for(i=0;i<n;i++){         printf("%d\t",arr[i]);     }     printf("\n");     return 0; }

Bubble Sort in C

#include<stdio.h> int main(){     int arr[]={3,1,0,6,9,4};     int i=0,j=0,temp=0;     int num=sizeof(arr)/sizeof(int);     for(i=0;i<num-1;i++){         for(j=0;j<num-i-1;j++){             if(arr[j]>arr[j+1]){                 temp=arr[j];                 arr[j]=arr[j+1];                 arr[j+1]=temp;             }         }     }     for(i=0;i<num;i++){         printf("%d\t",arr[i]);     }     printf("\n");  ...

Binary Search in C

#include<stdio.h> int BinarySearch(int arr[],int l, int r,int num){     printf("Entered Binary Search Function\n");     int mid=0;     printf("%d %d\n",l,r);     if(l<=r){         mid=l+(r-l)/2;         printf("%d\n",mid);         if(arr[mid]==num){             return 1;         }         else if(arr[mid]<num){             return BinarySearch(arr,mid+1,r,num);         }         else{             return BinarySearch(arr,l,mid-1,num);         }     }   ...

Selection Sort in C

#include<stdio.h> int main() {     int arr[]={5,3,7,9,6,2,8,10,21,14};     int i=0, min=0,temp=0,j=0;     for(i=0;i<9;i++){         min=i;         for(j=i+1;j<10;j++){             if(arr[min]>arr[j]){                 min=j;             }         }         temp=arr[i];         arr[i]=arr[min];         arr[min]=temp;         printf("%d\n",arr[i]);     }     for(i=0;i<10;i++)         printf("%d\t",arr[i]);     printf("\n"); ...