Saturday, 25 February 2017

C Program to implement search Operation in Matrix

This question has been already appeared in the interviews of the following companies:-
OLA, MakeMyTrip, Oracle, One97, TinyOwl, Polycom, Visa, Directi, Inmobi, Groupon

Source: geeksforgeeks 

In this question you have given a matrix of n*m order in which every row and column is sorted in increasing order and an element x. You have to find the element x in the given matrix. If the element is present in the matrix then print 1 otherwise print 0.

Note:- You have to find the element in O(m+n) Time Complexity


ALGORITHM

1. set:  element= element to be searched in the matrix
2. for i in range 1 to n   #n is the number of rows
3.      if  element>=matrix[i][0] and element<=matrix[i][m]
4.            for j in range 0 to m   #m is the number of columns
5.                  if element=matrix[i][j]
6.                       return True
7.                  EndIf
8.            EndFor
9.      EndIf
10. EndFor
11. return False
12. Exit    


PROGRAM:

#include <stdio.h>

class matrix_search{
 
   private:
       int matrix[100][100];
       int n,m;
   public:
       void get_data();
       void print();
       bool search(int element);
};
void matrix_search::get_data()
{
   int i,j,element;
   printf("\nEnter the number of rows and columns of matrix: ");
   scanf("%d%d",&n,&m);  /*n rows and m columns */
   for(i=0;i<n;i++)
   {
      for(j=0;j<m;j++)
      {
           scanf("%d",&matrix[i][j]);
      }
   }
}
void matrix_search::print()
{
   int i,j;
   printf("\nInput matrix is: \n");
   for(i=0;i<n;i++)
   {
       for(j=0;j<m;j++)
       {
            printf("%d  ",matrix[i][j]);
       }
       printf("\n");
   }
}
bool matrix_search::search(int element)
{
  int i,j;
     for(i=0;i<n;i++)
     {
           if(element>=matrix[i][0] && element<=matrix[i][m-1] )
           {
                for(j=0;j<m;j++)
                {
                    if(element==matrix[i][j])
                    {
                         return true;
                    }
                }
           }
     }
     return false;
}
int main()
{
 matrix_search ms;
 ms.get_data();
 int element;
 printf("\nEnter the element you want to search in the matrix: ");
    scanf("%d",&element);
    if(ms.search(element))
    {
        printf("\nElement is present in the matrix !");
    }
    else
    {
        printf("\nElement is not present in the marix !");
    }
    return 0;
}


OUTPUT:

 

Wednesday, 22 February 2017

C++ Program to find the Nth node of the Linked List from the End

This question has already appeared in the interveiws of the following questions
Epic Systems, Snapdeal, Monotype Solutions, Adobe, Amazon, Citicorp

Source:- geeksforgeeks

we strongly recommend you to first try this question yourself at geeksforgeeks before seeing the editorial. 


 In the question, you have given a linked list and you have to complete a given method which is taking two arguments head node of the linked list and the n (the node which you have to find) and you have to return the value of that nth node.


The completed function is given below:



/* struct Node
 {
   int data;
   Node* next;
 }; */

/* Should return data of n'th node from the end of linked list */
int getNthFromLast(Node *head, int n)
{
       struct Node *current;
       current=head;
       int count=0;
       while(current!=NULL)
       {
            count++;
            current=current->next;
       }
       if(count<n)
       {
              return -1;
       }
       if(count==n)
       {
           return head->data;
       }
       else
       {
              current=head;
              while(current!=NULL)
              {
                    if(count==n+1)
                    {
                           current=current->next;
                           return current->data;
                    }
                    else
                    {
                        count--;
                        current=current->next;
                    }
              }
       }
      
}

Complete program for finding the nth node from end of the linked list is also given below in  the case you want to understand and try the complete program.


#include <stdio.h>
#include <stdlib.h>
struct node
{
   int data;
   struct node *next;
};

class linklist{
 
