-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_operation.c
More file actions
270 lines (264 loc) · 6.72 KB
/
Copy patharray_operation.c
File metadata and controls
270 lines (264 loc) · 6.72 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int size=0,i=0,len=0;
int insert(int list[]);
int delete(int list[]);
int search(int list[]);
int sort(int list[]);
int merge(int list1[],int list2[]);
void main()
{
char ch,op;
int *pt,*pt2,j=0,k=0;
printf("Enter the size of the array:\n");
scanf("%d",&size);
pt= (int*)malloc(size*sizeof(int));
printf("Enter the elements in the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&pt[i]);
}
start:printf("Array operation\n");
printf("Enter 'I' for Inserting, 'D' for Deletion, 'S'for Searching, 'O' for Arrenging, 'M' for Merging\n");
printf("Enter your choice:\n");
scanf(" %c",&ch);
do
{
switch(toupper(ch))
{
case 'I':
{
insert(pt);
break;
}
case 'D':
{
delete(pt);
break;
}
case 'O':
{
sort(pt);
break;
}
case 'S':
{
search(pt);
break;
}
case 'M':
{
printf("Enter the size of the second array:\n");
scanf("%d",&len);
pt2=(int *)malloc(len*sizeof(int));
printf("Enter the elements of the second array:\n");
for(j=0;j<len;j++)
{
scanf("%d",&pt2[j]);
}
merge(pt,pt2);
break;
}
default:
{
printf("Wrong choice");
}
}
printf("\nDo you want to continue?\n");
printf("Press 'y' for 'yes' and 'n' for 'no'\n");
scanf(" %c",&op);
fflush(stdin);
if(op=='n'||op=='N')
{
break;
}
if(op!='y'||op!='Y')
{
goto start;
}
}
while(op=='y'||op=='Y');
printf("You have exited!");
system("pause");
}
int insert(int list[])
{
int pos=0,ele=0;
printf("Enter the position of insertion\n");
scanf("%d",&pos);
printf("Enter the element to be inserted\n");
scanf("%d",&ele);
for(i= (size-1); i>=pos; i--)
{
list[i+1]= list[i];
}
list[pos]=ele;
size++;
for(i=0;i<size;i++)
{
printf("%d ",list[i]);
}
}
int delete(int list[])
{
int pos=0;
printf("Enter the position to be deleted:\n");
scanf("%d",&pos);
for(i=pos;i<size;i++)
{
list[i-1]=list[i];
}
size--;
for(i=0;i<size;i++)
{
printf("%d ",list[i]);
}
}
int search(int list[])
{
char ch;
int ub=size-1,lb=0,mid=0,find=0,flag=0,i=0;
printf("Enter 'B' for binary search and 'L' for linear search\n");
scanf(" %c",&ch);
printf("Enter the number to be searched\n");
scanf("%d",&find);
switch(toupper(ch))
{
case 'B':
{
while(lb<=ub)
{
mid=(lb+ub)/2;
if(list[mid]>find)
{
ub= mid-1;
}
if(list[mid]<find)
{
lb= mid+1;
}
if(list[mid]==find)
{
flag=1;
break;
}
}
if(flag==1)
{
printf("Number found");
}
else
{
printf("Number not found at position: %d",mid);
}
break;
}
case 'L':
{
for(i=0;i<=size;i++)
{
if(find==list[i])
{
flag=1;
break;
}
}
if(flag==1)
{
printf("Number found at position: %d",mid);
}
else
{
printf("Number not found");
}
break;
}
default:
{
printf("Wrong choice");
}
}
}
int sort(int list[])
{
char a,ch;
int j=0,temp_str=0;
printf("press 'A' for Ascending arrangemrnt and 'D' for descending arrangment\n");
scanf(" %c",&a);
if(a=='A'||a=='a')
{
printf("Press 'B' for Bubble sort,Press 'S' for Selection sort,Press 'I' for Insertion sort,\nPress 'H' for Heap sort,Press 'Q' for Quick sort,Press 'M' for Merge sort,\nPress 'R' for Radix sort,Press 'C' for Counting sort,Press 'O' for Bucket sort,\nPress 'P' for Shell sort\n");
scanf(" %c",&ch);
switch(ch)
{
case 'B':
{
for(i=0;i<size;i++)
{
for(j=0;j<(size-i-1);j++)
{
if(list[j+1]<list[j])
{
temp_str= list[j];
list[j]= list[j+1];
list[j+1]=temp_str;
}
}
}
for(i=0;i<size;i++)
{
printf("%d ",list[i]);
}
}
}
}
else if(a=='D'||a=='d')
{
printf("Press 'B' for Bubble sort,Press 'S' for Selection sort,Press 'I' for Insertion sort,\nPress 'H' for Heap sort,Press 'Q' for Quick sort,Press 'M' for Merge sort,\nPress 'R' for Radix sort,Press 'C' for Counting sort,Press 'O' for Bucket sort,\nPress 'P' for Shell sort\n");
scanf(" %c",&ch);
switch(toupper(ch))
{
case 'B':
{
for(i=0;i<size;i++)
{
for(j=0;j<(size-i-1);j++)
{
if(list[j+1]>list[j])
{
temp_str= list[j];
list[j]= list[j+1];
list[j+1]=temp_str;
}
}
}
for(i=0;i<size;i++)
{
printf("%d ",list[i]);
}
}
}
}
else
{
printf("Wrong choice");
}
}
int merge(int list1[],int list2[])
{
int list3[100],j=0,m=0;
for(i=0;i<size;i++)
{
list3[i]=list1[i];
}
for(j=0;j<len;j++)
{
list3[i+j]=list2[j];
}
printf("The merged array is:\n");
for(m=0;m<(i+j);m++)
{
printf("%d ",list3[m]);
}
}