Showing posts with label C Plus Plus guide. Show all posts
Showing posts with label C Plus Plus guide. Show all posts

Wednesday, 23 January 2013

Radix Sort


Radix Sorting is the sorting algorithm or a type of sorting which is used to sort individual data items.Radix sorting is used when we have a large list of value.Worst case Complexity of radix sort is O(var. n).This technique is used in c ++,java,c# language etc.


Radix Sorting Example:-

233,456,678,555,764,631,4326,88
=233,456,678,555,764,631,88('Sort' on base of last digit)

Phase 1:-

0
1    631
2    
3    233
4    764
5    555
6    456
7    
8    678,88
9

=631,233,764,555,456,678,88

Phase 2:-

0     
1    
2    
3    631,233
4
5    555,456
6    764
7    678
8    88
9     

=631,233,555,456,764,678,88

Phase 3:-

0    (0)88
1    
2    233
3    
4    456
5    555
6    631,678   
7    764
8    
9

=88,233,456,555,631,678,764 (Sorted Array)

Function cum Program:-


void radix(int a[],int n)
{
int i,r[max],exponent=1;
for(i=0;i<n;i++)
{
if(a[i]>m)
m=a[i];
}
while(m/exponent>0)
{
for(i=0;i<n;i++)
b[a[i]/exponent % 10];
for(i=1;<10;i++)
b[i]=b[i]+1;
for(i=n-1;i<n;i++)
b[a[i]/exponent % 10]=a[i];
for(i=o;i<n;i++)
{
a[i]=b[i];
}}}


That's all about Radix Sorting in data structure and computer science.


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Shell Sort


Shell Sorting is the sorting algorithm or a type of sorting which is used to sort data items in ascending or descending order.This method is developed by D.L Shell.Worst case Complexity of Shell sort is O(n).This technique is used in c ++,java,c# language etc.Shell Sort technique is also called as modified insertion sorting.


Shell Sorting Example and Algorithm:-

Gap=n/2.
10/2=5
gap=5.

=q  d  i  e  c  h  b  t  n  j (From q to h,d to b,i to t,e to n,c to j).

=h  b  i  e  c  q  d  t  n  j (after arrangement)

Now no value will swap,so gap=gap/2.
5/2=2.5=2

=h  b  i  e  c  q  d  t  n  j

=h  b  c  e  d  q  i  j  n  t (From h to c,b to e,c to d,e to q,d to i,q to j,i to n,j to t).

=c  b  d  e  h  j  i  q  n  t (after arrangement)


Now no value will swap,so gap=gap/2
2/1=1.


=h  b  c  e  d  q  i  j  n  t (From h to b,b to c,c to e,d to q,q to i,i to j,j to n,n to t).

=b  c  d  e  h  j  n  q  t (Sorted list).


Function cum Program:-

void shell(int a[],int n)
{
int gap,flag,i,j,t;
gap=gap/2;
flag=1;
while(gap)
{
while(flag)
{
flag=0;
for(i=0;i<n-gap;i++)
{
if(a[i]>a[i+gap])
{
t=a[i];
a[i]=a[i+gap];
a[i+gap]=t;
}
}
}
gap=gap/2;
}
}


That's all about Shell Sorting in data structure and computer science.

If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Tuesday, 22 January 2013

Insertion Sort


Insertion Sorting is the sorting algorithm or a type of sorting which is used to sort data items in ascending or descending order.Best case Complexity of insertion sort is O(n) for comparison and O(1) for swap.Insertion sort is best for used when list size is small.This technique is used in c ++,java,c# language etc.

Insertion Sorting Example and Algorithm:-


=6    5    1    3    2    8    4(unsorted)

=5    6    1    3    2    8    4
=1    5    6    3    2    8    4
=1    3    5    6    2    8    4
=1    2    3    5    6    8    4
=1    2    3    4    5    6    8(Sorted)

Function cum Program:-



