-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create Linear_Search.c * Update README.md * Create Binary_Search.c * Update README.md * Update README.md
- Loading branch information
1 parent
dfbd035
commit 50606ca
Showing
4 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* @author : ashwek | ||
* @date : 29/01/2019 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int linear_search(int *arr, int n, int search){ | ||
|
||
int i; | ||
|
||
for(i=0; i<n; i++){ | ||
if( arr[i] == search ) | ||
return i; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
void main(){ | ||
|
||
int *arr, n, search, i; | ||
|
||
printf("Enter size of array = "); | ||
scanf("%d", &n); | ||
|
||
arr = (int *)malloc(sizeof(int)*n); | ||
|
||
printf("Enter %d elements : ", n); | ||
for(i=0; i<n; i++){ | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
printf("Enter a value to search = "); | ||
scanf("%d", &search); | ||
|
||
i = linear_search(arr, n, search); | ||
|
||
if( i == -1 ){ | ||
printf("%d not found in array", search); | ||
} | ||
else{ | ||
printf("%d found at %d index", search, i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* @author : ashwek | ||
* @date : 29/01/2019 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int binary_search(int *arr, int n, int search){ | ||
|
||
int low = 0, high = n-1; | ||
int mid; | ||
|
||
while( low <= high ){ | ||
|
||
mid = (high - low) / 2 + low; | ||
|
||
if( arr[mid] == search ){ | ||
return mid; | ||
} | ||
else if( search < arr[mid] ){ | ||
high--; | ||
} | ||
else{ | ||
low++; | ||
} | ||
|
||
} | ||
|
||
return -1; | ||
} | ||
|
||
void main(){ | ||
|
||
int *arr, n, search, i; | ||
|
||
printf("Enter size of array = "); | ||
scanf("%d", &n); | ||
|
||
arr = (int *)malloc(sizeof(int)*n); | ||
|
||
printf("Enter %d elements (in sorted order) : ", n); | ||
for(i=0; i<n; i++){ | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
printf("Enter a value to search = "); | ||
scanf("%d", &search); | ||
|
||
i = binary_search(arr, n, search); | ||
|
||
if( i == -1 ){ | ||
printf("%d not found in array", search); | ||
} | ||
else{ | ||
printf("%d found at %d index", search, i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters