Pages

Wednesday 25 December 2019

HEAP SORT

Heaps:
A heap is a specific tree based data structure in which all the nodes of tree are in a specific order. Let’s say if X is a parent node of Y, then the value of X follows some specific order with respect to value of Y and the same order will be followed across the tree.

There can be two types of heap:

Max Heap: In this type of heap, the value of parent node will always be greater than or equal to the value of child node across the tree and the node with highest value will be the root node of the tree.

Min Heap: In this type of heap, the value of parent node will always be less than or equal to the value of child node across the tree and the node with lowest value will be the root node of tree.



1) Heap Sort:

We can use heaps in sorting the elements in a specific order in efficient time.
Let’s say we want to sort elements of array Arr in ascending order. We can use max heap to perform this operation.

Processing:
1) Initially we will build a max heap of elements in Arr.

2) Now the root element that is Arr[ 1 ] contains maximum element of Arr. After that, we will exchange this element with the last element of Arr and will again build a max heap excluding the last element which is already in its correct position and will decrease the length of heap by one.

3) We will repeat the step 2, until we get all the elements in their correct position.

4) We will get a sorted array.
Example on Max Heap




Processing:
Step 1: 8 is swapped with 5.
Step 2: 8 is disconnected from heap as 8 is in correct position now and.
Step 3: Max-heap is created and 7 is swapped with 3.
Step 4: 7 is disconnected from heap.
Step 5: Max heap is created and 5 is swapped with 1.
Step 6: 5 is disconnected from heap.
Step 7: Max heap is created and 4 is swapped with 3.
Step 8: 4 is disconnected from heap.
Step 9: Max heap is created and 3 is swapped with 1.
Step 10: 3 is disconnected.




After building max-heap, the elements in the array Arr will be:

// Java program for implementation of Heap Sort
class rkheapsort
{
// Driver program
public static void main(String args[])
{
int arr[] = {2, 8, 6, 1, 4, 3};
int n = arr.length;

rkheapsort  hs = new rkheapsort();
hs.sort(arr);

System.out.printf("\n Sorted array : ");
hs.printArray(arr);
}
public void sort(int arr[])
{
int n = arr.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
{
System.out.printf("\n Maxheap : ");
    heapify(arr, n, i);
printArray(arr);
}
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// call max heapify on the reduced heap
System.out.printf("\n Placing Maxheap value in its position :");
heapify(arr, i, 0);
printArray(arr);
}
}


// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2*i+ 1; // left = 2*i + 1
int right = 2*i + 2; // right = 2*i + 2

// If left child is larger than root
if (left < n && arr[left] > arr[largest])
largest = left;

// If right child is larger than largest so far
if (right < n && arr[right] > arr[largest])
largest = right;

// If largest is not root
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;

// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
}
}


MERGE SORT

import java.util.*;