#include<iostream.h>
#include<conio.h>
void insert(int a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
t=a[i];
for(j=i-1;j>=0;j--)
{
if(t<a[j])
a[j+1]=a[j];
else
break;
}
a[j+1]=t;
}}


That's all about Insertion Sorting in data structure and computer science.



If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Selection Sort


Selection Sorting is the sorting algorithm or a type of sorting which is used to sort individual data items in ascending or descending order.Complexity of Selection Sort is O(n)2.It is the most simple sorting method as compared to bubble,insertion,heap etc.This technique is used in c ++,java,c# etc.


Selection Sorting Example and Algorithm:-

=5  7  4  3  2  6  1(Before Sort)
=4  7  5  3  2  6  1(Swap 4 & 3)
=3  7  5  4  2  6  1(Swap 3 & 2)
=2  7  5  4  3  6  1(Swap 2 & 1)
=1  7  5  4  3  6  2(Swap 7 & 5)
=1  5  7  4  3  6  2(Swap 5 & 4)
=1  4  7  5  3  6  2(Swap 4 & 3)
=1  3  7  5  4  6  2(Swap 3 & 2)
=1  2  7  5  4  6  2(Swap 7 & 5)
=1  2  5  7  4  6  3(Swap 5 & 4)
=1  2  4  7  5  6  3(Swap 4 & 3)
=1  2  3  7  5  6  4(Swap 7 & 5)
=1  2  3  5  7  6  4(Swap 5 & 4)
=1  2  3  4  7  6  5(Swap 7 & 6)
=1  2  3  4  6  7  5(Swap 6 & 5)
=1  2  3  4  5  7  6(Swap 7 & 6)
=1  2  3  4  5  6  7(After Sort)

Example of Selection Sort(In Program):-


#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,t,n;
cout<<"Enter size";
cin>>n;
cout<<"Enter array element";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
cout<<"Array as";
for(i=0;i,n;i++)
cout<<a[i];
getch();
}


That's all about Selection Sorting in data structure and computer science.


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Monday, 21 January 2013

Bubble Sort


Bubble Sorting is the sorting algorithm or a type of sorting which is used to sort individual data items in ascending or descending order.Best case Complexity of selection sort is O(n).This sorting technique are also called as sinking sort.This technique is used in c ++,java,c# language etc.

Bubble Sorting Example and Algorithm:-

=5  7  4  3  2  6  1(Before Sort)
=5  4  7  3  2  6  1(Swap 7 & 3)
=5  4  3  7  2  6  1(Swap 7 & 2)
=5  4  3  2  7  6  1(Swap 7 & 6)
=5  4  3  2  6  7  1(Swap 7 & 1)
=5  4  3  2  6  1  7(Swap 5 & 4)
=4  5  3  2  6  1  7(Swap 5 & 3)
=4  3  5  2  6  1  7(Swap 5 & 2)
=4  3  2  5  6  1  7(Swap 6 & 1)
=4  3  2  5  1  6  7(Swap 4 & 3)
=3  4  2  5  1  6  7(Swap 4 & 2)
=3  2  4  5  1  6  7(Swap 5 & 1)
=3  2  4  1  5  6  7(Swap 3 & 2)
=2  3  4  1  5  6  7(Swap 4 & 1)
=3  2  1  4  5  6  7(Swap 3 & 1)
=2  1  3  4  5  5  7(Swap 2 & 1)

=1  2  3  4  5  6  7(After Sort)


Example of Bubble Sort(In Program):-

#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,t,n;
cout<<"Enter size";
cin>>n;
cout<<"Enter array element";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}

}
cout<<"Array as";

for(i=0;i,n;i++)
cout<<a[i];
getch();
}


That's all about Bubble Sorting in data structure and computer science.


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Binary Search


Binary Search:-Binary Search is a searching technique which is used to locate items in ordered list of particular number.This technique is used in c,c++,java,c# etc.

Complexity of Binary Search is o(logn).

Binary Search Example and Algorithm:-

