Skip to content

Commit ddf3334

Browse files
authored
Merge pull request codeIIEST#27 from abiduzz420/master
implemented DMA for arrays in searching algorithms
2 parents c59c98c + 982a075 commit ddf3334

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

search/Binary_Search.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
#include<stdio.h>
2+
#include<stdlib.h>
3+
24
int main() {
35

4-
int n, a[30], item, i, j, mid, top, bottom;
6+
int n_terms, *arr, item, i, j, mid, top, bottom;
57

68

79
printf("Enter how many elements you want:\n"); // no of elements
8-
scanf("%d", &n);
9-
printf("Enter the %d elements in ascending order\n", n);
10-
for (i = 0; i < n; i++) {
11-
scanf("%d", &a[i]);
10+
scanf("%d", &n_terms);
11+
arr = (int*)malloc(sizeof(int)*n_terms);
12+
printf("Enter the %d elements in ascending order\n", n_terms);
13+
for (i = 0; i < n_terms; i++) {
14+
scanf("%d", &arr[i]);
1215
}
1316

1417
printf("\nEnter the item to search\n"); // Target element to be searched
1518
scanf("%d", &item);
1619
bottom = 1;
17-
top = n;
20+
top = n_terms;
1821

1922
do {
2023
mid = (bottom + top) / 2;
21-
if (item < a[mid])
24+
if (item < arr[mid])
2225
top = mid - 1; // Here we are dividing the array into two equal parts
23-
else if (item > a[mid]) /* if target element > mid part of array , we do the search in the upper part of the array
26+
else if (item > arr[mid]) /* if target element > mid part of array , we do the search in the upper part of the array
2427
else search in the lower part */
2528
bottom = mid + 1;
26-
} while (item != a[mid] && bottom <= top);
29+
} while (item != arr[mid] && bottom <= top);
2730

28-
if (item == a[mid]) {
31+
if (item == arr[mid]) {
2932
printf("Binary search successfull!!\n");
30-
printf("\n %d found in position: %d\n", item, mid + 1);
33+
printf("%d found in position: %d\n", item, mid + 1);
3134
} else {
32-
printf("\n Search failed\n %d not found\n", item);
35+
printf("Search failed\n%d not found\n", item);
3336
}
3437
return 0;
3538
}

search/linear_search.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
#include <stdio.h>
2-
2+
#include <stdlib.h>
33
int main()
44
{
5-
int array[100], search, c, n;
5+
int *array, search, c, n_terms;
66

77
printf("Enter the number of elements in array\n");
8-
scanf("%d",&n); // Total no of elements
8+
scanf("%d",&n_terms); // Total no of elements
99

10-
printf("Enter %d integer(s)\n", n);
10+
array = (int*)malloc(sizeof(int)*n_terms);
11+
printf("Enter %d integer(s)\n", n_terms);
1112

12-
for (c = 0; c < n; c++)
13+
for (c = 0; c < n_terms; c++)
1314
scanf("%d", &array[c]); // Reading the elements
1415

1516
printf("Enter the number to search\n"); // Target element to be searched
1617
scanf("%d", &search);
1718

18-
for (c = 0; c < n; c++)
19+
for (c = 0; c < n_terms; c++)
1920
{
2021
if (array[c] == search) /* if required element found */
2122
{
2223
printf("%d is present at location %d.\n", search, c+1);
2324
break;
2425
}
2526
}
26-
if (c == n)
27+
if (c == n_terms)
2728
printf("%d is not present in array.\n", search); // Element not found
2829

2930
return 0;

0 commit comments

Comments
 (0)