   private:
         int data;
         node *head,*current;
   public:
         void get_data();
         int find_node(int n);
         void print();
};
void linklist::get_data()
{
 
      head=NULL;
      current=(struct node*)malloc(sizeof(struct node));
      current->data=8;
      current->next=NULL;
      if(head==NULL)
      {
             head=current;
      }
      current->next=(struct node*)malloc(sizeof(struct node));
      current=current->next;
      current->data=16;
      current->next=(struct node*)malloc(sizeof(struct node));
      current=current->next;
      current->data=25;
      current->next=(struct node*)malloc(sizeof(struct node));
      current=current->next;
      current->data=45;
      current->next=(struct node*)malloc(sizeof(struct node));
      current=current->next;
      current->data=50;
      current->next=NULL;

}
void linklist::print()
{
   current=head;
   while(current!=NULL)
   {
           printf("%d->",current->data);
           current=current->next;
   }
   int n;
   printf("\nEnter the nth node from the end which you want to found: ");
   scanf("%d",&n);
   printf("\n\nThe nth node is: %d",find_node(n));
}
int linklist::find_node(int n)
{
 
       current=head;
       int count=0;
       while(current!=NULL)
       {
            count++;
            current=current->next;
       }
       if(count<n)
       {
              return -1;
       }
       if(count==n)
       {
           return head->data;
       }
       else
       {
              current=head;
              while(current!=NULL)
              {
                    if(count==n+1)
                    {
                           current=current->next;
                           return current->data;
                    }
                    else
                    {
                        count--;
                        current=current->next;
                    }
              }
       }
      
}
int main()
{
   linklist ll;
   ll.get_data();
   ll.print();
   return 0;
}


OUTPUT:

 

C program for merging two sorted arrays in Increasing Order

C program for merging two sorted arrays into one single sorted array in increasing order is given below

ALGORITHM:

1. n1 is the total number of elements in array1
2. n2 is the total number of elements in array2
3. Declare an empty array3 of size n1+n2
3. set i:=1, j:=1, k:=1
4. do while j<n1 && k<n2
5.         if array1[j] < array2[k]
6.              set:  array3[i]=array1[j]
7.              set:  i=i+1
8.              set:  j=j+1
9.         else
10.             set:  array3[i]=array2[k]
11.             set:  i=i+1
12.             set:  k=k+1
13. EndWhile
14. If j=n1+1
15.      do while k<n2
16.             set:  array3[i]=array2[k]
17.             set:  i=i+1
18.             set:  k=k+1
19. Else:
20.      do while j<n1
21.             set:  array3[i]=array1[j]
22.             set:  i=i+1
23.             set:  j=j+1
23. EndIf
24. set: n=n1+n2


Program:

#include <stdio.h>
#include <stdlib.h>
int merge(int n1,int n2);
int arr1[100],arr2[100],arr3[100];
int main()
{
   int n1,n2,n,i;
   printf("\nEnter the total number of elements of array 1: ");
   scanf("%d",&n1);
   printf("\nEnter the elements of array 1: \n");
   for(i=0;i<n1;i++)
   {
          scanf("%d",&arr1[i]);
   }
   printf("\nEnter the total number of elements of array 2: ");
   scanf("%d",&n2);
   printf("\nEnter the elements of array 2: \n");
   for(i=0;i<n2;i++)
   { 
             scanf("%d",&arr2[i]);
   }
   n=merge(n1,n2);
   printf("\n");
   printf("\nElement of the array after merging are:\n");
   for(i=0;i<n;i++)
   {
        printf("%d ",arr3[i]);
   }
   return 0;
}
int merge(int n1,int n2)
{
   int i,j,k,n;
   i=0;
   j=0;
   k=0;
   while(j<n1 && k<n2)
   {
          if(arr1[j]<arr2[k])
          {
                arr3[i]=arr1[j];
                i++;
                j++;
          }
          else
          {
                arr3[i]=arr2[k];
                i++;
                k++;
          }
   }
   if(j==n1)
   {
             while(k<n2)
             {
                   arr3[i]=arr2[k];
                   i++;
                   k++;
             }
   }
   else
   {
          while(j<n1)
          {
                arr3[i]=arr1[j];
                i++;
                j++;
          }
   }
   n=n1+n2;
   return n;


}


