Search here

Pages

Saturday, August 13, 2011

Pass by Address(swapping)

Program for Passby Address-PARAMETER PASSING 
#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
void main()
{
int a,b;
cout<<"Enter any two numbers";
cin>>a>>b;
swap(a,b);
cout<<"\n the swapped valuesin the main funtion are"<<a<<"and"<<b;
getche();
}
void swap(int &x,int &y)
{
int t;
t=x;
x=y;
y=t;
cout<<"\n the  swapped values in the function are"<<x<<"and"<<y;

}
Read Full

Pass by reference(swapping)

 Program for Passby reference-PARAMETER PASSING
#include<iostream.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
cout<<"Enter any two numbers";
cin>>a>>b;
swap(&a,&b);
cout<<"\n the swapped valuesin the main funtion are"<<a<<"and"<<b;
getche();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
cout<<"\n the  swapped values in the function are"<<  *x<<"and"<<     *y;

}

Read Full

Pyramid of Numbers

Have you ever tried doing some pyramid of numbers as in the given
    1
    123
     12345
     123
     1
The program here works for at most 7 digits
Program compiled and executed in turboc++ 
#include<iostream.h>
#include<conio.h>
void main()
{
long int i=0,n,j=1,k;
clrscr();
cout<<"Enter any Odd digit number\t";
R:cin>>n;
if((n%2)==0)
{
cout<<"The number you entered is not odd.Enter an odd number\n";
goto R;
}
else
{
cout<<"The Pyramid sequence is\n";
for(k=0;k<=((n-1)/2);k++)
{
for(int x=0;x<=(((n-1)/2)-k);x++)
cout<<" ";

i=(i*10)+(j+k);
cout<<i<<endl;
j++;
i=(i*10)+(j+k);
}

i=(i/10);
int y=1;
for(k=((n+1)/2);k<n;k++)
{
y++;
int z=y;
do
{
cout<<" ";
z--;
}while(z!=0);
i=(i/100);
cout<<i<<endl;
}
}
getch();
}
Read Full

Insertion Sort Using Functions

Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

  • Simple implementation
  • Efficient for (quite) small data sets
  • Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
  • More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
  • Stable; i.e., does not change the relative order of elements with equal keys
  • In-place; i.e., only requires a constant amount O(1) of additional memory space
  • Online; i.e., can sort a list as it receives it
Logic : Here, sorting takes place by inserting a particular element at the appropriate position, that’s why the name-  insertion sorting. In the First iteration, second element A[1] is compared with the first element A[0]. In the second iteration third element is compared with first and second element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position up and inserts the desired element at the suitable position. This procedure is repeated for all the elements in the list.
If we complement the if condition in this program, it will give out the sorted array in descending order.
When humans manually sort something (for example, a deck of playing cards), most use a method that is similar to insertion sort.
Animation showing the algorithm for Insertion sort :
A brief view on how the logic Proceeds :



Program for Insertion Sort Compiled and Executed in turbo c++ 
#include<iostream.h>
#include<conio.h>
void insertionsort(int a[],int)   ;
void main()
{
int n,i,j,k,a[30];
cout<<"enter the no of elements you want to enter";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++)
cin>>a[i];
insertionsort(a,n);
getch();
}
void insertionsort(int array[],int size)
{
int i, j, key;

for (j=1; j<size; j++)
{
key= array[j];
i=j-1;
while((i>=0)&&(array[i]>key))
{
array[i+1]= array[i];
i=i-1;
}
array[i+1]=key;
}
cout<<"sorted array is";
for(i=0;i<size;i++)
cout<<array[i]<<"\t";
}
Class Sorting algorithm
Data structure Array
Worst case performance О(n2)
Best case performance O(n)
Average case performance О(n2)
Worst case space complexity О(n) total, O(1) auxiliary
 
Read Full

Bubble Sorting

Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, it is not efficient for sorting large lists; other algorithms are better.

Performance:

Bubble sort has worst-case and average complexity both О(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other О(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.
                     The only significant advantage that bubble sort has over most other implementations, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted is efficiently built into the algorithm. Performance of bubble sort over an already-sorted list (best-case) is O(n). By contrast, most other algorithms, even those with better average-case complexity, perform their entire sorting process on the set and thus are more complex. However, not only does insertion sort have this mechanism too, but it also performs better on a list that is substantially sorted (having a small number of inversions).

An Animated Example:



Program for Bubble sort compiled and executed in turbo C++  
#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
void main()
{
int a[30],i,j,k;
clrscr();
cout<<"Enter the No of elements yo want to enter";
cin>>k;
cout<<"Now enter elements";
for(i=0;i<k;i++)
cin>>a[i];
for(i=0;i<k;i++)
{
for(j=0;j<k-i;j++)
{
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<"The sorted order is";
for(i=0;i<k;i++)
cout<<a[i]<<"\t";
getch();
} 
Buuble Sort-Overview
Class Sorting algorithm
Data structure Array
Worst case performance O(n2)
Best case performance O(n)
Average case performance O(n2)
Worst case space complexity O(1) auxiliary


Read Full