class mergesort
 {
  public static void main(String[] args)
  {
   int i,j,k,n,m,g,temp;
   Scanner ms = new Scanner(System.in);
   System.out.println("\nEnter Size Of first Array :");
   m=ms.nextInt();
   int[] a =new int[m]; // Creation of array with given size
   System.out.println("\nEnter unsorted first Array :");
   for(i=0;i<m;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=ms.nextInt();
    }

  for(i=0;i<m-1;i++)
    {
   for(j=0;j<m-1-i;j++)
     {
      if(a[j]>a[j+1])
        {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
      }
    }
   System.out.print("\n Sorted Array A elements :");
   for(i=0;i<m;i++)
     {
      System.out.print( a[i] +" ");
     }
   System.out.println("\nEnter Size Of second Array :");
   n=ms.nextInt();
   int[] b=new int[n]; // Creation of array with given size
   int[] c=new int[m+n]; // Creation of array with given size
   System.out.println("\nEnter unsorted second Array :");
   for(j=0;j<n;j++)
    {
     System.out.printf("\n Element b[%d]  :",j);
     b[j]=ms.nextInt();
    }

  for(i=0;i<n-1;i++)
    {
   for(j=0;j<n-1-i;j++)
     {
      if(b[j]>b[j+1])
        {
         temp=b[j];
         b[j]=b[j+1];
         b[j+1]=temp;
        }
      }
    }
  System.out.print("\n Sorted Array B elements :");
   for(j=0;j<n;j++)
     {
      System.out.print( b[j] +" ");
     }
 
 i=0;j=0;
  for(k=0;k<m+n;k++)
    {
      if(j>=n||(i<m && (a[i] <=b[j])))
         {
          c[k]=a[i];
          i=i+1;
         }
      else if(i>=m||(j<n && (a[i] >b[j])))
         {
      c[k]=b[j];
      j=j+1;
         }
     }
   System.out.print("\n Sorted Array elements :");
   for(k=0;k<m+n;k++)
     {
      System.out.println( c[k] +" ");
    }
 } 
}

output:


QUICK SORT

// Java program for implementation of QuickSort
class qsrk
{
    static void sort(int a[], int p, int r)
    {
        if (p < r)
        {
            int q = partition(a, p, r);
            sort(a, p, q-1);
            sort(a, q+1, r);
        }
    }
    public static void main(String args[])
    {
        int a[] = {5, 7, 6, 1, 3, 2, 4};
        int n = a.length;
        System.out.printf("\n unsorted array :");
        for (int i=0; i<n; i++)
            System.out.print(a[i]+" ");
       
        sort(a, 0, n-1);

        System.out.printf("\n sorted array :");
        for (int i=0; i<n; i++)
            System.out.print(a[i]+" ");
    }

    static int partition(int a[], int p, int r)
    {
        int pivot = a[r];
        int i = (p-1); // index of smaller element
        for (int j=p; j<=r-1; j++)
        {
            // If current element is smaller than the pivot
            if (a[j] < pivot)
            {
                i++;

                // swap a[i] and a[j]
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
                System.out.printf("\n\n exchange a[%d,i]=%d and a[%d,j]=%d ", i,a[j],j,a[i]);
   
            }
        }

        // swap a[i+1] and a[r] (or pivot)
        int temp = a[i+1];
        a[i+1] = a[r];
        a[r] = temp;
        System.out.printf("\n\n exchange a[%d,i+1]=%d and a[%d,r]=%d ", i+1,a[r],r,a[i+1]);
        return i+1;
    }

}

output:





Tuesday 24 December 2019

INSERTION SORT

Insertion sort
Insertion sort is based on the idea that one element from the input elements is consumed in each iteration to find its correct position i.e., the position to which it belongs in a sorted array.
It iterates the input elements by growing the sorted array at each iteration. It compares the current element with the largest value in the sorted array. If the current element is greater, then it leaves the element in its place and moves on to the next element else it finds its correct position in the sorted array and moves it to that position. This is done by shifting all the elements, which are larger than the current element, in the sorted array to one position ahead.

Take array A[]=[7,4,5,2].


Since 7 is the first element has no other element to be compared with, it remains at its position. Now when on moving towards 47 is the largest element in the sorted list and greater than 4. So, move 4 to its correct position i.e. before 7. Similarly with 5, as 7 (largest element in the sorted list) is greater than 5, we will move 5 to its correct position. Finally for 2, all the elements on the left side of 2 (sorted list) are moved one position forward as all are greater than 2 and then 2 is placed in the first position. Finally, the given array will result in a sorted array.
Time Complexity:
In worst case,each element is compared with all the other elements in the sorted array. For N elements, there will be N2 comparisons. Therefore, the time complexity is O(N2).

Program for INSERTION SORT

import java.util.Scanner;
class insertionsort
 {
  public static void main(String[] args)
  {
   int i,j,n,temp=0;
   Scanner is = new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=is.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   System.out.println("Enter unsorted Array :");
   for(i=0;i<n;i++)
    {     System.out.printf("\n Element a[%d]  :",i);
     a[i]=is.nextInt();
    }
   System.out.print("\n Unsorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }

   for(j=0;j<n;j++)
    {
     temp=a[j];
     for(i=j-1;i>=0 && temp<a[i];i--)
       {
        a[i+1]=a[i];
       }
     a[i+1]=temp;
     System.out.printf("\n pass %d element inserted in proper place %d\n\n ",j,temp);
     for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

    }
   System.out.print("\n Sorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
  }
}


output:

Monday 23 December 2019

SELECTION SORT

import java.util.Scanner;

class selectionsort
 {
  public static void main(String[] args)
  {
   int i,j,n,min,temp=0;
   Scanner ss = new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=ss.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   System.out.println("Enter unsorted Array :");
   for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=ss.nextInt();
    }
   System.out.print("\n Unsorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
 
   for(i=0;i<n;i++)
    {
     min=i;
     for(j=i+1;j<n;j++)
      {
        if(a[j]<a[min])
        {
      min=j;
        }
      }
      temp=a[i];
      a[i]=a[min];
      a[min]=temp;
     System.out.printf( "\n Pass-%d  value that is to swap =%d with minimum value =%d",i,temp, a[i]);
    }
   System.out.print("\n Sorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
  }
}

output:

BUBBLE SORT

Sorting Algorithms are concepts that every competitive programmer must know. Sorting algorithms can be used for collections of numbers, strings, characters, or a structure of any of these types.

Bubble sort is based on the idea of repeatedly comparing pairs of adjacent elements and then swapping their positions if they exist in the wrong order.

Assume that A[ ] is an unsorted array of n elements. This array needs to be sorted in ascending order. The pseudo code is as follows:

Lets try to understand the pseudo code with an example: 
A [4] = { 7, 4, 5, 2}

In step 1, 7 is compared with 4. Since 7>47 is moved ahead of 4. Since all the other elements are of a lesser value than 77 is moved to the end of the array.
Now the array is A[]={4,5,2,7}.
In step 2, 4 is compared with 5. Since 5>4 and both 4 and 5 are in ascending order, these elements are not swapped. However, when 5 is compared with 25>2 and these elements are in descending order. Therefore, 5 and 2 are swapped.
Now the array is A[]={4,2,5,7}.
In step 3, the element 4 is compared with 2. Since 4>2 and the elements are in descending order, 4 and 2 are swapped.
The sorted array is A[]={2,4,5,7}.

Complexity:

The complexity of bubble sort is O(n2) in both worst and average cases, because the entire array needs to be iterated for every element.


Program for BUBBLE SORT 

import java.util.Scanner;
class bubblesort
 {
  public static void main(String[] args)
  {
   int i,j,n,temp;
   Scanner bs = new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=bs.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   System.out.println("Enter unsorted Array :");
   for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=bs.nextInt();
    }
   System.out.print("\n Unsorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }
   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
   for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-1-i;j++)
      {
        if(a[j]>a[j+1])
        {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
      }
    }
   System.out.print("\n Sorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
  }
}



output

Thursday 19 December 2019

INTERPOLATION SEARCH

import java.util.Scanner;

class interpolationsearch
 {
  public static void main(String[] args)
  {
   int i,j,start,end,mid,n,item,temp,flag=0;
   Scanner is = new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=is.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   System.out.println("Enter unsorted Array :");
   for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=is.nextInt();
    }
  
   for(i=0;i<n;i++)
    {
     for(j=0;j<n-1;j++)
      {
        if(a[j]>a[j+1])
        {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
      }
    }
   System.out.print("\n Sorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
   System.out.print("\n Enter the element to be searched :");
   item=is.nextInt();
   start=0;end=n-1;
      while(start <= end)
       {
        mid= start + (end - start) * ((item-a[start])/(a[end]-a[start]));
        if(item < a[mid])
          end=mid - 1;
        else if(item > a[mid])
          start=mid + 1;
        else if(item == a[mid])
          {
           System.out.printf("\n Element %d is found at location %d",item,mid+1);
           flag=1;
           break;
          }
       }
   
   if (flag==0)
      System.out.printf("\n Element %d is not in the array",item);
 }
}

output:


BINARY SEARCH

The binary search program can be used it's only a sorted list of elements that means the binary search is used only with the list of elements that already arrange in order

The binary search cannot be used for a list of elements in a random order this process starts comparing the search element if the middle element in the list both are Matched then the result is element found 

otherwise we need to check whether the search element  is smaller or larger than the middle element in the list if the search element smaller then repeat the same process for the left sublist of the middle element if the search element is larger then we repeat the same process for the rights of sublist of the middle element 

repeat this process until we find the set element in the list or until left with sub list of only element and if that element also doesn't match with the search element then the result it as element not found in the list

Let us taken an example

A[5]      =         [79]. [26]. [88]. [11]. [65]

                         A[0]  a[1]  a[2]   a[3]  a[4]

By using bubble sort technique the sorted array

A[5]      =         [11]. [26]. [65]. [79]. [88]

                         A[0]  a[1]  a[2]   a[3]  a[4]

Let 79 search element

Start=0,end=4

Mid=0+4/2=2

IF a[mid]=search element then the result is element found but here a(2)=63=/79(search element)

Now we have to check whether the search element is less than or greater than a[mid]

65<79

A[mid] search element

Now it will repeat the process on the right sub array

Start=mid+1 end=4

=2+1

=3

Mid=start+end/2=3+4/2=7/2=3.5=a(3)=79

Here the search element is equal to the a[mid] of right sub array hence the result will be element 


PROGRAM ON BINARY SEARCH:
import java.util.Scanner;

class binarysearch
 {
  public static void main(String[] args)
  {
   int i,j,start,end,mid,n,item,temp,flag=0;
   Scanner bs = new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=bs.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   System.out.println("Enter unsorted Array :");
   for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=bs.nextInt();
    }
  
   for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-1-i;j++)
      {
        if(a[j]>a[j+1])
        {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
      }
    }
   System.out.print("\n Sorted Array elements :");
   for(i=0;i<n;i++)
     {
      System.out.print( a[i] +" ");
     }

   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
   System.out.print("\n Enter the element to be searched :");
   item=bs.nextInt();
   start=0;end=n-1;
      while(start <= end)
       {
        mid=(start+end)/2;
        if(item < a[mid])
          end=mid - 1;
        else if(item > a[mid])
          start=mid + 1;
        else if(item == a[mid])
          {
           System.out.printf("\n Element %d is found at location %d",item,mid+1);
           flag=1;
           break;
          }
       }
   
   if (flag==0)
      System.out.printf("\n Element %d is not in the array",item);
 }
}

output:

LINEAR SEARCH

Linear search process starts comparing the search element with the first element in the given list if both are match then result is element found otherwise search element is compared with the next element in the given list repeat the process until the search element is compared with the last element in the given list the last element also doesn't match then the result is element is not found in the list that means the search element is compared with element by element in the list

Example

           A[6]   =             [65]    [20]     [10]     [55]     [32]    [12]

                                   A[0]    A[1]    A[2]   A[3]    A[4]   A[5]

Let the search element to be 12

Step 1:-search element is compared with first element 65

             A[6]   =           [65]    [20]     [10]     [55]     [32]    [12]

                                   A[0]    A[1]    A[2]   A[3]    A[4]   A[5]

                                    12

Both are not matching so move the next element

Step 2:-search element 12 is compared with next element 20

   A[6]   =             [65]    [20]     [10]     [55]     [32]    [12]

                           A[0]    A[1]    A[2]   A[3]    A[4]   A[5]

                                        12

But are not matching so move to next element

Step 3:-searching element  12 is compared to the  10

     A[6]   =             [65]    [20]     [10]     [55]     [32]    [12]

                            A[0]    A[1]    A[2]   A[3]    A[4]   A[5]

                                                    12

Both are not matching so move to the next element

Step 4:-search element 12 is compare with the next element 55

             A[6]   =           [65]    [20]     [10]     [55]     [32]    [12]

                                   A[0]    A[1]    A[2]   A[3]    A[4]   A[5]

                                                                      12

Both are not matching so move to next element

Step 5:-search element 12 is compared with next element 32

  A[6]   =             [65]    [20]     [10]     [55]     [32]    [12]

                           A[0]    A[1]    A[2]   A[3]    A[4]   A[5]

                                                                           12

Both are not matching so move to next element

Step 6:-searching element is compared with next element 12

   A[6]   =             [65]    [20]     [10]     [55]     [32]    [12]

                            A[0]    A[1]    A[2]   A[3]    A[4]   A[5]

                                                                                      12

Both matching are matching so we stop comparing and display element found at index 5


Program on Linear Search:
import java.util.Scanner;

class linearsearch
 {
  public static void main(String[] args)
  {
   int i,n,item,flag=0;
   Scanner ls = new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=ls.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   System.out.println("Enter unsorted Array :");
   for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=ls.nextInt();
    }
   System.out.print("\n Unsorted Array elements :");
   for(i=0;i<n;i++)
    {
     System.out.print(a[i]+" ");
    }
   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d]----- DATA=%d --- location=%d\n",i*4,i,a[i],i+1);
    }
   System.out.print("\n Enter the element to be searched :");
   item=ls.nextInt();
   for(i=0;i<n;i++)
   {
    if(a[i]==item)
     {
      System.out.printf("\n Element %d is found at location %d",item,i+1);
      flag=1;
      break;
     }
   }
     if (flag==0)
      System.out.printf("\n Element %d is not in the array",item);
 }
}

