Wednesday, August 28, 2019

C++ Language Program for Sort in Insertion Sort.



BACHELOR OF COMPUTER APPLICATION (B.C.A.) FIRST SEMESTER

Here you will get simple C++ program 
C++ Language Program for Sort in Insertion Sort.
IT IS THE SMALLEST LOGIC USED FOR CODING OF THIS PROGRAM.
IN WHICH YOU CAN CREATE OWN VARIABLES AND FUNCTIONS.

User will input will be an unsorted form in Array and that array is sorted by Insertion Sort,then it will be display on screen.


The program coding is given below.
#include <iostream.h>
#include <conio.h>
class Abc
{
int a[10],N,PTR,TEMP;
public:
void getdata()
{
cout<<"Enter N: ";
cin>>N;
cout<<"Enter Elements: ";
for (int i=1;i<=N;i++)
{
cin>>a[i];
}
}
void insert()
{
a[0]=0;
for (int k=2;k<=N;k++)
{
TEMP=a[k];
PTR=k-1;
while(TEMP<a[PTR])
{
a[PTR+1]=a[PTR];
PTR=PTR-1;
}
a[PTR+1]=TEMP;
}
}
void display()
{
cout<<"\nInsertion Sort\n";
for (int i=1;i<=N;i++)
{
cout<<a[i]<<" ";
}
}
};
void main()
{
clrscr();
Abc a1;
a1.getdata();
a1.insert();
a1.display();
getch();
}
Output will be given after you compile it.

No comments:

Post a Comment