forked from harsh2102/C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_of_strings.c
74 lines (69 loc) · 1.69 KB
/
binary_search_of_strings.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
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define LENGTH 50
void descending(char [ROWS][LENGTH]);
void ascending(char [ROWS][LENGTH]);
void display(void);
int main(){
int start, end, mid, t1, t2;
char s[ROWS][LENGTH];
char s2[ROWS][LENGTH];
char search_name[LENGTH];
char found = 'F';
int i = 0;
start = 0;
end = ROWS;
do{
printf("Enter name: ");
scanf("%s", s[i]);
strcpy(s2[i], s[i]);
if (s[i][0] >= 65 && s[i][0] <= 90){
s[i][0] = (int)s[i][0] + 32;
}
++i;
}while(i < ROWS);
char temp[50];
char swap = 'Y';
while (swap == 'Y'){
swap = 'N';
for (int i = 0; i < (ROWS - 1); ++i){
if (strcmp(s[i], s[i+1]) > 0){
swap = 'Y';
strcpy(temp, s[i]);
strcpy(s[i], s[i+1]);
strcpy(s[i+1], temp);
}
}
}
printf("\n");
for(int i = 0; i < ROWS; ++i){
printf("%s\n", s[i]);
}
printf("\nEnter name to search: ");
scanf("%s", search_name);
t1 = clock();
while(start <= end && found == 'F'){
mid = (start + end) / 2;
if (strcmp(search_name, s[mid]) == 0){
found = 'T';
}
else{
if (strcmp(search_name, s[mid]) > 0){
start = mid + 1;
}
else {
end = mid - 1;
}
}
if (found == 'T'){
printf("Found name at: %d\n", mid);
}
}
t2 = clock();
double time_taken = (double)(t2 - t1) / CLOCKS_PER_SEC;
printf("\n\nTime taken: %f", time_taken);
}