output:

Wednesday 18 December 2019

Program to delete an element in an array

import java.util.Scanner;
public class Rkdelete
 {
  public static void main(String[] args)
  {
   int pos,item,n;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=s.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size plus one
 
   for(int i=0;i<n;i++)
       {
        System.out.printf("enter a[%d] = ",i);
        a[i]=s.nextInt();
       }
  System.out.print("\n ARRAY element  :");
  for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
 
   for(int i=0;i<n;i++)
   System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d  position : %d",i*4,i,a[i],i+1);
   System.out.print("\nenter the position of the item to be deleted :\n");
   pos=s.nextInt();
   pos=pos-1;
   for(int j=pos;j<n-1;j++)
       {
        a[j]=a[j+1];
       }
 
   System.out.print("\n ARRAY after deleting an element  :\n");
   for(int i=0;i<n-1;i++)
       {
        System.out.print( a[i] +" ");
       }
 
for(int i=0;i<n-1;i++)
System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d  position : %d",i*4,i,a[i],i+1);
}
}
output:

Program to insert an element in an array

Program to insert an element in an array
import java.util.Scanner;
public class Rkin
 {
  public static void main(String[] args)
  {
   int pos,item,n;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=s.nextInt();
   int[] a;//declaration of array
   a=new int[n+1]; // Creation of array with given size plus one
  
   for(int i=0;i<n;i++)
       {
        System.out.printf("enter a[%d] = ",i);
        a[i]=s.nextInt();
       }
   System.out.print("ARRAY before inserting an element  :\n");
   for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
   for(int i=0;i<n;i++)
   System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d  position : %d",i*4,i,a[i],i+1);
   System.out.print("\nenter the position of the item :\n");
   pos=s.nextInt();
   System.out.print("\nenter the item to be inserted :\n");
   item=s.nextInt();
   pos=pos-1;
   for(int j=n-1;j>=pos;j--)
       {
        a[j+1]=a[j];
       }
    a[pos]=item;
    for(int i=0;i<n+1;i++)
       {
        System.out.print( a[i] +" ");
       }
  
    for(int i=0;i<n+1;i++)
       System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d  position : %d",i*4,i,a[i],i+1);
   }
}
output:



