-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Without_pointer.c
More file actions
264 lines (263 loc) · 8.21 KB
/
Array_Without_pointer.c
File metadata and controls
264 lines (263 loc) · 8.21 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
#include <stdio.h>
#include <stdlib.h>
int arr_size = 0;
int main();
void flow (){
char choice;
printf("\n\nDo you want to continue (Y | N): ");
fflush(stdin);
scanf("%c", &choice);
if (choice == 'Y' || choice == 'y')
main();
else
exit(0);
}
void display_rev(int dis_rev[] ){
printf("\nYour Array elements are: ");
int i;
for(i = arr_size - 1; i >= 0; i--){
printf("%d ", dis_rev[i]);
}
}
void display(int dis[]){
int i;
printf("\nYour Array elements are: ");
for(i = 0; i < arr_size; i++){
printf("%d ", dis[i]);
}
}
void insert(int arr_insert[]){
int ins_ele, pos;
printf("\nEnter element you want to insert: ");
scanf("%d", &ins_ele);
printf("\nEnter postion at which you want insert this element: ");
scanf("%d", &pos);
arr_size++;
int i ;
for (i= arr_size - 2; i >= pos - 1; i--){
arr_insert[i + 1] = arr_insert[i];
}
arr_insert[pos - 1] = ins_ele;
display(arr_insert);
flow();
}
void delete(int arr_delete[]){
int pin;
display(arr_delete);
printf("\n\nEnter position of element you want to delete: ");
scanf("%d", &pin);
int i;
for (i= pin; i < arr_size; i++){
arr_delete[i - 1] = arr_delete[i];
}
arr_size--;
display(arr_delete);
flow();
}
void modify(int arr_mod[]){
int pin, mod_ele;
display(arr_mod);
printf("\n\nEnter position of array you want to modify: ");
scanf("%d", &pin);
printf("\n\nEnter new value: ");
scanf("%d", &mod_ele);
arr_mod[pin - 1] = mod_ele;
display(arr_mod);
flow();
}
void bubble_sort_asc(int arr_bubb_asc[]){
int temp;
int i;
for (i= 0; i < arr_size; i++){
int flag = 0;
int j;
for (j= 0; j < arr_size - 1 - i; j++){
if (arr_bubb_asc[j] > arr_bubb_asc[j + 1]){
flag = 1;
temp = arr_bubb_asc[j];
arr_bubb_asc[j] = arr_bubb_asc[j + 1];
arr_bubb_asc[j + 1] = temp;
// count++;
}
}
if (flag == 0)
break;
}
}
void inser_sort_asc(int arr_ins_asc[]){
int temp;
int i,j;
for (i= 1; i < arr_size; i++){
for (j= i; j > 0; j--){
if (arr_ins_asc[j] < arr_ins_asc[j - 1]){
temp = arr_ins_asc[j];
arr_ins_asc[j] = arr_ins_asc[j - 1]; // 0 1 2 3 4 5
arr_ins_asc[j - 1] = temp; //1 4 5 10 6 2
}
else {
break;
}
}
}
}
void selc_sort_asc(int arr_selc_asc[]){
int min, temp;
int i;
for (i= 0; i < arr_size - 1; i++){
min = i;
int j;
for (j = i + 1; j < arr_size; j++){ // 0 1 2 3 4
if (arr_selc_asc[j] < arr_selc_asc[min]){
min = j;
}
}
temp = arr_selc_asc[min];
arr_selc_asc[min] = arr_selc_asc[i];
arr_selc_asc[i] = temp;
}
}
void linear_srch(int arr_lnr[], int key){
int i;
for ( i = 0; i < arr_size; i++){
if (key == arr_lnr[i])
break;
}
if (i != arr_size)
printf("\nYour KEY found at index of number: %d\n", i);
else
printf("\nSearch was unsuccesful, you KEY hasn't found.\n");
flow();
}
int bin_srch(int arr_bin[], int key, int beg, int end){
int mid;
while (beg <= end){
mid = (beg + end) / 2;
if (arr_bin[mid] == key)
return mid;
else if (key < arr_bin[mid])
end = mid - 1;
else
beg = mid + 1;
}
return -1;
}
int main(){
int arr_input[250], option;
printf("\n\nName: Shripad Kanakdande\nRoll No. 22234\nTitle: Array Operations\n");
printf("\n==================================================================");
printf("\n\nEnter the size of Array: ");
scanf("%d", &arr_size);
printf("\nEnter the elements of array on which you want to perform certain Operation: ");
int i;
for (i= 0; i < arr_size; i++){
scanf("%d", &arr_input[i]);
}
printf("\nWhich operation do you want to perform\n1. Display\n2. Insert\n3. Delete\n4. Modify\n5. Sort\n6. Search\n");
printf("\nEnter your option here: ");
scanf("%d", &option);
switch(option){
case 1: {
display(arr_input);
flow();
break;
}
case 2: {
insert(arr_input);
break;
}
case 3: {
delete(arr_input);
break;
}
case 4: {
modify(arr_input);
break;
}
case 5: {
int sort_opt, sort_type;
printf("\nIn which type you want to arrange elements of Array:\n1. Ascending\n2. Descending\n");
printf("\nEnter your choice here: ");
fflush(stdin);
scanf("%d", &sort_type);
switch(sort_type){
case 1: {
printf("\nWhich sorting technique you want to perform: \n1. Bubble Sort\n2. Insertion Sort\n3. Selection Sort\n");
printf("\nEnter your choice here: ");
scanf("%d", &sort_opt);
switch(sort_opt){
case 1: {
bubble_sort_asc(arr_input);
display(arr_input);
flow();
break;
}
case 2: {
inser_sort_asc(arr_input);
display(arr_input);
flow();
break;
}
case 3: {
selc_sort_asc(arr_input);
display(arr_input);
flow();
break;
}
}
break;
}
case 2: {
printf("\nWhich sorting technique you want to perform: \n1. Bubble Sort\n2. Insertion Sort\n3. Selection Sort\n");
printf("\nEnter your choice here: ");
scanf("%d", &sort_opt);
switch(sort_opt){
case 1: {
bubble_sort_asc(arr_input);
display_rev(arr_input);
flow();
break;
}
case 2: {
inser_sort_asc(arr_input);
display_rev(arr_input);
flow();
break;
}
case 3: {
selc_sort_asc(arr_input);
display_rev(arr_input);
flow();
break;
}
}
break;
}
}
break;
}
case 6: {
int srch_type, key, count;
printf("\nEnter KEY you want to find: ");
scanf("%d", &key);
printf("\nWhich type of search do you want to perform:\n1. Linear Search\n2. Binary Search\n");
printf("\nEnter your option here: ");
scanf("%d", &srch_type);
switch(srch_type){
case 1: {
linear_srch(arr_input, key);
break;
}
case 2: {
bubble_sort_asc(arr_input);
int result = bin_srch(arr_input, key, 0, arr_size - 1);
if (result == -1)
printf("\nSearch was unsuccesful, you KEY hasn't found.\n");
else
printf("\nYour KEY found at index of number: %d\n", result);
flow();
break;
}
}
}
}
}