My Blog List

Wednesday, November 27, 2019

Insertion sort using cpp.

Insertion sort using CPP.

In this tutorial we learn another sorting technique that is insertion sort.
this technique comparing element in the array each other and then when smaller element is found then
change to its proper position.

complexity:
worst complexity:n^2
average complexity:n^2
best complexity:n


example:
 
we are sorting this array element in ascending order.
suppose we have a unsorted array of element 5
arr[5]=43 25 12 73 39

//in the first iteration compare 1st and 2nd position in array if smaller element is found then change to its proper position.
//after iteration first array become
arr[5]=25 43 12 73 39

//after first iteration compare 1st and 2nd position of element with 3rd position of element and then sort element.
//after iteration array become.
arr[5]=12 25 43 73 39

//now compare all previous element with 4th position of element.
in this iteration no change because no smaller element is found.
array element become as it is.

//for last iteration it compare all previous positin with 5th position of element.
array become.
arr[5]=12 25 39 43 73

//finally we get a sorted array in ascending order.


This is the simple code for sort array in ascending order by using insertion sort.
     
#include<iostream.h>
#include<conio.h>

//declare the function for insertion sort.
void insertsort(int *arr,int size)
{
    int i,j,index;
    for(i=1;i<size;i++)
    {
        index=arr[i];
        j=i;
        while((j>0)&&(arr[j-1]>index))
        {
            arr[j]=arr[j-1];
            j=j-1;
        }
        arr[j]=index;
    }
}
void main()
{
    int *arr,size;
    clrscr();

    //Enter array size dynamically. 
    cout<<"\nEnter the size of array:";
    cin>>size;
    cout<<"\nEnter the element:";
    for(int i=0;i<size;i++)
    {
        cin>>arr[i];
    }
    insertsort(arr,size);
    cout<<"\nSorted element are:";
    for(i=0;i<size;i++)
    {
        cout<<arr[i]<<"\n";
    }
    getch();
}


OUTPUT:



Monday, November 11, 2019

Selection Sort

In this tutorial learn how sort the data using Selection Sort Technique.

This sorting technique is combination of searching and sorting.Its working per iteration in unsorted element with smallest or largest element.The number of iteration for this sort is n-1.


Example:
arr[]=42 63 47 87 11