Tuesday 17 December 2019

Program to illustrate Ascending order of n array elements

Program to illustrate Ascending order of n array elements

import java.util.Scanner;
public class Rkasc
 {
  public static void main(String[] args)
  {
   int n,temp;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=s.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   for(int i=0;i<n;i++)
       {
        System.out.printf("enter a[%d] = ",i);
        a[i]=s.nextInt();
       }
   System.out.print("ARRAY before Ascending order :\n");
   for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
   for(int i=0;i<n-1;i++)
   {
    for(int j=0;j<n-1-i;j++)
      {
        if(a[j]>a[j+1])
        {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
      }
   }
    System.out.print("\nARRAY after Ascending order :\n");
    for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
  
for(int i=0;i<n;i++)
  System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d ",i*4,i,a[i]);
 }
}

output:

Program to illustrate Descending order of n array elements

Program to illustrate Descending order of n array elements

import java.util.Scanner;
public class Rkdes
 {
  public static void main(String[] args)
  {
   int n,temp;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=s.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   for(int i=0;i<n;i++)
       {
        System.out.printf("enter a[%d] = ",i);
        a[i]=s.nextInt();
       }
   System.out.print("ARRAY before Descending order :\n");
   for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
   for(int i=0;i<n;i++)
    {
     for(int j=0;j<n-1;j++)
      {
        if(a[j]<a[j+1])
        {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
      }
     }
  System.out.print("\nARRAY after Descending order :\n");
    for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
  
for(int i=0;i<n;i++)
  System.out.printf("\nAddress:%h ---array : a[%d] ---- data :%d ",i*4,i,a[i]);
 }
}

output:

Monday 16 December 2019

Program on address calculation in Multi Dimensional array

Program on address calculation in Multi Dimensional array
import java.util.*;

class mda
{
 public static void main (String args[])
 {
  int item,m,n,o;
  Scanner s = new Scanner (System.in);
  System.out.print( " Enter the size of the dimensions : ");
  m=s.nextInt();
  System.out.print( " Enter the size of the row : ");
  n=s.nextInt();
  System.out.print( " Enter the size of the column : ");
  o=s.nextInt();
  //int[] a; // declaration
  //a=new int[n];// creation

  int[][][] a= new int[m][n][o]; // declaration and creation
  System.out.printf("\n Enter %d Elements ",m*n*o);
  for(int i=0;i<m;i++)
   {
   for(int j=0;j<n;j++)
    {
   for(int k=0;k<o;k++)
     {
      System.out.printf("\n Elements a[%d][%d][%d] =",i,j,k);
      a[i][j][k]=s.nextInt();
     }
    }
   }
  System.out.print("\n Array elements : ");
  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
     for(int k=0;k<o;k++)
      {
       System.out.print( a[i][j][k] + " ");
      }
     }
   }
  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
     for(int k=0;k<o;k++)
      {
       System.out.printf("\nArray = a[%d][%d][%d] ---- Data = %d",i,j,k,a[i][j][k]);
      }
     }
   }
  System.out.print("\n Enter the Array element which u want : ");
  item=s.nextInt();
  for(int i=0;i<m;i++)
   {
   for(int j=0;j<n;j++)
    {
    for(int k=0;k<o;k++)
     {
     if(item == a[i][j][k])
      System.out.printf("\nArray = a[%d][%d][%d] ---- Data =   %d",i,j,k,a[i][j][k]);
     }
    }
   }
  }
 }

