Skip to content

Commit addf4d9

Browse files
committed
searching
1 parent cb266c2 commit addf4d9

File tree

11 files changed

+190
-0
lines changed

11 files changed

+190
-0
lines changed
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="binarySearch" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="Debug">
10+
<Option output="bin/Debug/binarySearch" prefix_auto="1" extension_auto="1" />
11+
<Option object_output="obj/Debug/" />
12+
<Option type="1" />
13+
<Option compiler="gcc" />
14+
<Compiler>
15+
<Add option="-g" />
16+
</Compiler>
17+
</Target>
18+
<Target title="Release">
19+
<Option output="bin/Release/binarySearch" prefix_auto="1" extension_auto="1" />
20+
<Option object_output="obj/Release/" />
21+
<Option type="1" />
22+
<Option compiler="gcc" />
23+
<Compiler>
24+
<Add option="-O2" />
25+
</Compiler>
26+
<Linker>
27+
<Add option="-s" />
28+
</Linker>
29+
</Target>
30+
</Build>
31+
<Compiler>
32+
<Add option="-Wall" />
33+
<Add option="-fexceptions" />
34+
</Compiler>
35+
<Unit filename="main.cpp" />
36+
<Extensions>
37+
<code_completion />
38+
<envvars />
39+
<debugger />
40+
<lib_finder disable_auto="1" />
41+
</Extensions>
42+
</Project>
43+
</CodeBlocks_project_file>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_layout_file>
3+
<FileVersion major="1" minor="0" />
4+
<ActiveTarget name="Debug" />
5+
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6+
<Cursor>
7+
<Cursor1 position="401" topLine="2" />
8+
</Cursor>
9+
</File>
10+
</CodeBlocks_layout_file>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*Algorithm for binary search
2+
Time Complexity = O(log n) if the array is already sorted
3+
otherwise, O(n log n) if the array is not sorted then apply merge sort then binary sort
4+
5+
space Complexity = O(1)
6+
*/
7+
8+
#include<iostream>
9+
using namespace std;
10+
11+
int binarySearch(int arr[], int n, int key) {
12+
// since the array is sorted we will directly jump to binary search
13+
int low = 0;
14+
int high = n - 1;
15+
16+
while(low <= high) {
17+
int mid = (low + high)/2;
18+
19+
if(arr[mid] == key) {
20+
return mid;
21+
}
22+
else if(arr[mid] < key) {
23+
low = mid + 1;
24+
}
25+
else {
26+
high = mid - 1;
27+
}
28+
}
29+
}
30+
31+
int main() {
32+
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
33+
int n = sizeof(arr)/sizeof(arr[0]);
34+
35+
int key = 7;
36+
int index = binarySearch(arr, n, key);
37+
38+
if(index >= 0) {
39+
cout<<"Element "<<key<<" found at index "<<index<<endl;
40+
} else {
41+
cout<<"Element not "<<key<<"found"<<endl;
42+
}
43+
44+
return 0;
45+
}
Binary file not shown.
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_project_file>
3+
<FileVersion major="1" minor="6" />
4+
<Project>
5+
<Option title="linearSearch" />
6+
<Option pch_mode="2" />
7+
<Option compiler="gcc" />
8+
<Build>
9+
<Target title="Debug">
10+
<Option output="bin/Debug/linearSearch" prefix_auto="1" extension_auto="1" />
11+
<Option object_output="obj/Debug/" />
12+
<Option type="1" />
13+
<Option compiler="gcc" />
14+
<Compiler>
15+
<Add option="-g" />
16+
</Compiler>
17+
</Target>
18+
<Target title="Release">
19+
<Option output="bin/Release/linearSearch" prefix_auto="1" extension_auto="1" />
20+
<Option object_output="obj/Release/" />
21+
<Option type="1" />
22+
<Option compiler="gcc" />
23+
<Compiler>
24+
<Add option="-O2" />
25+
</Compiler>
26+
<Linker>
27+
<Add option="-s" />
28+
</Linker>
29+
</Target>
30+
</Build>
31+
<Compiler>
32+
<Add option="-Wall" />
33+
<Add option="-fexceptions" />
34+
</Compiler>
35+
<Unit filename="main.cpp" />
36+
<Extensions>
37+
<code_completion />
38+
<envvars />
39+
<debugger />
40+
</Extensions>
41+
</Project>
42+
</CodeBlocks_project_file>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_layout_file>
3+
<FileVersion major="1" minor="0" />
4+
<ActiveTarget name="Debug" />
5+
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6+
<Cursor>
7+
<Cursor1 position="568" topLine="3" />
8+
</Cursor>
9+
</File>
10+
</CodeBlocks_layout_file>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*This algorithm is based on most basic searching called Linear Search
2+
Time Complexity = O(n) worst case
3+
space Complexity = O(1)
4+
*/
5+
#include <iostream>
6+
using namespace std;
7+
8+
int linearSearch(int arr[], int n, int key) {
9+
for(int i = 0; i < n; i++) {
10+
if(arr[i] == key) {
11+
return i;
12+
}
13+
}
14+
return -1;
15+
}
16+
17+
int main() {
18+
int arr[] = {1, 2, 3, 4, 5, 5, 5, 6, 4};
19+
int n = sizeof(arr)/sizeof(arr[0]);
20+
21+
int keyToSearch = 10;
22+
int foundIndex = linearSearch(arr, n, keyToSearch);
23+
24+
if(foundIndex >= 0) {
25+
cout<<"Element "<<keyToSearch<<" is at index "<<foundIndex<<endl;
26+
} else {
27+
cout<<"Element "<<keyToSearch<<" not found"<<endl;
28+
}
29+
return 0;
30+
}
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_layout_file>
3+
<FileVersion major="1" minor="0" />
4+
<ActiveTarget name="Debug" />
5+
<File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
6+
<Cursor>
7+
<Cursor1 position="477" topLine="0" />
8+
</Cursor>
9+
</File>
10+
</CodeBlocks_layout_file>

0 commit comments

Comments
 (0)