Skip to content

implemented DMA for arrays in searching algorithms #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions search/Binary_Search.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
#include<stdio.h>
#include<stdlib.h>

int main() {

int n, a[30], item, i, j, mid, top, bottom;
int n_terms, *arr, item, i, j, mid, top, bottom;


printf("Enter how many elements you want:\n"); // no of elements
scanf("%d", &n);
printf("Enter the %d elements in ascending order\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
scanf("%d", &n_terms);
arr = (int*)malloc(sizeof(int)*n_terms);
printf("Enter the %d elements in ascending order\n", n_terms);
for (i = 0; i < n_terms; i++) {
scanf("%d", &arr[i]);
}

printf("\nEnter the item to search\n"); // Target element to be searched
scanf("%d", &item);
bottom = 1;
top = n;
top = n_terms;

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

if (item == a[mid]) {
if (item == arr[mid]) {
printf("Binary search successfull!!\n");
printf("\n %d found in position: %d\n", item, mid + 1);
printf("%d found in position: %d\n", item, mid + 1);
} else {
printf("\n Search failed\n %d not found\n", item);
printf("Search failed\n%d not found\n", item);
}
return 0;
}
15 changes: 8 additions & 7 deletions search/linear_search.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[100], search, c, n;
int *array, search, c, n_terms;

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

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

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

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

for (c = 0; c < n; c++)
for (c = 0; c < n_terms; c++)
{
if (array[c] == search) /* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
if (c == n_terms)
printf("%d is not present in array.\n", search); // Element not found

return 0;
Expand Down