output:

Program on address calculation in Two Dimensional array

Program on address calculation in Two Dimensional array
import java.util.*;

class tda
{
 public static void main(String args[])
 {
  int item,m,n;
  Scanner s = new Scanner (System.in);
  System.out.print( " Enter the size of the row : ");
  m=s.nextInt();
  System.out.print( " Enter the size of the col : ");
  n=s.nextInt();
  //int[] a; // declaration
  //a=new int[n];// creation

  int[][] a= new int[m][n]; // declaration and creation
  System.out.printf("\n Enter %d Elements ",m*n);
  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
      System.out.printf("\n Elements a[%d][%d] =",i,j);
      a[i][j]=s.nextInt();
     }
   }

  System.out.print("\n Array elements : ");
  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
      System.out.print( a[i][j] + " ");
     }
   }

  for(int i=0;i<m;i++)
   {
    for(int j=0;j<n;j++)
     {
      System.out.printf("\nArray = a[%d][%d] ---- Data = %d",i,j,a[i][j]);
     }
   }
  System.out.print("\n Enter the Array element which u want : ");
  item=s.nextInt();
  for(int i=0;i<m;i++)
   {
   for(int j=0;j<n;j++)
    {
    if(item == a[i][j])
      {
       System.out.printf("\nArray = a[%d][%d] ---- Data =   %d",i,j,a[i][j]);
      }
    }
  }
 }
}
output:

