1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include<iostream>
#include<conio.h>
using namespace std;
int lsearch(int[], int* , int*);
int b_search(int[], int*, int*);
void deletion(int[], int*, int*);
void insertion(int[], int*, int*, int*);
void bubble_sort(int[], int*);
void display(int[], int*);
int a[20], i, n, j, temp, k, l, item;
int main()
{
char ch;
cout<<"How many Array Elements??"<<endl;
cin>>n;
cout<<"Enter the array elements"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Do you want to perform any operation?"<<endl;
cin>>ch;
while(ch=='y'||ch=='Y')
{
cout<<"Which operation do you want to perform??"<<endl
<<"Press 1 for linear search"<<endl<<"Press 2 for binary search"<<endl<<
"Press 3 for deletion"<<endl<<"Press 4 for insertion"<<endl
<<"Press 5 for sorting the elements"<<endl<<"Press 6 for displaying array"
<<endl<<endl;
cin>>l;
switch(l)
{
case 1:
cout<<"Enter the element you want to search"<<endl;
cin>>item;
lsearch(&a[0], &n, &item);
break;
case 2:
cout<<"Enter the element to be searched"<<endl;
cin>>item;
b_search(&a[0], &n, &item);
break;
case 3:
cout<<"Enter the element you want to delete"<<endl;
cin>>item;
deletion(&a[0], &n, &item);
break;
case 4:
cout<<"Enter the new element you want to insert"<<endl;
cin>>item;
cout<<"Enter the element index"<<endl;
cin>>k;
insertion(&a[0], &n, &item, &k);
break;
case 5:
bubble_sort(&a[0], &n);
break;
case 6:
display(&a[0], &n);
break;
default:
cout<<"Sorry!!! No option matched"<<endl;
}
cout<<"Do you wish to continue??"<<endl;
cin>>ch;
}
return 0;
}
int lsearch(int a[], int *n_a, int *item_a)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==item)
{
cout<<"Item found at "<<i+1<<" position"<<endl;
break;
}
}
if(i==n)
{
cout<<"Item not found"<<endl;
}
}
int b_search(int a[], int *n_a, int *item_a)
{
int beg, endd, mid;
beg=0;
endd=n-1;
while(beg<=endd)
{
mid=(beg+endd)/2;
if(item==a[mid])
{
cout<<"Item found at the "<<mid+1<<" position"<<endl;
break;
}
else if(item>a[mid])
{
beg=mid+1;
}
else
{
endd=mid-1;
}
}
if(beg>endd)
{
cout<<"Item not found"<<endl;
}
}
void deletion(int a[],int *n_a, int *item_a)
{
int i, k;
for(i=0; i<n; i++)
{
if(a[i]==item)
{
k=i;
break;
}
}
i=1;
for(i=k;i<n; i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<"The array after element deletion is "<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void insertion(int a[], int *n_a, int *item_a, int *k_a)
{
int i;
i=n-1;
while(i>=k-1)
{
a[i+1]=a[i];
i--;
}
a[k-1]=item;
n=n+1;
cout<<endl<<endl<<"Now after inserting, the array is:"<<endl;
for(i=0; i<n; i++)
{
cout<<a[i]<<endl;
}
}
void bubble_sort(int a[], int *n_a)
{
int i, j, temp;
cout<<"Doing Bubble Sort:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
}
void display(int a[], int *n_a)
{
int i;
cout<<"Displaying the array"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
|