OUTPUT:

 
See also: Program for merging two sorted array into one sorted array in non-increasing order.

C Program to Merge two sorted Arrays in non-increasing order

This question has been already appeared in the interviews of following companies:
Juniper, Synopsis, Snapdeal, Brocade, Amdocs, Goldman Sachs, Quickr, Microsoft, Linkedin, Zoho
 

In the question you have given two sorted array and then you have to merge them into single sorted array in non-increasing order.

Source :- geeksforgeeks  

We strongly recommend you to try this program on geeksforgeeks before seeing the solution.

Input:-

In inputs, First line contains T, which is denoting the number of test cases.
First line of each test case contains two space separated integers X and Y, denoting the size of the two sorted arrays.
Second line of each test case contains X space separated integers, denoting the first sorted array P.
Third line of each test case contains Y space separated integers, denoting the second array Q.


Ouput:-

For each test case, print (X + Y) space separated integer representing the merged array.

Constraints:- 

1<T<=100
1<=X,Y<=50000
0<=Xi,Yi<=10^9 





#include <stdio.h>
#define MAX 50000
void merge(int n1,int n2);
int arr1[MAX],arr2[MAX];
int main() {
    
    int n1,n2,i,j,t;    /*t is the number of test cases */
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
          scanf("%d%d",&n1,&n2);   /*n1 & n2 are the number of elements in array1 and array2 */
          for(j=0;j<n1;j++)
          {
              scanf("%d",&arr1[j]);
          }
          for(j=0;j<n2;j++)
          {
              scanf("%d",&arr2[j]);
          }
          merge(n1,n2);
    }
    return 0;
}
void merge(int n1,int n2) /*here we are calling to method merge to merging both arrays in sorted order */
{
    int n,i=0,j=0,k=0,arr3[n1+n2];
    while(j<n1 && k<n2)
    {
          if(arr1[j]>arr2[k])
          {
               arr3[i]=arr1[j];
               i++;
               j++;
          }
          else
          {
               arr3[i]=arr2[k];
               i++;
               k++;
          }
    }
    if(j>=n1)
    {
        while(k<n2)
        {
            arr3[i]=arr2[k];
            i++;
            k++;
        }
    }
    else
    {
        while(j<n1)
        {
            arr3[i]=arr1[j];
            i++;
            j++;
        }
    }
    n=n1+n2;
    for(i=0;i<n;i++)
    {
        printf("%d ",arr3[i]);
    }
    printf("\n");
}


OUTPUT:


See also: - Algorithm Implementation of merging two sorted array in C in increasing Order

Tuesday, 21 February 2017

C Program To Find The Middle Element of The Linked List

This Question has been appeared in the interview of following companies:

Payu, Samsung, MAQ software, Microsoft, Nagarro, Netskope, Adobe, Amazon, Flipkart, GE, IgniteWorld

Source: geeksforgeeks

In the exact question you have given a linked in which you have to find the middle element of the Linked List. For example if the Linked List is 1->2->5->7->9 then the middle element is 5. 

In the case of even node, their will be two elements in the middle one and you have to return the second middle element of the Linked List. For example if the Linked List is 1->3->4->5->6->8 then the middle element is 5. 

In the exact question you don't have to write the complete program, you just have to complete a methode which will accept only one argument (i.e head of the Linked List) and you have to return the middle element. There are several test cases and this method is individually called for each test case. In the case of empty Linked List you have to return -1.

The function which you have to complete is given below.
 


struct Node {
    int data;
    Node* next;
}; */

