-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_With_pointer.c
More file actions
340 lines (336 loc) · 10.9 KB
/
Array_With_pointer.c
File metadata and controls
340 lines (336 loc) · 10.9 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include<stdio.h>
int arr[100];
int size,*i,*j,choice;
void create(int []);
void display(int[]);
void swap(int *a, int *b)
void delete(int []);
void insert(int []);
void modify(int []);
void linear(int []);
void binary(int[]);
void bubble_sort(int []);
void selection_sort(int []);
void insertion_sort(int []);
int main()
{
void create(int arr[])
{
printf("Enter the size of array ");
scanf("%d",&size);
printf("\nEnter the elements of array one at a time\n"); //creating array
for(int i=0;i<size;i++)
scanf("%d",&arr[i]);
}
void display(int arr[])
{
printf("\nIndex "); //displaying the index of array elements
for(i=0;i<size;i++)
printf("a[%d]\t",i);
printf("\nArray "); //displyaing array
for(i=arr;i<(size+arr);i++)
printf("%d\t",*i);
}
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void insert(int *arr)
{
int num1,postn;
printf("\nYou have selected INSERTION operation\n");
printf("\nEnter the element to be inserted: ");
scanf("%d",&num1);
printf("\nEnter the position where the element is to be inserted(First index is 0): ");
scanf("%d",&postn);
for(int i=0;i<=(size-postn-1);i++)
{
*(arr+size-i) = *(arr+size-i-1); //shifting elements to right
}
*(arr+postn) = num1;
size++;
printf("\nThe array after inserting the element: ");
for(i=arr;i<(size+arr);i++)
printf("%d\t",*i);
}
void modify(int *arr)
{
int num;
int chnge, positn;
printf("\nYou have selected MODIFICATION operation\n");
printf("\nEnter the element to be modified: ");
scanf("%d",&num);
printf("\nEnter modification: ");
scanf("%d",&chnge);
for(i=0;i<size;i++)
{
if(*(arr+i)==num) //searching the element in array which is to be modified
{
positn = (arr+i);
*(arr+positn) = chnge; //modification
}
}
printf("The modified array is: ");
for(i=arr;i<(size+arr);i++)
printf("%d\t",*i);
}
void delete(int arr[])
{
int elmnt; //deleting an element from array
printf("\nYou have selected DELETION operation\n");
printf("\nEnter the element to be deleted ");
scanf("%d",&elmnt);
for(i=0;i<size;i++)
{
if(*(arr+i)==elmnt) //searching the element entered in array
{
int j = (arr+i); //declaring the index of element to be deleted
for(i=j;j<size;i++) //shifting left including the element to be deleted
{
*(arr+i) = *(arr+i+1); //shifting algorithm
}
}
}
size--;
printf("\nThe array after deleting element ");
for(i=arr;i<(size+arr);i++)
printf("%d\t",*i);
}
void linear(int *arr)
{
int num1;
int count=0;
printf("\nEnter the element to be searched: "); //taking input the element to be searched
scanf("%d",&num1);
for(i=0;i<size;i++)
{
if(*(arr+i)==num1)
{
count++;
}
}
if(count==0)
{
printf("\nThe number %d is not present in the array",num1); //presence of element
}
else
{
printf("\nThe number %d is present in the array",num1);
}
printf("\nNumber of times the element occurs in array: %d",count); //count of number of occurrences
printf("\nThe index at which the element is occuring: ");
for(i=0;i<size;i++)
{
if(*(arr+i)==num1)
{
printf("\t%d",i); //displaying the index where the element is present
}
}
}
void binary(int *arr)
{
int lower,upper,mid,search,pass=1;
printf("\nEnter element you want to search:");
scanf("%d",&search);
lower=0;
upper=n-1;
mid=(upper+lower)/2;
int found=0;
int temp;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(*(arr+j)>*(arr+1+j))
{
swap((arr+j),(arr+1+j));
}
}
}
if(arr[mid]<search)
{
lower=mid+1;
mid=(upper+lower)/2;
}
else if(arr[mid]==search)
{
printf("\n\n%d is found. ",search);
found=1;
break;
}
else
{
upper=mid-1;
mid=(upper+lower)/2;
}
if(found!=1)
printf("\n\n%d not found in the list",search)
if(found!=1)
printf("\n%d not found in the list",search);
}
void bubble_sort(int arr[])
{
int temp, swap, num;
int count=0,pass=0,itn=0,comp=0;
for(int i=0;i<size-1;i++) //for passes
{
printf("\n-------------------------------------------------------------------------------------");
printf("\nPass %d :",i+1);
pass++;
for(j=0;j<size-i-1;j++) //for iteration and swapping wherever required
{
printf("\nIteration %d: ",j+1);
itn++;
if(*(arr+j)>*(arr+j+1))
{
swap = 1;
count=count+1; //counting number of swaps
comp++; //counting number of comparisons
swap((arr+j),(arr+j+1)); //swapping
for(num=arr;num<(size+arr);num++)
printf("%d\t",*num);
printf("\tSwap");
}
else
{
swap = 0;
for(num=arr;num<(size+arr);num++)
printf("%d\t",*num);
}
}
}
printf("\n-------------------------------------------------------------------------------------");
printf("\nSorted Array: ");
for(i=arr;i<(size+arr);i++) //displaying sorted array
printf("%d\t",*i);
if(count==0)
printf("\nThis is the BEST CASE condition");
else if(count==itn)
printf("\nThis is the WORST CASE conditon");
else
printf("\nThis is the AVERAGE CASE condition");
printf("\nNumber of swaps: %d",count);
printf("\nNumber of Passes: %d",pass);
printf("\nNumber of iterations: %d", itn);
}
void selection_sort(int *arr)
{
int min,num,a,b,swaps,pass=0,swapnum=0;
for(i=0;i<size-1;i++)
{
min = i; //assuming element at index i to be smallest
for(int j=i+1;j<size;j++)
{
if(*(arr+min)>*(arr+j)) //swapping
{
min = j;
swaps = 0;
pass++;
}
}
swap((arr+j), (arr+i));
printf("\n------------------------------------------------------------");
printf("\nPass %d: ",i+1); //printing passes
for(num=arr;num<(size+arr);num++)
printf("%d\t",*num);
printf("\nMin: %d", arr[min]);
}
printf("\n------------------------------------------------------------");
printf("\nSorted Array ");
for(i=arr;i<(size+arr);i++)
printf("%d\t",*i);
if(swapnum==size-1)
printf("\nThis is WORST CASE");
else if(swapnum==0)
printf("\nThis is BEST CASE");
else
printf("\nThis is AVERAGE CASE");
printf("\nPasses: %d",pass);
printf("\nSwaps: %d",swapnum);
}
void insertion_sort(int *arr)
{
int i, j;
int temp,num;
for(i=1;i<size;i++)
{
temp = *(arr+i);
for(j=i-1;j>=0;j--)
{
if(*(arr+j)>temp)
{
*(arr+j+1) = *(arr+j);
}
else
break;
insert++;
}
*(arr+j+1) = temp;
printf("\n------------------------------------------------------------");
printf("\nPass %d: ",i);
for(num=arr;num<(size+arr);num++)
printf("%d\t",*num);
}
printf("\n------------------------------------------------------------");
printf("\nSorted Array ");
for(i=arr;i<(size+arr);i++)
printf("%d\t",*i);
printf("\t%d",arr[i]);
if(insert==size-1)
printf("\nThis is WORST CASE");
else if(insert==0)
printf("\nThis is BEST CASE");
else
printf("\nThis is AVERAGE CASE");
printf("\nPasses: %d",size-1);
printf("\nInsertion: %d",insert);
}
create(arr);
display(arr);
do
{
printf("\n(1) Enter 1 to insert element in array \n(2) Enter 2 to delete the element \n(3) Enter 3 to modify the element \n(4) Enter 4 to search the element");
printf("\n(5) Enter 5 to sort the elements in array \n(6) Enter 6 to Quit ");
printf("\nEnter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: insert(arr);break;
case 2: delete(arr);break;
case 3: modify(arr);break;
case 4:
{
printf("\n(1) Enter 1 to apply LINEAR SEARCH \n(2) Enter 2 to apply BINARY SEARCH");
char choice1;
printf("\nEnter the choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: linear(arr);break;
case 2: binary(arr);break;
}
break;
}
case 5:
{
printf("\n(1) Enter 1 to apply BUBBLE SORT \n(2) Enter 2 to apply SELECTION SORT \n(3) Enter 3 to apply INSERTION SORT");
char choice2;
printf("\nEnter the choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: bubble_sort(arr);break;
case 2: selection_sort(arr);break;
case 3: insertion_sort(arr);break;
default: printf("\nINVALID INPUT entered");
}
break;
}
default:
printf("\nPROGRAM ENDED");break;
}
}
while (choice != 6);
}