Sunday 30 August 2020

JVM(Java Virtual Machine)

                 JVM Architecture
              Java Virtual Machine
1. Class Loader
2. Run - Time Data Area
3. Execution Engine 
1. Class Loader ----------> a. Loading
                                          b. Linking
                                          c. Initialisation Loading ------------------> Bootstrap Loader 
                        --------> Application Loader
                          --------> Extension Loader
Bootstrap Loader loads the system class from RT.Jar to the Run Time Memory. The system class is written inside the RT.Jar and this RT.Jar will load this system class into the memory. So it is done by the Bootstrap Loader.
      Source file ----compiled----> .class file
This .class file is loaded in the run time memory with the help of Application Loader.
Extension Class Loader is responsible for loading additional class files from .jre or .ext folder. 
If you have a source code if you need to connect this source code with some data base then you need ojdbc.jar class file in this scenario we add this 3rd party jar file into the extension class loader and extension class loader makes the pre compiled class file available to the JVM by loading files into the memory area.
Linking ---------------> it is required to verify the generated .class file  verification is what, once the class file is loaded to the memory then there is a verification phase
and this verification is done by the linking phase.
     Linking-------verifies-------->class files
Where the byte code class file is verified if it's confirmed to the standards or not.
2. Run-Time Data Area is the 2nd main component of JVM. JVM needs a memory area to store the class files. This run-time memory area is comprised of many types of memories like method area, heap memory area, stack memory area etc.
Stack Memory Application :-
Stack is a data structure
Stack is a abstract sata type
Stack can be performed by two main operations PUSH and POP
PUSH -> Inserting item one by one when pushing is done stack is known as full.
POP -> Deleting item one by one starting from the top of the stack. Now when the stack is empty the pop operation is done.
Think JVM contains stack

Visualize JVM divided into 3 part. And this stack is part of 2nd component Run-Time Data Area Component.
The purpose of this stack whenever we write javap-c<class_name> in command
prompt then we we'll able to see a byte code this byte code is comprised of two operations PUSH and POP or LOAD and STORE.


3. Execution Engine is the actual JVM execution engine that converts byte code in to the Machine Code and executes the instructions.
Interpreter -> Interpreter reads the class file or the byte code and executes it one by one.
Here is a problem, whenever interpreter reads a particular file if there exists any repeated calling procedure i.e when a method is called multiple times, it interprets those lines again and again. This is the main problem with the interpreter and this problem is solved by JIT Compiler. JIT Compiler solves this problem by using a special component which is known as profiler.
Profiler will find the hotspot where the repeated function or method calling appears and then repeatedly executed codes are converted to native code by profiler so, that it can avoid repeated method calls. So, it is very important that the interpreter is responsible for execution the specific part of byte code and interpreter suffers from a problem of looping again and again where repeated method calling exists.
Garbage Collector is a member of the 3rd component of the JVM. This garbage collector has the responsibility of collecting or destroying the object which is no longer used. 






                      

Monday 24 August 2020

Inverted half pyramid using numbers using CPP

Source Code : 