Program on address calculation in Single Dimensional array

Program on address calculation in Single Dimensional array

import java.util.*;
class sda
{
 public static void main(String args[])
 {
  int item,n;
  Scanner s = new Scanner (System.in);
  System.out.print( " Enter the size of the array ");
  n=s.nextInt();
  //int[] a; // declaration
  //a=new int[n];// creation
  int[] a= new int[n]; // declaration and creation
  for(int i=0;i<n;i++)
   {
    a[i]=s.nextInt();
   }
  System.out.print("\n Array elements : ");
  for(int i=0;i<n;i++)
   {
   System.out.print( a[i] + " ");
   }
  for(int i=0;i<n;i++)
   {
   System.out.printf("\n Address = %h ---- Array = a[%d] ---- Data = %d",i*4,i,a[i]);
   }
  System.out.print("\n Enter the Array element which u want : ");
  item=s.nextInt();
  for(int i=0;i<n;i++)
   {
    if(item == a[i])
   System.out.printf("\n Address = %h ---- Array = a[%d] ---- Data =   %d",i*4,i,a[i]);
   }
  
  }
 }

output:

Saturday 14 December 2019

Program on Instance ,Static ,Local variables

public class Variables
{
   int a=10; // INSTANCE VARIABLE
   static int c=30; // STATIC VARIABLE
   public static void main(String[] args)
     {
    int b=20; // LOCAL VARIABLE

    Variables v= new Variables();// object creation is required to access instance variable
    int z=v.a;

    System.out.print("\n INSTANCE VARIABLE ="+v.a);
    System.out.print("\n LOCAL VARIABLE ="+b);
    System.out.println("\n STATIC VARIABLE ="+c);

    System.out.print("\n INSTANCE VARIABLE ="+z+ "\n" +" LOCAL VARIABLE="+b+ "\n" +" STATIC VARIABLE = "+c);

     }
}

