#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,ptr,temp;
cout<<"\n------------ INSERTION SORT ------------ \n\n";
cout<<"Enter No. of Elements : ";
cin>>n;
cout<<"\nEnter Elements : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(p=1;p<n;p++) // Here the loop start from p=1, as we know that in insertion sort
// the actual passing start from the 2nd element and in C++ 0=1st element
// and 1=2nd element.
{
temp=a[p];
ptr=p-1;
while(ptr>=0&&temp<a[ptr]) // we set ptr>=0, because the selcted value will be compared with the previous
// element i.e element 2 will be compared with element 1(A[1] will be compared with A[0])
{
a[ptr+1]=a[ptr]; // Move Element Forward
a[ptr]=temp; // Insert Element in Proper Place
ptr--;
}
}
cout<<"\nAfter Sorting : \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
Tuesday, 8 April 2014
Data Structure: C++ Programming of Bubble Sort
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,j,temp;
cout<<"\n------------ BUBBLE SORT ------------ \n\n";
cout<<"Enter No. of Elements : ";
cin>>n;
cout<<"\nEnter Elements : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(p=0;p<n-1;p++) // Loop for Pass
{
for(j=0;j<n-p;j++) // The element will be compared with n-p elements.
// If we have 4 elements in the array, then for the first pass (p=0), the element will be
// compared with 3 elements i.e (n-p=3-0=3)
{
if(a[j]>a[j+1])
{
temp=a[j]; // Interchange Values
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nAfter Sorting : \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,j,temp;
cout<<"\n------------ BUBBLE SORT ------------ \n\n";
cout<<"Enter No. of Elements : ";
cin>>n;
cout<<"\nEnter Elements : \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(p=0;p<n-1;p++) // Loop for Pass
{
for(j=0;j<n-p;j++) // The element will be compared with n-p elements.
// If we have 4 elements in the array, then for the first pass (p=0), the element will be
// compared with 3 elements i.e (n-p=3-0=3)
{
if(a[j]>a[j+1])
{
temp=a[j]; // Interchange Values
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nAfter Sorting : \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
What is Computer Architecture?
What is Computer Architecture?
Computer Architecture is the conceptual design and fundamental operational structure of computer system.
Subscribe to:
Posts (Atom)