-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayOperationMenu.cpp
245 lines (211 loc) · 5.43 KB
/
ArrayOperationMenu.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <iostream>
using namespace std;
struct Array
{
int *A;
int size;
int length;
};
void Display(struct Array a)
{
int i;
cout << "element are : ";
for (i = 0; i < a.length; i++)
cout << a.A[i]<<" ";
}
void Insert(struct Array *a, int index, int x)
{
int i;
if (index >= 0 && index <= a->size)
{
for (i = a->length; i > index; i--)
a->A[i] = a->A[i - 1];
a->A[index] = x;
a->length++;
}
cout<<" Index : "<<index<<" And Element of Array : "<<a->A[index];
}
int Deletes(struct Array *a, int index)
{
int x = 0;
int i;
if (index >= 0 && index < a->length)
{
x = a->A[index];
for (i = index; i < a->length - 1; i++)
a->A[i] = a->A[i + 1];
a->length--;
return x;
}
return 0;
}
void swap(int *a, int *b)
{
int *temp = a;
a = b;
b = temp;
}
int LinearSearch(struct Array *a, int element)
{
for (int i = 0; i < a->length; i++)
{
if (element == a->A[i])
{
swap(&a->A[i], &a->A[0]);
return i;
}
}
return -1;
}
int BinarySearch(struct Array *a, int element, int low, int high)
{
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (element == a->A[mid])
return mid;
else if (element < a->A[mid])
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
int RBinSearch(struct Array *a, int element, int low, int high)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (element == a->A[mid])
return mid;
else if (element < a->A[mid])
RBinSearch(a, element, low, high = mid - 1);
else
RBinSearch(a, element, low = mid + 1, high);
}
return -1;
}
void Reverse(struct Array *a)
{
int *b = new int[a->length];
for (int i = a->length - 1, j = 0; i >= 0; i--, j++)
b[j] = a->A[i];
for (int i = 0; i < a->length; i++)
a->A[i] = b[i];
cout<<"Reverse of Array : ";
for(int i=0;i<a->length;i++)
cout<<a->A[i]<<" ";
}
void InsertSort(struct Array *a, int x)
{
int i = a->length - 1;
if (a->length == a->size)
exit(0);
while (i >= 0 && a->A[i] > x)
{
a->A[i + 1] = a->A[i];
i--;
}
a->A[++i] = x;
a->length++;
cout<<"Added at Index : "<<i<<" And Element is : "<<a->A[i]<<endl;
}
bool sorted(struct Array *a)
{
bool flag = true;
for (int i = 0; i < a->length-1; i++)
{
if (a->A[i] > a->A[i + 1])
{
flag = false;
break;
}
}
return flag;
}
int main()
{
struct Array ob;
int ch;
int low = 0,high;
int element, index;
cout << "Enter size : ";
cin >> ob.size;
ob.A = new int[ob.size];
ob.length = 0;
do
{
cout << "\n<-----*** Menu ***-----> \n";
cout << "1. Insert \n";
cout << "2. Sorted Insert \n";
cout << "3. Delete \n";
cout << "4. Array Reverse\n";
cout << "5. Search \n";
cout << "6. Display\n";
cout << "7. Checking sorted Array\n";
cout << "8. Exit \n";
cout << "Enter your choice : ";
cin >> ch;
switch (ch)
{
case 1:
cout << "Enter index and element for insertion : ";
cin >> index >> element;
Insert(&ob, index, element);
high = ob.length-1;
break;
case 2:
cout << "Enter element for insertion : ";
cin >>element;
InsertSort(&ob,element);
high = ob.length-1;
break;
case 3:
cout << "Enter index for deletion : ";
cin >> index;
Deletes(&ob, index);
break;
case 4:
Reverse(&ob);
break;
case 5:
cout << "There are three search \n A. Linear search \n B. Binary search \n C. Binary search by Recursion \n";
cout << "Enter your choice for required search : ";
char chh;
cin >> chh;
switch (chh)
{
case 'A':
cout << "Enter element for your required search : ";
cin >> element;
cout<<"Index at that element : "<<LinearSearch(&ob, element);
break;
case 'B':
cout << "Enter element for your required search : ";
cin >> element;
cout<<"Index at that element : "<<BinarySearch(&ob, element, low, high);
break;
case 'C':
cout << "Enter element for your required search : ";
cin >> element;
cout<<"Index at that element : "<<RBinSearch(&ob, element, low, high);
break;
}
break;
case 6:
Display(ob);
break;
case 7:
if(sorted(&ob))
cout<<"\nArray is sorted\n";
else
cout<<"\nArray is not sorted";
break;
case 8:
cout << "exit";
exit(0);
}
}while(ch<10);
return (0);
}