/* Should return data of middle node. If linked list is empty, then  -1*/
int getMiddle(Node *head)
{
     /* write your code here */

}


ALGORITHM

1.  set count:=0
2.  set current:=head #pointer contains memory address of head node of the Linked list
3.  while current!=NULL
4.        set  count:=count+1
5.        set  current=current->next
6.  EndWhile
7.  set n=count/2
8.  if head=NULL
9.        return -1
10. Else
11.       set count2=0
12.       set current:=head
13.       while current!=NULL
14.              if count2=n
15.                     return current->data
16.              else
17.                     set count2=count2+1
18.                     set current=current->next
19.              End IfElse
20.       EndWhile
21. Exit


The completed function is

/* Link list Node 
struct Node {
    int data;
    Node* next;
}; */

/* Should return data of middle node. If linked list is empty, then  -1*/
int getMiddle(Node *head)
{
     struct Node *current;
     int count=0;
     current=head;
     if(head==NULL)
     {
          return -1;
     }
     else
     {
         current=head;
         while(current!=NULL)
         {
               count++;
               current=current->next;
         }
         int n=count/2;
         int count2=0;
         current=head;
         while(current!=NULL)
         {
              if(count2==n)
              {
                  return current->data;
              }
              else
              {
                  count2++;
                  current=current->next;
              }
         }
     }

}


I have also coded complete program incase you want to write this program from beginning.

#include <stdio.h>
#include <stdlib.h>
struct node{
  int data;
  struct node *next;
}*root=NULL,*temp;
void insert(int item);
int middle_Element(struct node *head);
void print();
int main()
{
    
    int choice,item;
    do
    {
      printf("\nEnter the element you want to insert in the Linked List: ");
             scanf("%d",&item);
             insert(item);
             printf("\nDo you want to coninue[1/0]: ");
             scanf("%d",&choice);
    }while(choice==1);
    printf("\n\nThe original Linked List is:\n");
    print();
    printf("\nMiddle element of the Linked List is: %d",middle_Element(root));
    return 0;
}
void insert(int item)
{
    if(temp==NULL)
    {
         temp=(struct node*)malloc(sizeof(struct node));
         temp->data=item;
         temp->next=NULL;
         root=temp;
    }
    else
    {
         temp->next=(struct node*)malloc(sizeof(struct node));
         temp=temp->next;
         temp->data=item;
         temp->next=NULL;
    }
}
void print()
{
    struct node *current;
    current=root;
    while(current!=NULL)
    {
         printf("%d->",current->data);
         current=current->next;
    }

}
int middle_Element(struct node *head)
{
  struct node *current;
     int count=0;
     current=head;
     if(head==NULL)
     {
          return -1;
     }
     else
     {
         current=head;
         while(current!=NULL)
         {
               count++;
               current=current->next;
         }
         int n=count/2;
         int count2=0;
         current=head;
         while(current!=NULL)
         {
              if(count2==n)
              {
                  return current->data;
              }
              else
              {
                  count2++;
                  current=current->next;
              }
         }
     }
}


OUTPUT:

 

Implementation of Linear Search Algorithm in C++

In computer science, linear search or sequential search is a method for finding a target value within a list or Array. It sequentially checks each element of the list for the target value until a match is found or until all the elements have been searched.

Worst Case Time Complexity is: O(n)


Algorithm:
1. set: item=elemet to be found in the array
2. set: flag=0
3. For i in range 1 to n
4.         if array[i]=item
5.                  set: flag=1
6.                  set: loc=i
7.                  break
8.         EndIf
9. EndFor
10.If flag=1
11.     print loc
12.Else
13.     print "Element is not found"
14.Exit


Program:-
#include <stdio.h>
#define MAX 100

class linearsearch{
 
