Sunday, 13 August 2017

Merge Sort

#include<stdlib.h>
#include<stdio.h>
void merge(int arr[], int l, int m, int r)
{
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;
    // create temp arrays
    int L[n1], R[n2];
    // Copy data to temp arrays L[] and R[]
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1+ j];
     // Merge the temp arrays back into arr[l..r]
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
     // Copy the remaining elements of L[], if there are any
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }
    // Copy the remaining elements of R[], if there are any
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
// l is for left index and r is right index of the sub-array of arr to be sorted
void merge_Sort(int arr[], int l, int r)
{
    if (l < r)
    {
        // Same as (l+r)/2, but avoids overflow for large l and h
        int m = l+(r-l)/2;
 
        // Sort first and second halves
        merge_Sort(arr, l, m);
        merge_Sort(arr, m+1, r);
 
        merge(arr, l, m, r);
    }
}
// Function to print an array
void print_Array(int A[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", A[i]);
    printf("\n");
}

int main()
{
    int arr[] = {12, 11, 13, 5, 6, 7};
    int arr_size = sizeof(arr)/sizeof(arr[0]);
 
    printf("Given array is \n");
    print_Array(arr, arr_size);
 
    merge_Sort(arr, 0, arr_size - 1);
 
    printf("\nSorted array is \n");
    print_Array(arr, arr_size);
    return 0;
}

Insertion Sort

#include <stdio.h>
#include <math.h>
Function to sort an array using insertion sort
void insertion_Sort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;
Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
A utility function ot print an array of size n
void print_Array(int arr[], int n)
{
int i;
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
 
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
insertion_Sort(arr, n);
print_Array(arr, n);
return 0;
}

Bubble Sort

#include <stdio.h>

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
A function to implement bubble sort
void bubble_Sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}

int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubble_Sort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;

Selection Sort

#include <stdio.h> 
void swap(int *xp, int *yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
void selectionSort(int arr[], int n)
{
    int i, j, min_index;
     One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++)
    {
         Find the minimum element in unsorted array
        min_index = i;
        for (j = i+1; j < n; j++)
          if (arr[j] < arr[min_index])
            min_index = j;
         Swap the found minimum element with the first element
        swap(&arr[min_index], &arr[i]);
    }
}
 Function to print an array
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
int main()
{
    int arr[] = {64, 25, 12, 22, 11};
    int n = sizeof(arr)/sizeof(arr[0]);
    selectionSort(arr, n);
    printf("Sorted array: \n");
    printArray(arr, n);
    return 0;
}

Write a C program to find whether a given matrix is an upper triangular matrix or not.

                                    UPPER TRIANGULAR MATRIX
                                        
                                              C-Solution
#include<stdio.h>
#include<stdlib.h>
int **create(int r,int c)
{
int **mat,i;
mat=(int **)malloc(r*sizeof(int *));
for(i=0;i<r;i++)
{
*(mat+i)=(int *)malloc(c*sizeof(int ));
}
return mat;
}
int **getelement(int **m,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",*(m+i)+j);
}
}
return m;
}
void exec(int **m,int r,int c)
{
int i,j,c1=1;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if((j<i)&&*(*(m+i)+j)!=0)
{
c1=0;
}
}
}
if(c1==1)
{
printf("yes");
}
else
{
printf("no");
}
}
int main()
{
int **m;
int r;
scanf("%d",&r);
m=create(r,r);
m=getelement(m,r,r);
exec(m,r,r);
return 0;
}

Saturday, 22 July 2017

Top 100 Networking Interview Questions click here
Basic Networking Interview Questions with answer  click here
Top 500 Java Interview Questions and Answers click here

Friday, 21 July 2017

Write program for Lower Triangula Matrix

Lower triangle matrix
Write a program to print the following pattern for a given integer.

Sample Input 1:
3
Sample Output 1:

1
6 2
5 4 3 
                                          C-Solution
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,i,c=1,j=0,ans;
scanf("%d",&n);
int a[n][n];
if(n%2==0){ans=(pow(n,2)/2)+n/2;}
else
ans=(n-(n/2))*n;

int m=n-1,p=0,l=0,k;
while(c-1<ans)
{
for(i=l+p;i<=m&&c-1<ans;i++)
a[i][i-l]=c++;
++l;
for(j=m-l;j>=p&&c-1<ans;j--)
a[m][j]=c++;
for(k=m-1;k>l+p-1&&c-1<ans;k--)
a[k][p]=c++;
m--;p++;
}
for(i=0;i<n;i++)
{ for(j=0;j<=i;j++)
{ printf("%d ",a[i][j]);}
printf("\n");
}
return 0;
}

Home Page

 Zoho Interview Questions Alternate Sorting   C program for Circular Pattern C program for Spiral Matrix C program to print ...