-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_and_binary_search_on_dynamic_1d_array.c
More file actions
158 lines (130 loc) · 4 KB
/
Copy pathlinear_and_binary_search_on_dynamic_1d_array.c
File metadata and controls
158 lines (130 loc) · 4 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
#include <stdio.h>
#include <stdlib.h>
//function-definitions;
int menu();
int* create(int *size);
int linearSearch(const int array[], int size, int element);
void bubbleSort(int array[], int size);
int binarySearch(const int array[], int size, int element);
int main() {
int choice;
int *array;
int size = 0;
int element;
int result;
do {
choice = menu();
switch (choice) {
case 1:
array = create(&size);
if (array==NULL){
printf(":( Sorry, There was an unknown error\n");
printf("encountered while creating the array.\n");
}
else{
printf(":) Array of size '%d' is Successfully Created.\n\n", size);
}
break;
case 2: // searching using Linear Search algorithm;
if (size == 0){
printf(":( OOPS seems like the array was not created.\n\n");
break;
}
printf("\n");
printf("Enter Element to Search: ");
scanf("%d", &element);
result = linearSearch(array, size, element);
if (result == -1) {
printf("\n:( Element NOT found.\n\n");
} else {
printf("\n:) Element found at index %d.\n\n", result);
}
break;
case 3: // searching using Binary Search algorithm;
if (size == 0){
printf(":( OOPS seems like the array was not created.\n\n");
break;
}
//sorting the array using bubble sort method;
printf("\nSorting the array...\n");
bubbleSort(array, size);
printf("Array sorted successfully.\n");
//searching the element using binary search algorithm;
printf("\n");
printf("Enter Element to Search: ");
scanf("%d", &element);
result = binarySearch(array, size, element);
if (result == -1) {
printf("\n:( Element NOT found.\n\n");
} else {
printf("\n:) Element found at index %d.\n\n", result);
}
break;
case 4:
break;
default:
printf(":( Enter valid operation number.\n\n");
}
}
while (choice != 4);
return 0;
}
int menu(){
int choice;
printf("Select a choice\n");
printf("1. Create Array\n");
printf("2. Linear Search\n");
printf("3. Binary Search\n");
printf("4. Exit\n");
printf("\nEnter Choice: ");
scanf("%d", &choice);
return choice;
}
int* create(int *size){
printf("Enter the size of the array: ");
scanf("%d", size);
//dynamically creating an array of size of user input;
int* array = (int *) malloc(sizeof(int) * (*size));
for(int i=0 ; i< *size ; i++){
printf("Enter Array[%d]: ", i);
scanf("%d", &array[i]);
}
return array;
}
int linearSearch(const int array[], int size, int element){
for(int i=0 ; i<size ; i++){
if(array[i]==element){
return i;
}
}
return -1;
}
void bubbleSort(int array[], int size){
for(int i=0 ; i<size-1 ; i++){
for(int j=0 ; j<size-i-1 ; j++){
if (array[j] > array[j+1]){
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
}
int binarySearch(const int array[], int size, int element){
int start = 0;
int end = size;
while(start <= end){
int mid = (start + end)/2;
if (array[mid] == element){
return mid;
}
else if(element < array[mid]){
end = mid-1;
}
else{
start = mid+1;
}
}
return -1;
}
//By - Shubham 219301072 B.Tech Sec-3C (CSE);