A world of Programming- Gsbprogramming
By Gurpreet Singh, Department of Computer Science & Engineering, Amritsar College of Engineering & Technology
Thursday 6 August 2015
Add Lyrics to mp3 file using Windows Media Player in simple steps
Tuesday 4 August 2015
Applet Demo 1
import java.applet.*;
import java.awt.*;
public class Demo1 extends Applet
{
public void init()
{
//Code to run automatically
this.setSize(500, 500);
this.setBackground(Color.CYAN);
}
public void paint(Graphics g)
{
//now draw graphics
g.drawLine(0, 0, 100, 100);
g.drawRect(0, 0, 100, 100);
//change color of graphics
g.setColor(Color.blue);
g.drawString("Applet Demo", 0, 120);
}
}
Monday 3 August 2015
Getting Started with Java GUI Programming
Download the Netbeans IDE from filehippo.com/download_netbeans
Also download Java JDK(32-bit or 64-bit) according to your operating system. For 32-bit operating system download the 32-bit version of Java JDK and for 64-bit operating system download the 64-bit version of Java JDK
After downloading firstly install Java JDK and then Netbeans
Saturday 1 August 2015
Binary Search Program
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,*a,item,loc=-1;
cout<<"\nenter the no. of elements :";
cin>>n;
a=new int[n]; //dynamically initialize array
cout<<"\nenter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"\nenter the element you want to search: ";
cin>>item;
int mid,end=n,beg=0;
while(beg<=end&&loc==-1)
{
mid=(beg+end)/2;
if(a[mid]==item)
loc=mid;
else if(a[mid]<item)
beg=mid+1;
else
end=mid-1;
}
if(loc!=-1)
cout<<"\nlocation of "<<item<<" is:"<<loc+1;
else
cout<<"\n"<<item<<" not found!!!";
getch();
}
Linear Search Program
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int *a,n,loc=-1,item;
cout<<"\nenter the no. of elements (max. 10):";
cin>>n;
a=new int[n]; //dynamically initialize array
cout<<"\nenter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"\nenter the element to be searched: ";
cin>>item;
for(i=0;i<n;i++)
if(a[i]==item)
{ loc=i;
break;
}
if(loc==-1)
cout<<"\n"<<item<<" not found!!!";
else
cout<<"location of "<<item<<" is: "<<loc+1;
getch();
}
Selection Sort Program
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,temp,min;
cout<<"\nEnter the no. of elements you want:";
cin>>n;
int *a=new int[n]; //dynamically initialize array
cout<<"\nEnter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
If(a[min]>a[j])
min=j;
}
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
cout<<"\nSorted list of given integers is(in ascending order) :\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
Bubble Sort Program
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,temp;
cout<<"\nEnter the no. of elements you want:";
cin>>n;
int *a=new int[n]; //dynamically initialize array
cout<<"\nEnter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nSorted list of given integers is(in ascending order) :\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
Merge Sort Program
#include<iostream.h>
#include<conio.h>
#include<values.h>
void merge(int a[],int p,int q,int r)
{
int n1=q-p+1,n2=r-q;
int *l=new int[n1+1];
int *rt=new int[n2+1];
for(int i=0;i<n1;i++)
l[i]=a[p+i];
for(int j=0;j<n2;j++)
rt[j]=a[q+j+1];
l[n1]=MAXINT; //SENTINEL
rt[n2]=MAXINT; //SENTINEL
i=j=0;
for(int k=p;k<=r;k++)
{
if(l[i]<=rt[j])
{
a[k]=l[i];
i+=1;
}
else
{
a[k]=rt[j];
j+=1;
}
}
}
void mergesort(int a[],int p,int r)
{
int q;
if(p<r)
{
q=(p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r);
merge(a,p,q,r);
}
}
void main()
{
clrscr();
int n;
cout<<"\nEnter the no. of elements you want:";
cin>>n;
int *a=new int[n]; //dynamically initialize array
cout<<"\nEnter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
mergesort(a,0,n-1);
cout<<"\n\nSORTED ARRAY IS:\n";
for( i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
Quick Sort Program
#include<iostream.h>
#include<conio.h>
int partition(int a[],int p,int r)
{
int x,i,temp;
x=a[r];
i=p-1;
for(int j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
temp=a[i+1];
a[i+1]=a[j];
a[j]=temp;
return i+1;
}
void quicksort(int a[],int p,int r)
{
int q;
if(p<r)
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
}
void main()
{
clrscr();
int *a,n;
cout<<"\nEnter the no. of element you want:";
cin>>n;
a=new int[n]; //dynamically initialize array
cout<<"\nEnter the elements:\n";
for(int i=0;i<n;i++)
cin>>a[i];
quicksort(a,0,n-1);
cout<<"\nSorted Array is:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
Heap Sort Program
#include<iostream .h>
#include<conio .h>
int heapsize,length;
void exchange(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void maxheapify(int a[],int i)
{
int l,r,largest,temp;
l=2*i; //left[i]
r=2*i+1; //right[i]
if(l<=heapsize&&a[l]>a[i])
largest=l;
else
largest=i;
if(r<=heapsize&&a[r]>a[largest] )
largest=r;
if(largest!=i)
{
exchange(a[i],a[largest]);
maxheapify(a,largest);
}
}
void buildmaxheap(int a[])
{
heapsize=length;
for(int i=length/2;i>=1;i--)
maxheapify(a,i);
}
void heapsort(int a[])
{
buildmaxheap(a);
for(int i=length;i>=2;i--)
{
exchange(a[1],a[i]) ;
heapsize-=1;
maxheapify(a,1);
}
}
void main()
{
clrscr();
int *a,n;
cout<<"\nEnter the no of element you want(max 20):";
cin>>n;
length=n;
a=new int[n+1]; //DYNAMICALLY INITIALISE ARRAY,AS ARRAY MUST START FROM INDEX 1 SO SIZE IS (N+1)
cout<<"\nEnter the elements:\n";
for(int i=1;i<=n;i++)
cin>>a[i];
heapsort(a);
cout<<"\nSorted Array is:\n";
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
getch();
}
Subscribe to:
Posts (Atom)