/*A program to impelement one dimentional array operations.
compiler- Turbo c++ author- Mangilal Sharma
--------------------------------------------------------*/
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
class array1d
{
private:
int a[20],b[20],min,max,loc,m,n,i,j,k,p,choice;
public:
int insertion();
void traverse(int);
void deletion();
void merging();
void searching();
void sorting();
array1d();
int Partition(int low,int high,int arr[]);
void Quick_sort(int low,int high,int arr[]);
void heap_sort();
void merge(int top,int size, int bottom);
void merge_sort(int low,int high);
};
int array1d::insertion()
{
cout<<"Enter no. of elements for array:";
cin>>n;
cout<<"Enter the elements for array:\n";
for(int i=0;i<n;i++)
{
cout<<"Enter element"<<i+1<<" ";
cin>>a[i];
b[i]=a[i];
}
traverse(n);
return n;
}
void array1d::traverse(int n)
{
if(n>0)
{
cout<<" Elements are:-\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
}
else cout<<"Array is empty";
return;
}
void array1d::deletion()
{
cout<<"Enter no. that you want to delete:";
cin>>p;k=0;
for(i=0;i<n;i++)
if(a[i]==p)
{
k=1;break;
}
if(k==1)
{
cout<<"no.'"<<p<<"' is deleted";
for(i;i<n;i++) a[i]=a[i+1];n=n-1;
cout<<"\nAfter Deletion :";traverse(n);
}
else cout<<"element not present in array";
}
void array1d::searching()
{
cout<<"Enter no. that you want to Search:";
cin>>p;k=0;
for(i=0;i<n;i++)
if(a[i]==p)
{
k=1;break;
}
if(k==1) cout<<p<<"Present in the array at location"<<i;
else cout<<"element not present in array";
}
void array1d::merging()
{
if(n==0)
{
cout<<"First must be enter 1st array elements\n";
insertion();
}
cout<<"\nEnter no. of elements for 2nd array1d:";
cin>>m;
cout<<"Enter the elements for 2nd array:\n";
for(i=0;i<m;i++)
{
cout<<"Enter element"<<i+1<<" ";
cin>>b[i];
}
for(i=n,j=0;i<n+m;i++,j++) a[i]=b[j];
n=m+n;
for(i=0;i<n;i++) b[i]=a[i];
cout<<"Merged array is:\n";
traverse(n);
}
void array1d::sorting()
{
cout<<"1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n";
cout<<"4. Quick Sort\n5. Heap Sort\n6. Merge Sort\n7. Main Page\n8. Exit";
cout<<"\nEnter Your Choice";
cin>>choice;
cout<<"unsorted";traverse(n);
switch(choice)
{
case 1: //bubble sort
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
p = a[j];
a[j] = a[j+1];
a[j+1]=p;
}
}
}
cout<<"\nSorted";traverse(n);break;
case 2: //selection sort
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for (j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
p=a[i];
a[i]=a[loc];
a[loc]=p;
}cout<<"\nSorted";traverse(n);break;
case 3: //Insertion Sort
for(i=1;i<=n-1;i++)
{
p=a[i];
j=i-1;
while((p<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=p;
}cout<<"\nSorted";traverse(n);break;
case 4: //quick sort
int high=n-1;
int low=0;
Quick_sort(low,high,a);
cout<<"\nSorted";traverse(n);break;
case 5: //heap sort
for(k=2;k<=n;++k)
{
i=k;
p=a[k];
j=i/2;
while((i>1)&&(p>a[j]))
{
a[i]=a[j];
i=j;
j=i/2;
if(j<1) j=1;
}
a[i]=p;
}
heap_sort();break;
case 6: //merge sort
if(n>0) merge_sort(0,n-1);
cout<<"\nSorted";traverse(n);break;
case 7:
return;
case 8:
exit(1);
}
for(i=0;i<n;i++)
a[i]=b[i];
}
array1d::array1d()
{
m=0,n=0,k=0;
do
{
cout<<"\n\t\tONE-DIMENTIONAL ARRAY\n";
cout<<"\t\t1. To Insert an Element\n";
cout<<"\t\t2. To Delete an Element\n";
cout<<"\t\t3. To Display the Elements\n";
cout<<"\t\t4. To Search an item\n";
cout<<"\t\t5. To Merge the array\n";
cout<<"\t\t6. To Sort the elements\n";
cout<<"\t\t7. To Main page\n";
cout<<"\t\t8. To Exit from Program\n";
cout<<"\n\t\t Enter Your Choice= ";
cin>>choice;
switch(choice)
{
case 1: n=insertion();break;
case 2: deletion();break;
case 3: traverse(n);break;
case 4: searching();break;
case 5: merging();break;
case 6: sorting();break;
case 7: return;
case 8: exit(1);
default:cout<<"\n\t\tWrong Choice, Enter Again\n";
}
}while(1);
}
int array1d::Partition(int low,int high,int arr[])
{
int high_vac,low_vac,pivot;
pivot=arr[low];
while(high>low)
{
high_vac=arr[high];
while(pivot<high_vac)
{
if(high<=low) break;
high--;
high_vac=arr[high];
}
arr[low]=high_vac;
low_vac=arr[low];
while(pivot>low_vac)
{
if(high<=low) break;
low++;
low_vac=arr[low];
}
arr[high]=low_vac;
}
arr[low]=pivot;
return low;
}
void array1d::Quick_sort(int low,int high,int arr[])
{
int Piv_index;
if(low<high)
{
Piv_index=Partition(low,high,arr);
Quick_sort(low,Piv_index-1,arr);
Quick_sort(Piv_index+1,high,arr);
}
}
void array1d::heap_sort()
{
int step=1;
for(k=n;k>=2;--k)
{
p=a[1];
a[1]=a[k];
a[k]=p;
i=1;
int value=a[1];
j=2;
if((j+1)<k)
if(a[j+1]>a[j])
j++;
while((j<=(k-1))&&(a[j]>value))
{
a[i]=a[j];
i=j;
j=2*i;
if((j+1)<k)
if(a[j+1]>a[j])
j++;
else
if (j>n)
j=n;
a[i]=value;
}
cout<<"\nStep"<<step<<"\t";
step++;
for(p=1;p<=n;p++)
cout<<a[p]<<"\t";
}
}
void array1d::merge(int top,int size,int bottom)
{
int temp[40],f=top,s=size+1,t=top,upper;
while((f<=size)&&(s<=bottom))
{
if(a[f]<=a[s])
{
temp[t]=a[f];
f++;
}
else
{
temp[t]=a[s];
s++;
}
t++;
}
if(f<=size)
{
for(f=f;f<=size;f++)
{
temp[t]=a[f];
t++;
}
}
else
{
for(s=s;s<=bottom;s++)
{
temp[t]=a[s];
t++;
}
}
for(upper=top;upper<=bottom;upper++)
{
a[upper]=temp[upper];
}
}
void array1d::merge_sort(int low,int high)
{
if(low!=high)
{
int mid=(low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
/*=========================1Nov.,2010=============================*/
No comments:
Post a Comment
You are welcome, your comment helps me to make this blog more better.