/*after first iteration
arr[]=11 63 47 87 42

/*after Second iteration
arr[]=11 42 47 87 63

/*after Third iteration
arrr[]=11 42 47 87 63 
/*in this iteration array element remains unchanged because no smallest element found unsorted at postion 3rd.

/*after fourth iteraation
arr[]=11 42 47 63 87


Following code shows the Selection Sort:-

#include<iostream.h>
#include<conio.h>
void selsort(int numbers[],int n)
{
int i,j,min,temp;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(numbers[j]<numbers[min])
{
min=j;
}
}
temp=numbers[i];
numbers[i]=numbers[min];
numbers[min]=temp;
}
}
void main()
{
int *numbers,n;
clrscr();
cout<<"\nEnter the size of array:";
cin>>n;
numbers=new int[n];
cout<<"\nEnter the array element:";
for(int i=0;i<n;i++)
{
cin>>numbers[i];
}
selsort(numbers,n);
cout<<"\nSorted element:";
for(i=0;i<n;i++)
{
cout<<" "<<numbers[i];
}
getch();
}


Above code shows following output:


Quick Sort using Cpp.

In this tutorial we learn how to sort data using Quick Sort Technique

In this Sorting technique the algorithm works on divide and conquer technique.Its take a
first element as pivot and then divide the array.The element lesser than pivot element is left side and right side always greater.The Quick sort called itself recursively.

Following code shows the Quick Sort Technique:-

#include<iostream.h>
#include<conio.h>
int partition(int a[],int l,int r)
{
int pivot,i,j,temp;
pivot=a[l];
i=l;
j=r+1;
while(1)
{
do
++i;
while((a[i]<=pivot) &&i<=r);
do
--j;
while(a[j]>pivot);
if(i>=j)
break;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
temp=a[l];
a[l]=a[j];
a[j]=temp;
return j;
}
void quicksort(int a[],int l,int r)
{
int j;
if(l<r)
{
j=partition(a,l,r);
quicksort(a,l,j-1);
quicksort(a,j+1,r);
}
}
void main()
{
int *numbers,n;
clrscr();
cout<<"\nEnter the size of array";
cin>>n;
numbers=new int[n];
cout<<"\nEnter array element:";
for(int i=0;i<n;i++)
{
cin>>numbers[i];
}
quicksort(numbers,0,n-1);
cout<<"\nSorted array";
for(i=0;i<n;i++)
{
cout<<" "<<numbers[i];
}
getch();
}

Above code Shows following output:




Sunday, November 10, 2019

Bubble Sort using CPP.

In this tutorial we learn how to sort the data using Bubble Sort.

A Sorting technique is use to rearrange the data in the ordered manner.The data arrange by Ascending and Descending order.Bubble Sort is he simple Sorting Algorithm Technique it works by repeatedly swapping data.When the data are giving input in wrong order then the Sorting technique is used.

Following code shows the Bubble Sorting technique:-
In this example we take a dynamic array the size will dynamic as given by user.


#include<iostream.h>
#include<conio.h>
void bubblesort(int numbers[],int n)
{
int i,j,temp;
for(i=(n-1);i>=0;i--)
{
for(j=1;j<=i;j++)
{
if(numbers[j-1]>numbers[j])
{
temp=numbers[j-1];
numbers[j-1]=numbers[j];
numbers[j]=temp;
}
}
}
}
void main()
{
int *numbers,n;
clrscr();
cout<<"\nEnter the size of array:";
cin>>n;
numbers=new int[n];
cout<<"\nEnter the array element:";
for(int i=0;i<n;i++)
{
cin>>numbers[i];
}
bubblesort(numbers,n);
cout<<"\nSorted element:";
for(i=0;i<n;i++)
{
cout<<" "<<numbers[i];
}
getch();
}


The above code shows the following output:-




Program for circular queue in cpp

In this tutorial we learn how to create circular queue in cpp. In the data structure queue is one type of data storage type.We include the data in queue. The queue working first in first out(FIFO).It is a linear data structure.In this we can insert element until queue become full.

following is the simple coding for circular queue:-

#include<iostream.h>
#include<conio.h>
#define SIZE 5
class cqueue
{
int front,rear;
int cq[SIZE];
public:
cqueue()
{
front=rear=-1;
}
void insert(int ele)
{
if(rear==front-1||front==0&&rear==SIZE-1)
{
cout<<"Circular queue is full";
return;
}
else if(front==-1 &&rear==-1)
{
front=rear=0;
cq[rear]=ele;
}
else
{
rear=(rear+1)%SIZE;
cq[rear]=ele;
}
}
int deleted()
{
int x;
if(front==-1&& rear==-1)
{
cout<<"\nCircular queue is empty";
return 0;
}
else
{
if(front==rear)
{
x=cq[rear];
front=rear=-1;
return x;
}
else
{
x=cq[front];
front=(front+1)%SIZE;
return x;
}
}
}
void display()
{
if(front==-1 && rear==-1)
{
cout<<"\nCircular queue is empty";
return;
}
else
{
if(front<=rear)
{
for(int i=front;i<=rear;i++)
{
cout<<" "<<cq[i];
}
}
else
{
for(int i=front;i<SIZE;i++)
{
cout<<" "<<cq[i];
}
if(front>rear)
{
for(int i=0;i<=rear;i++)
{
cout<<" "<<cq[i];
}
}
}
}
}
};
void main()
{
cqueue c;
int ch,ele,m;
clrscr();
do
{
cout<<"\nMenu\n1.Insert\n2.Delete\n3.Display\n4.Exit";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter element:";
cin>>ele;
c.insert(ele);
break;
case 2:
m=c.deleted();
cout<<"\nDleted element:"<<m;
break;
case 3:
c.display();
break;
case 4:
cout<<"\nExit";
break;
}
}while(ch!=4);
getch();
}


The above program shows the following output:




Saturday, November 9, 2019

program for constructor overloding in cpp



#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int roll;
char name[20];
public:
student(){};
student(int r,char n[])
{
roll=r;
strcpy(name,n);
}
void show()
{
cout<<"\nRoll number:"<<roll;
cout<<"\nName:"<<name;
}
};
void main()
{
student s,s1(18,"Mayur");
clrscr();
s.show();
s1.show();
getch();
}




code for creating a star using svg in html5

This is the code for creating a simple logic for display the star shape using a svg element in html5.

<html>
<head><title>star</title></head>
<svg height="400" width="400"">
<polygon points="100,10,40,198,190,78,10,78,160,198" fill="red"/>
</svg>
</html>





output

Programming for printing rainbow in html using svg

This is the programs for creating a rainbow shape using svg element in html5.


<html>
<head>
<title>Smile</title>
</head>
<body>
<svg  height="140" ">
<circle id = "circle" cx = "150" cy="150" r="150" fill = "violet" />

<circle id = "circle" cx = "150" cy="150" r="130" fill = "orange" />

<circle id = "circle" cx = "150" cy="150" r="110" fill = "yellow" />

<circle id = "circle" cx = "150" cy="150" r="90" fill = "blue" />

<circle id = "circle" cx = "150" cy="150" r="70" fill = "green" />

<circle id = "circle" cx = "150" cy="150" r="50" fill = "white" />

<circle id = "circle" cx = "150" cy="150" r="30" fill = "red" />


</svg>
</body>
</html>

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++. On the basis of Inheritance concept the another type of inheritance is the Multiple I...