Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c4abf18

Browse files
authoredFeb 5, 2021
Create Binary Search Along with Time complexity
A program that implements binary search algorithm to find a use entered name in an array of 31 elements at least. You need to populate the array with names at the start of the program and then sort it before implementing binary search on it. Print the time to perform the whole operation of binary search at the end.  If the name exits o Print index values  If the name does not exit
1 parent b3ca381 commit c4abf18

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
 
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
2+
//Humza Khawar
3+
4+
//binary search
5+
6+
7+
#include<stdio.h>
8+
#include<ctype.h>
9+
#include<time.h>
10+
#include <string.h>
11+
12+
void ascending_sort();
13+
int binarySearch(int, int, char*);
14+
char Name_Array[50][20] = { 0 };
15+
16+
void main() {
17+
18+
int n = sizeof(Name_Array) / sizeof(Name_Array[0]);
19+
int choice = 0;
20+
char x[sizeof(Name_Array[0])];
21+
22+
for (int i = 0; i < 50; i++)
23+
{
24+
printf("\nEnter Name %d : ", i + 1);
25+
scanf("%s", Name_Array[i]);
26+
27+
28+
}
29+
30+
31+
for (int i = 0; i < 50; i++)
32+
{
33+
for (int j = 0; j < 20; j++)
34+
{
35+
36+
Name_Array[i][j] = toupper(Name_Array[i][j]);
37+
38+
39+
}
40+
41+
}
42+
43+
44+
ascending_sort();
45+
46+
printf("\nEnter Name to search: ");
47+
scanf("%s", x);
48+
for (int j = 0; j < sizeof(Name_Array[0]); j++)
49+
{
50+
51+
x[j] = toupper(x[j]);
52+
53+
54+
}
55+
56+
double begin = clock();
57+
int result = binarySearch(0, n - 1, x);
58+
double end = clock();
59+
60+
printf("\nName is at %d index.", result);
61+
62+
printf("\nTime measured: %f seconds.\n", (double)(end - begin)/ CLOCKS_PER_SEC);
63+
}
64+
65+
66+
void ascending_sort() {
67+
68+
for (int j = 0; j < 49; j++) {
69+
70+
for (int i = 0; i <49; i++)
71+
{
72+
if (strcmp(Name_Array[i], Name_Array[i + 1]) < 0)
73+
{
74+
75+
continue;
76+
77+
}
78+
else if (strcmp(Name_Array[i], Name_Array[i + 1]) > 0)
79+
{
80+
char temp_name[20];
81+
82+
83+
84+
85+
strcpy(temp_name, Name_Array[i]);
86+
87+
strcpy(Name_Array[i], Name_Array[i + 1]);
88+
89+
strcpy(Name_Array[i + 1], temp_name);
90+
}
91+
}
92+
}
93+
94+
}
95+
int binarySearch(int l, int r, char x[sizeof(Name_Array[0])])
96+
{
97+
if (r >= l) {
98+
int mid = l + (r - l) / 2;
99+
100+
// If the element is present at the middle
101+
// itself
102+
if ((strcmp(Name_Array[mid], x) == 0)) {
103+
return mid;
104+
}
105+
106+
107+
// If element is smaller than mid, then
108+
// it can only be present in left subarray
109+
110+
if ((strcmp(Name_Array[mid], x) > 0)) {
111+
112+
return binarySearch(l, mid - 1, x);
113+
114+
115+
}
116+
117+
// Else the element can only be present
118+
// in right subarray
119+
return binarySearch(mid + 1, r, x);
120+
}
121+
122+
// We reach here when element is not
123+
// present in array
124+
return -1;
125+
}

0 commit comments

Comments
 (0)
Please sign in to comment.