Skip to content

Commit 928db71

Browse files
author
Bhrigu Kansra
authored
Merge pull request bhrigukansra#130 from sankamohotttala/master
search algorithms
2 parents de4b9de + af59539 commit 928db71

File tree

3 files changed

+10070
-1
lines changed

3 files changed

+10070
-1
lines changed

contributors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
35. [Shrey Aryan](https://github.com/shrey183)
3838
36 [Kautuk Kundan](https://github.com/kautukkundan)
3939
37. [Deepanshu Jain](https://github.com/thedeepanshujain)
40-
38. [Your Name](https://github.com/yourusername)
40+
38. [Sanka Mohottala] (https://github.com/sankamohottala)

searching/C/Final_binary_search.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <windows.h> //this library for Sleep()
5+
#include <time.h>
6+
#include "sorted_list.h"
7+
8+
9+
int UpB=9999;
10+
int LoB=0;
11+
int midP;
12+
13+
14+
bool search(int value){
15+
while(1){ //infinite loop
16+
midP=(UpB+LoB)/2; //mathematical equation to find midP index
17+
// accurate upto 2^30 size integer arrays
18+
Sleep(100); //100ms sleep is used to get an accurate measurement of time complexity
19+
20+
if(value==sorted_list[midP]){
21+
return true;
22+
break;
23+
24+
}
25+
else{
26+
if(UpB==LoB){ //this is the mathematical property used to get out of loop if value not in array
27+
return false;
28+
break;
29+
}
30+
31+
else{
32+
if(value>midP){
33+
LoB=midP+1;
34+
35+
}
36+
else{
37+
UpB=midP-1;
38+
}
39+
}
40+
}
41+
}
42+
}
43+
float code_time(clock_t a,clock_t b){ //this function is used to measure time for one search
44+
float duration = (float)(a - b) / CLOCKS_PER_SEC;
45+
46+
47+
return duration;
48+
}
49+
int main()
50+
{
51+
int i;
52+
clock_t start, end;
53+
54+
printf("Enter the value to search: ");
55+
scanf("%d",&i);
56+
start = clock();
57+
if(search(i)==true){
58+
printf("Number is found :Index is %d\n",midP);
59+
}
60+
else
61+
printf("Number is not in the array\n");
62+
63+
end = clock();
64+
printf("Total time taken to do the search: %f\n",code_time(end,start));// code_time is to calculate time duration
65+
printf("Exiting of the program...\n");
66+
return 0;
67+
}

0 commit comments

Comments
 (0)