#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,x,n,a,l,f,flag;
cout<<"Enter Size";
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
flag=0;
cout<<"Enter Element to be Search";
cin>>x;
l=n-1;
f=0;
while(f<=l)
{
m=(f+l)/2;
if(a[m]==x)
{
flag=1;
break;
}
if(x>a[m])
{
f=m+1;
else
l=m-1;
}
if(flag==1)
cout<<"Element Found"<<m;
else
cout<<"NOT FOUND";
getch();
}


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Linear Search


Linear Search:-Linear Search are also called as sequential Search,in this technique we search the data items from structured data base in a linear way.
Linear way means one after another.This method considered as simple when we compared this to binary search.This technique is used in c,c++,java,c# etc.

Complexity of Linear Search is o(n).

Program/algorithm/Example of Linear Searching:-


#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,k,x,n,flag;
cout<<"Enter Size"
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
flag=0;
cout<<"Enter element to search";
cin>>x;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag==1)
cout<<"Element Found";
else
cout<<"Not Found";
}



If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Searching(Computer Science)


Searching is a technique which is widely used in c,c++,java,c# and many other programming languages.We can search anything like files,folder,page,documents etc or in simple words we can search particular data item in a structured data base like dictionary.

Searching Technique is divide into 3 parts:-

1)Linear Search.
2)Binary Search.
3)Indexed Search.

1)Linear Search:-Linear Search are also called as sequential Search,in this technique we search the data items from structured data base in a linear way.
Linear way means one after another.This method considered as simple when we compared this to binary search.


#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,k,x,n,flag;
cout<<"Enter Size"
cin>>n;
for(i=0;i<n;i++)
.
.
.
For More Click Here


2)Binary Search:-Binary Search is a searching technique which is used to locate items in ordered list of particular number.

3)Indexed Searching:-Index searching is technique which improve the efficiency of search items.In index searching the file is divided into blocks and keys.




If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Sunday, 20 January 2013

Hashing Techniques


Hashing is a searching technique that is widely used in c ++,java,c# etc.It has the best case complexity as compared to other searching technique.Its best case complexity is equal to 0(1) means record search in first attempt.

Now I discuss different-2 functions/method and hash collision:-

1)Mid Square Method:-

We will take an example:-

=22031168110(a value)
=11*11=121(square of mid value)

So store data in 121 location.

2)Division/Remainder Method:-

We take an example:-

=2345782829394(a value)

So divide this above value with the closest prime number and store  the value in that divisor.

3)Folding Method:-

We take an example:-

=121|456|111|45(a single value)
121+456+111+45=986

So stored that value in 986 location.

Now we discuss hash collision techniques:-

4)Linear Probing:-It is the most simple technique of hashing because if collision occurs,then store value in another empty space.




5)Bucketing Method:-Another great technique of hashing which is used to avoid collision in hash table.




6)Chaining method:-It is the technique which is used in hashing to avoid collision in hash table.




If data comes at location 2 then it create a chain like a linked list.So it is the best hashing technique as compared to other technique.



That's all about Hashing Techniques or algorithm(in data structure and computer science)


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Virtual Function


Virtual Function:-When we used same function in both the child and parent class,than we apply virtual function in parent class.Virtual function has dynamic binding function because method calling is resolve in run time.We put virtual keyword in parent class when we use this logic in our class.It is used in c ++,java,c#(c sharp) etc.

Example:-


#include<iostream.h>

#include<conio.h>

class parent

{
public:
virtual void show()
{
cout<<"THIS IS PARENT CLASS";
}
};
class child:public parent
{
public:
void show()
{
cout<<"THIS IS CHILD CLASS";
}
};
void main()
{
parent p;
child c;
parent*ptr;
ptr=&p;
ptr->show();
ptr=&c;
ptr->show();
getch();
}



That's all about Virtual Functions(in oops and data structure).


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Operator Overloading