#include<iostream>
using namespace std;
int main()
{
int rows;
cout<<"Enter number of rows: ";
cin>> rows;
for(int i = rows; i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}

Program Output :




Sunday 23 August 2020

CPP Program to perform radix sort

Source Code :

#include<iostream>
using namespace std;

int getMax(int arr[], int n)
{
int max = arr[0];
for(int i = 1; i < n; i++)
      if (arr[i] > max)
           max = arr[i];
    return max;
}

void countSort(int arr[], int n, int exp)
{
int output[n], i, count[10] = {0};
for(i = 0; i < n; i++)
     count[(arr[i] / exp)%10]++;
for(i = 0; i < n; i++)
     count[i] += count[i-1];
     
    for(i = n - 1; i >= 0; i--)
    {
    output[count[(arr[i] / exp) % 10] - 1] = arr[i];
    count[(arr[i] / exp) % 10]--;
}
for(i = 0; i < n; i++)
          arr[i] = output[i];
}

void radixsort(int arr[], int n)
{
int exp, m;
m = getMax(arr, n);
for(exp = 1; m/exp > 0; exp *= 10)
      countSort(arr, n, exp);
}

int main()
{
int n, i;
cout<<"Enter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
radixsort(arr, n);
cout<<"Sorted Data ";
for(i = 0; i < n; i++)
  cout<<"->"<<arr[i];
  return 0;
}

Program Output :

Saturday 22 August 2020

CPP Program to implement counting sort

Source Code : 

#include<iostream>
using namespace std;
void CounterSort(int a[], int n, int r, int lower)
{
int i, j = 0, counter[r] = {0};
for(i=0; i<n; i++)
   counter[a[i]-lower]++;
   
   i=0;
   
   while(i < r)
   {
       flag:
       a[j] = lower+1;
       j++;
       counter[i]--;
       
       if(counter[i] > 0)
       goto flag;
       
       i++;
   }
}

int main()
{
int n, i, range, ulimit, llimit;
cout<<"Enter the number of data element to be sorted: ";
cin>>n;
cout<<"Enter the lower and upper limit of the data to be entered: ";
cin>>llimit>>ulimit;
range = ulimit-llimit+1;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
CounterSort(arr, n, range, llimit);
cout<<"Sorted Data ";
for(i = 0; i < n; i++)
       cout<<"->"<<arr[i];
       
       return 0;
}

Program Output : 



Friday 21 August 2020

CPP Program to perform quick sort

Source Code : 

#include<iostream>
#include<cstdlib>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int Partition(int a[], int low, int high)
{
int pivot, index, i;
index = low;
pivot = high;
for(i=low; i<high; i++)
{
if(a[i] < a[pivot])
{
swap(&a[i], &a[index]);
index++;
}
}
swap(&a[pivot], &a[index]);
return index;
}

int RandomPivotPartition(int a[], int low, int high)
{
int pvt, n, temp;
n = rand();
pvt = low + n%(high-low+1);
swap(&a[high], &a[pvt]);
return Partition(a, low, high);
}

int QuickSort(int a[], int low, int high)
{
int pindex;
if(low < high)
{
pindex = RandomPivotPartition(a, low, high);
QuickSort(a, low, pindex-1);
QuickSort(a, pindex+1, high);
}
return 0;
}

int main()
{
int n, i;
cout<<"Enter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
QuickSort(arr, 0, n-1);
cout<<"Sorted Data ";
for(i = 0; i < n; i++)
    cout<<"->"<<arr[i];
    
    return 0;
}

Program Output : 

Thursday 20 August 2020

CPP Program to implement merge sort

Source Code :

#include<iostream>
using namespace std;
void swapping(int &a,int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size)
{
for(int i = 0;i<size;i++)
   cout<<array[i]<<" ";
    cout<<endl;
}
void merge(int *array, int l, int m, int r )
{
int i,j,k,nl,nr;
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr];
for(i=0;i<nl;i++)
  larr[i] = array[l+i];
for(j=0;j<nr;j++)
   rarr[j] = array[m+1+j];
i=0; j=0; k=l;
while(i < nl && j<nr)
{
if(larr[i] <= rarr[j])
{
array[k] = larr[i];
i++;
}
else
{
array[k] = rarr[j];
j++;
}
k++;
while(i<nl) 
{
array[k] = larr[i];
i++; k++;
}
while(j<nr)
{
array[k] = rarr[j];
j++; k++;
}
}

void mergeSort(int *array,int l,int r)
{
int m;
if(l < r)
{
int m = l+(r-1)/2;
mergeSort(array,l,m);
mergeSort(array,m+1,r);
merge(array,l,m,r);
}
}
int main()
{
int n;
cout<< "Enter the number of elements: ";
cin>>n;
int arr[n];
cout<<"Enter elements: "<<endl;
{
for(int i = 0; i<n; i++)
{
cin>>arr[i];
}
}
cout<<"Array before Sorting: ";
display(arr , n);
mergeSort(arr, 0, n-1);
cout<<"Array after Sorting: ";
display(arr , n);
return 0;
}

Program Output : 

Tuesday 18 August 2020

CPP Program to implement insertion sort

Source Code : 

#include<iostream>
using namespace std;
struct list
{
int data;
list *next;
};

list* InsertnList(list *head, int n)
{
list *newnode = new list;
list *temp = new list;
newnode->data = n;
newnode->next = NULL;
if(head == NULL)
{
head = newnode;
return head;
}
else
{
temp = head;
if(newnode->data < head->data)
{
newnode->next = head;
head = newnode;
return head;
}
while(temp->next != NULL)
{
if(newnode->data < (temp->next)->data)
break;
temp=temp->next;
}
newnode->next = temp->next;
temp->next = newnode;
return head;
}
}

int main()
{
   int n, i, num;
   list *head = new list;
   head = NULL;
   
   cout<<"Enter the number of data element to be sorted: ";
   cin>>n;
   
   for(i=0;i<n;i++)
   {
    cout<<"Enter element "<<i+1<<": ";
    cin>>num;
    head = InsertnList(head, num);
   }
   
   cout<<"Sorted Data ";
   while(head != NULL)
   {
    cout<<"->"<<head->data;
    head = head->next;
   }
   
   return 0;
}

Program Output : 

Monday 17 August 2020

CPP Program to implement bubble sort

Source Code : 

#include<iostream>
using namespace std;
void Bublesort (int arr[], int n)
{
int i,j;
for(i=0;i<n;++i)
{
for(j=0;j,n-i-1;++j)
{
   if(arr[j]>arr[j+1])
   {
    arr[j] = arr[j]+arr[j+1];
    arr[j+1] = arr[j]-arr[j+1];
    arr[j] = arr[j]-arr[j+1];
   }
}
}
}

int main()
{
int n, i;
cout<<"Enter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
Bublesort(arr,n);
cout<<"Sorted Data ";
for(i=0;i<n;i++)
cout<<"_>"<<arr[i];
return 0;
}

Program Output :