output:
 

Friday 13 December 2019

Java Development Kit (JVM) , Java Runtime Environment (JRE), Java Virtual Machine (JVM)






Program to find Minimum and Maximum number in an Array

import java.util.Scanner;

class rkmin
 {
  public static void main(String x[])
  {
   int i,p=0,q=0,n,min,max;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=s.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
  for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=s.nextInt();
    }
   System.out.print("\n Array elements :");
   for(i=0;i<n;i++)
    {
     System.out.print(a[i]+" ");
    }
   for(i=0;i<n;i++)
    {
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d] DATA=%d\n",i*4,i,a[i]);
    }
    min=a[0];
    max=a[0];
   for(i=0;i<n;i++)
    {
    if(a[i]>max)
    {max=a[i]; p=i;}

   if(a[i]<min)
     {min=a[i];q=i;}
    }

   System.out.println("\n maximum number is :" +max+ " and " + "minimum number is :"+min);

//    System.out.println("minimum number is :"+min);

   for(i=0;i<n;i++)
   {
    if(a[i]==max)
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d] DATA=%d\n",i*4,i,a[i]);
    if(a[i]==min)
     System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d] DATA=%d\n",i*4,i,a[i]);
   }
  }
}

output:

Program to illustrate Two Dimensional Array

Program to illustrate Two Dimensional Array 

import java.util.Scanner;
class rkar2
 {
  public static void main(String x[])
  {
   int m,n;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Number of Rows(m) in Array  :");
   m=s.nextInt();
   System.out.println("Enter Number of Columns(n) in Array  :");
   n=s.nextInt();
   int[][] a;//declaration of array
   a=new int[m][n]; // Creation of array with given size
   System.out.println("Enter "+m+"Rows and "+n+" Columns "+m*n+" Elements");
   for(int i=0;i<m;i++)
    {
     for(int j=0;j<n;j++)
     {
     a[i][j]=s.nextInt();
     }
    }
   System.out.println("\n Array elements :");
   for(int i=0;i<m;i++)
    {
     for(int j=0;j<n;j++)
     {
     System.out.print(a[i][j]+"\t");
     }
     System.out.println("\n");
    }
   for(int i=0;i<m;i++)
    {
     for(int j=0;j<n;j++)
     {
      System.out.printf("\n ADDRESS=%h ---- ARRAY=a[%d][%d] DATA=%d\n",a,i,j,a[i][j]);
     } 
   }
  }
}

output:


Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...