Operator Overloading:-Operator Overloading provides option for creation of new definition of Operators.Operator overloading provide special meaning to most of the operator like =,+,-,*,=+,== etc. but expect some operator.It is used in c ++,java,c#(c sharp) etc.


Some exception operators are:-

1)Scope Resolution Operator(::)
2)Sizeof operator
3)Conditional Operator(?:) 
4)Class member access point
5)Value of (*.)

Example:-Write a program to overload the unary operator "-"(you can also overload binary operator).

# include<iostream.h>
void<conio.h>

class xyz
{
int p,q,r;
public:
void get();
void put();
void operator-();
};
void xyz::get()
{
cout<<"enter no.";
cin>>p>>q;
}
void num::put()
{
cout<<p<<q<<r;
}
void xyz::operator-()
{
p=-p;
q=-q;
r=-r;
}
void main()
{
xyz x;           //object
clrscr();
x.get();
x.put();
-x;
x.put();
getch();
}



That's all about Operator Overloading(in oops and data structure).



If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Function Overloading


Function Overloading(Overload Function) is a method of calling more than one function  by the same name or in simple word overloading means use of same thing for different work.

Function overloading calls several function by the same name but different no. of arguments and different no. of data types.Function overloading can be done in c ++,java,c# etc.

Example-Some examples/program samples are:-

1)int add(int,int,int);
void add();
float add(float,float);

2)int subtract(int,int);
void subtract();

3)char minimum(char,char);
int minimum(int,int);
float minimum(float,float);

4)char max(char,char);
int max(int,int);
float(float,float);

5)void multiply();
int multiply(int,int);
float multiply(float,float);



If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Saturday, 19 January 2013

Reverse a Linked List(Program)


Now I create/print a program that reverse the whole linked list.I made this program in c ++ but you can also creates it with java and c# also,with same concept which are given below.


#include<iostream.h>
#include<conio.h>
#include null 0
#include<stdio.h>
#include<stdlib.h>
struct link
{
int info;
struct link* next;
}*start,*node;
void rev(struct link*);
void create(struct link*);
void main()
{
create(start);
rev(start);
getch();
}
void create(struct link*node)
{
char ch;
clrscr();
start=null;
node=null;
do
{
if(node==null)
{
node=new link;
start=node;
}
else
{
node->next=node link;
node=node->next;
}
cout<<"enter information for node";
cin>>node->info;
Cout<<"want to continue";
flushall();
cin>>ch;
}while(ch!='n');
node->next=null;
}
void rev(struct link*node)
{
if node()
{
rev(node->next);//recursion 
cout<<node->info;
}
getch();
}



That's all about Reverse of the Linked List in data structure.


If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.

Doubly Linked List


After explaining singly linked list,circular linked list so now I explain you a doubly linked list.In singly linked list,each node has two parts i.e. info part/operation part and next pointer but in doubly linked list each node has 3 parts i.e. info,next pointer and pre part(so this is the main advantage of doubly linked list as compared to singly linked list).It is used in c ++,java,c# etc.

Example/implementation or Algorithm/program of Doubly linked  list:-



#include<iostream.h>

#include<conio.h>
#include null 0
#include<stdio.h>
#include<stdlib.h>

void main()

{
struct link
{
int info;
struct link* next*pre;
}*start,*node*last;
char ch;
clrscr();
start=null;
node=null;
do
{
if(node==null)
{
node=new link;
start=node;
node->pre=null;
}
else
{
node->next=node link;
node->next->pre=null;
node=node->next;
}
cout<<"enter information for node";
cin>>node->info;
Cout<<"want to continue";
flushall();
cin>>ch;
}while(ch!='n');
last=node;
node->next=null;
node=start;
cout<<"List=";
while(node)
{
cout<<node->info;
node=node->next;
}
node=last;
cout<<"Reverse list/reversing a list";
while (node)
{
cout<<node->info;
node=node->pre;

getch();
}




That's all about Doubly Linked List in data structure.



If you have any query then leave your comments and don't forgot to follow me on Google+,Facebook,Twitter.