-
Notifications
You must be signed in to change notification settings - Fork 10
/
Binary_Search.c
90 lines (69 loc) · 2.3 KB
/
Binary_Search.c
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
#include<stdio.h>
#include<stdlib.h>
void binarySearch(int searchValue, int a[], int n);
void selectionSort(int a[], int n)
{
int i, j, minimunIndex, temp;
printf("Just started to sort Using the Selection Sort Algorithm\n");
for ( i = 0; i < n; i++)
{
minimunIndex = i;
for ( j = i + 1; j < n; j++)
{
if (a[minimunIndex] > a[j])
minimunIndex = j;
}
temp = a[i];
a[i] = a[minimunIndex];
a[minimunIndex] = temp;
}
printf("Ended Sorting using Selection Sort\n");
}
void main()
{
int length, i, searchValue;
printf("Welcome to the Binary Search!!\n");
printf("You can search the data element from an array, don't worry I will do it for you\n");
printf("For simplicity here the data elements are considered as integers\n");
printf("So let me know how many integers you want:\n");
scanf("%d", &length);
int *a = (int *) calloc (length, sizeof(int));
printf("Please enter the integers now:\n");
for ( i = 0; i < length; i++)
{
scanf("%d", &a[i]);
}
printf("Please enter the value you want me to search for:\n");
scanf("%d", &searchValue);
// Binary Search needs the sorted array as input without which the search can't occur
// Hence sorting elements before the Search
selectionSort(a, length);
binarySearch(searchValue, a, length);
printf("Thanks for Investing time in Me!!");
}
void binarySearch(int searchValue, int a[], int n)
{
int middle, first, last;
first = 0;
last = n-1;
middle = first + last / 2;
printf("Starting to search the Data element --> %4d\n", searchValue);
while( first <= last)
{
if(a[middle] < searchValue)
first = middle + 1;
else if(a[middle] == searchValue)
{
printf("The element %d was found at the location %d starting from 0\n", searchValue, middle);
break;
}
else
last = middle - 1;
middle = first + last / 2;
}
if (first > last)
{
printf("Sorry the element you wanted me to Search doesn't exist in the given array\n");
}
printf("Just Ended Binary Search\n");
}