    private:
          int array[MAX],n;
    public:
          void get_data();
          int search_element();
          void print();
};
void linearsearch::get_data()
{
   printf("\nEnter the total number of elements: ");
   scanf("%d",&n);
   printf("\nEnter the elements of array: \n");
      for(int i=0;i<n;i++)
      {
             scanf("%d",&array[i]);
      }
}
int linearsearch::search_element()
{
   int loc,item,flag=0;
   printf("\nEnter the element to be searched: ");
   scanf("%d",&item);
   for(int i=0;i<n;i++)
   {
          if(item==array[i])
          {
                   flag=1;
                   loc=i+1;
                   break;
          }
   }
   if(flag==1)
   {
        return loc;
   }
   else
   {
        return flag;
   }
}
void linearsearch::print()
{
   printf("\nThe elements of array are: \n\n");
   for(int i=0;i<n;i++)
   {
         printf("%d ",array[i]);
   }
}
int main()
{
   linearsearch ls;
   ls.get_data();
   ls.print();
   int loc=ls.search_element();
   if(loc==0)
   {
           printf("\nElement is not present in the array");
   }
   else
   {
           printf("\nElement is found at %d",loc);
   }
   return 0;
}


Output:-

 

C Program to Segregate Even and Odd Nodes in a Linked List

This Question has appeared in the interview of Amazon

Question was like this 

Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.

Input

The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,N is the number of elements in Linked List.
The second line of each test case contains N input,elements in Linked List.


Output 

Print the all even numbers then odd numbers in the modified Linked List.

Constraints 

1 ≤ T ≤ 100
1 ≤ N ≤ 100
1 ≤ size of elements ≤ 1000
 


Example:-

Input
3
7
17 15 8 9 2 4 6
4
1 3 5 7
7
8 12 10 5 4 1 6

 
Output

8 2 4 6 17 15 9
1 3 5 7
8 12 10 4 6 5 1


C Program is given below to segregate the nodes in the same manner given above

 Algorithm:
…1) First Set the pointer to the last node.
…2) Then move all the odd nodes to the end of Linked List
……..a) Consider all odd nodes before the first even node and move them to end.
……..b) Change the head pointer to point to the first even node.
……..b) Consider all odd nodes after the first even node and move them to the end.



#include <stdio.h>
#include <stdlib.h>
struct node
{
     int data;
     struct node *next;
}*head=NULL,*temp,*endnode;
void insert(int item,int j,int n);
void segregate();
void print();
int main() {
  
    int n;
    int item,i,t,j;
    scanf("%d",&t);
    for(i=0;i<t;i++)
    {
         head=NULL;
         endnode=NULL;
         scanf("%d",&n);
         for(j=0;j<n;j++)
         {
             scanf("%d",&item);
             insert(item,j,n);
         }
         
         segregate();
           print();
         printf("\n");
    }
    return 0;
}
void insert(int item,int j,int n)
{
      if(head==NULL)
      {
           temp=(struct node*)malloc(sizeof(struct node));
           temp->data=item;
           temp->next=NULL;
           head=temp;
      }
      else
      {
          temp->next=(struct node*)malloc(sizeof(struct node));
          temp=temp->next;
          temp->data=item;
          temp->next=NULL;
          if(j==n-1)
          {
                 endnode=temp;
          }

      }
}
void segregate()
{
     struct node *current,*prev=NULL;
     struct node *end=endnode;
     temp=NULL;
     current=head;
     while(current!=NULL)
     {
           if(current->data%2!=0)
           {
                if(current->next==NULL)
                {
                    return;
                }
                else
                {
                    current=current->next;
                }
           }
           else
           {
                 break;
           }
     }
     current=head;
     while(current!=end && current->data%2!=0)
     {
            temp=current;
            current=current->next;
            endnode->next=temp;
            endnode->next->next=NULL;
            endnode=endnode->next;
            
     }
     head=current;
     if(current->data%2==0)
     {
             while(current!=end)
             {
                    if(current->data%2==0)
                    {
                           prev=current;
                           current=current->next;
                    }
                    else
                    {
                           temp=current;
                           prev->next=current->next;
                           current=current->next;
                           endnode->next=temp;
                           endnode->next->next=NULL;
                           endnode=endnode->next;
                    }
             }
     }
     else
     {
          prev=current;
     }
     if(endnode!=end && end->data%2!=0)
     {
          prev->next=end->next;
          end->next=NULL;
          endnode->next=end;
     }
     
}
void print()
{
     temp=head;
     while(temp!=NULL)
     {
         printf("%d ",temp->data);
         temp=temp->next;
     }
}


Output

 
Source: geeksforgeeks

Thursday, 16 February 2017

C Program to search an element in Binary Search Tree (BST) | Non Recursive Approach


Binary Search Tree (BST) which is also called as  ordered or sorted binary tree, is a node based binary tree data structure where each node has a comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in that node’s left subtree and smaller than the keys in all nodes in that node’s right sub-tree. Each node has only two Childs. The left sub-tree contains only nodes with keys less than the parent node; the right sub-tree contains only nodes with keys greater than the parent node.

Average search time complexity for BST is O(logn).

ALGORITHM:

SEARCH( current, item)
If   root:=NULL
        print "Tree is empty"
        return False;
If   root.data:=item
        return True
If   item<root.data
       current=current.left
Else
       current=current.right
While current!=NULL
       if  item:=current.data
              return True
      if   item<current.data
              current=current.left
      else
              current=current.right
EndWhile
Return

           


PROGRAM:
 
#include <stdio.h>
#include <stdlib.h>
struct node
{
   int data;
   struct node *left,*right;
}*root=NULL,*temp;
void insert(int item);
void create(int item);
int search_element(struct node *t,int item,int flag);
void search(struct node *t);
void inorder(struct  node *t);
int main()
{
   int ch,item,choice,flag;
   do
   {
       flag=0;
        printf("\n--Main Menu--");
           printf("\n1. Insert node");
           printf("\n2. Search node");
           printf("\n3. Inorder Traversal");
           printf("\nEnter your choice: ");
           scanf("%d",&choice);
           switch(choice)
           {
               case 1: printf("\nEnter the element you want to insert: ");
                       scanf("%d",&item);
                       insert(item);
                       break;
               case 2: printf("\nEnter the element you want to search: ");
                       scanf("%d",&item);
                       flag=search_element(root,item,flag);
                       if(flag==0)
                       {
                            printf("\n\nElement is not present in the tree !");

                       }
                       else
                       {
                            printf("\n\nElement is present in the array");
                       }
                       break;
               case 3: printf("\n\nThe root element is: %d\n",root->data);
                       inorder(root);
                       break;
               default: printf("\nError, you have entered the wrong choice !");

           }
           printf("\nDo you want to continue[1/0]: ");
           scanf("%d",&ch);
   }while(ch==1);
   return 0;
}
void insert(int item)
{
  create(item);
  if(root==NULL)
  {
      root=temp;
  }
  else
  {
      search(root);
  }
}
void create(int item)
{
  temp=(struct node*)malloc(sizeof(struct node));
  temp->data=item;
  temp->left=temp->right=NULL;
}
void search(struct node *t)
{
   if(temp->data<t->data && t->left!=NULL)
   {
         search(t->left);
   }
   else if(temp->data<t->data && t->left==NULL)
   {
         t->left=temp;
   }
   else if(temp->data>t->data && t->right!=NULL)
   {
         search(t->right);
   }
   else if(temp->data>t->data && t->right==NULL)
   {
         t->right=temp;
   }
}
void inorder(struct node *t)
{
   if(root==NULL)
   {
        printf("\nTree is empty !");
        return;
   }
   if(t->left!=NULL)
   {
        inorder(t->left);
   }
   printf("%d ",t->data);
   if(t->right!=NULL)
   {
        inorder(t->right);
   }
}
int search_element(struct node *t,int item, int flag)
{
   if(root==NULL)
   {
        return flag;
   }
   if(item==root->data)
   {
        flag=1;
        return flag;
   }
   if(item<t->data)
   {
        t=t->left;
   }
   else 
   {
        t=t->right;
   }
   while(t!=NULL)
   {
         if(t->data==item)
         {
             flag=1;
             return flag;
         }
         if(item<t->data)
         {
             t=t->left;
         }
         else
         {
             t=t->right;
         }
   }
   return flag;
}


OUTPUT:


Output of searching an element in the formed tree:  


Run this code yourself.

Wednesday, 15 February 2017

Implementation of Bubble Sort Algorithm in C++

The most simplest sorting algorithm is Bubble sort Algorithm. This is an comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order.

Note:-
This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2) where n is the number of items.

ALGORITHM :

Set i:=1
For i=1 to i<= n-1:
         For j=1 to j<=n-1-i
                   If arr[j]>arr[j+1]
                             Set swap:=arr[j]
                                    arr[j]=arr[j+1];
                                    arr[j+1]=swap
                   End If 
         End For
End For
Exit

PROGRAM:
 
#include <stdio.h>
#define MAX 100

class BubbleSort
{
	 private:
	     int arr[100],n;
	 public:
	     void get_data();
	     void sort();
	     void print();
};
void BubbleSort::get_data()
{
	  printf("\nEnter the total number of elements: ");
	  scanf("%d",&n);
	  printf("\nEnter the elements of the array: \n");
	  for(int i=0;i<n;i++)
	  {
	       scanf("%d",&arr[i]);
	  }
}
void BubbleSort::sort()
{
	  for(int i=0;i<n-1;i++)
	  {
	       for(int j=0;j<n-i-1;j++)
	       {
	             if(arr[j]>arr[j+1])
	             {
	                   int swap=arr[j];
	                   arr[j]=arr[j+1];
	                   arr[j+1]=swap;
	             }
	       }
	  }
}
void BubbleSort::print()
{
	  printf("\nThe updated array is: \n");
	  for(int i=0;i<n;i++)
	  {
	        printf("%d ",arr[i]);
	  }
}
int main()
{
	  BubbleSort bs;
	  bs.get_data();
	  bs.print();
	  bs.sort();
	  bs.print();
	  return 0;
}

OUTPUT:

 

C Program to Insert an Element in Array at any position

Insertion operation is used to insert a new element at any position into one-dimensional array.
In order to insert a new element into an array. first, you have to create space for the new Element. Suppose there is N elements already stored in the array and then a task is given to you to store the element X at position J, then first you have to create the space for the X element by moving all the element next to their position.


PROGRAM:

#include <stdio.h>
#define MAX 100
int n,arr[MAX];
void insert(int item,int position);
void print();
int main()
{
	  int i,item,position;
	  printf("\nEnter the total number of elements: ");
	  scanf("%d",&n);
	  printf("\nEnter the elements of the array: \n");
	  for(i=0;i<n;i++)
	  {
	       scanf("%d",&arr[i]);
	  }
      print();
      printf("\nEnter the element you want to insert in the array: ");
      scanf("%d",&item);
      printf("\nEnter the position at which you want to insert the element: ");
      scanf("%d",&position);
      insert(item,position);
      print();
      return 0;
}
void insert(int item,int position)
{
	  int i,j;
	  if(position==n+1)
	  {
	  	   arr[n]=item;
	  }
	  else
	  {
	  	  for(i=n-1;i>=position-1;i--)
	  	  {
	  	  	   arr[i+1]=arr[i];
	  	  	   if(i==position-1)
	  	  	   {
	  	  	   	   arr[i]=item;
	  	  	   }
	  	  }
	  }
	  n=n+1;
}
void print()
{
	  int i;
	  printf("\nThe updated array is: \n");
	  for(i=0;i<n;i++)
	  {
	       printf("%d ",arr[i]);
	  }
}


OUTPUT: Output for insertion at various position