Skip to content

Commit e7b2f1e

Browse files
committed
feat:<Binary search> binary search content add
1 parent cea5875 commit e7b2f1e

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
<h1 class="text-center">Coming soon</h1>
1+
<h1 class="text-center">Binary Search Algorithm</h1>
2+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.defination"></div>
3+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.explainImage"></div>
4+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.properties"></div>
5+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.flowChart"></div>
6+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.workingProcedure"></div>
7+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.applications"></div>
8+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.other"></div>
9+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.advantage"></div>
10+
<div class="mt-md-4 mt-3" [innerHtml]="binarySearchMetaData.disadvantage"></div>

src/app/components/Algorithms/searching/binary-search/binary-search.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { binarySearchMetaData } from 'src/app/core/algorithms/searching-meta-data';
23

34
@Component({
45
selector: 'app-binary-search',
56
templateUrl: './binary-search.component.html',
67
styleUrls: ['./binary-search.component.css']
78
})
89
export class BinarySearchComponent implements OnInit {
9-
10+
binarySearchMetaData = binarySearchMetaData;
1011
constructor() { }
1112

1213
ngOnInit(): void {

src/app/core/algorithms/searching-meta-data.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,68 @@ export const linearSearchMetaData: Algorithms = {
8686
}
8787

8888
// ----------------------------------- End ----------------------------------------------//
89+
90+
// ---------------------------------- Binary Search ---------------------------------------//
91+
92+
const binarySearchData = {
93+
defination:`Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half.
94+
The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n)`,
95+
flowChart: 'assets/ds-image/DSA-404.webp',
96+
explainImage: 'assets/ds-image/DSA-404.webp',
97+
properties: [
98+
`The pre-condition for the binary search is that the elements must be arranged in a sorted order`,
99+
`The implementation of binary search is limited as it can be implemented only on those data structures
100+
that have two-way traversal`,
101+
`It is based on the divide and conquer approach`,
102+
`It is preferrable for the large-size data sets`,
103+
`It can be implemented only on a singledimensional array`
104+
],
105+
workingProcedure:[
106+
`Compare 'x' with the middle element`,
107+
`If 'x' matches with the middle element, we return the mid index`,
108+
`Else If 'x' is greater than the mid element, then 'x' can only lie in the right half subarray after the mid element.
109+
So we recur for the right half`,
110+
`Else ('x' is smaller) recur for the left half`
111+
],
112+
advantage: [
113+
`It is better than a linear search algorithm since its run time complexity is O(Log n)`,
114+
`At each iteration, the binary search algorithm eliminates half of the list and significantly reduces the search space`,
115+
`The binary search algorithm works even when the array is rotated by some position and finds the target element`
116+
],
117+
disadvantage: [
118+
`The recursive method uses stack space`,
119+
`Programming binary search algorithm is error prone and difficult`,
120+
`The interaction of binary search with memory hierarchy i.e. caching is poor.
121+
(because of its random access nature)`
122+
],
123+
applications: [
124+
`Find an element in a sorted array`,
125+
`Find the first value greater than or equal to x in a given array of sorted integers`,
126+
`Find the frequency of a given target value in an array of integers`,
127+
`Dictionary: In the dictionary, all the words are arranged in lexicographical order, therefore,
128+
to find a particular word, we can simply binary search to find the alphabets without having to go through each word`,
129+
`Find if a number is a square of any integer: To check if a number is a square of any integer, run a binary search from 1
130+
to num and check if the square of mid is equal to num.`
131+
],
132+
performance: [
133+
`Best Case Time Complexity of Binary Search: O(1)`,
134+
`Average Case Time Complexity of Binary Search: O(logN)`,
135+
`Worst Case Time Complexity of Binary Search: O(logN)`,
136+
`Space Complexity of Binary Search: O(1) for iterative, O(logN) for recursive`
137+
],
138+
139+
};
140+
141+
export const binarySearchMetaData : Algorithms = {
142+
defination: Helper.setHeader(binarySearchData.defination),
143+
properties: Helper.setListwithTitleHtml('Properties of Binary Search', binarySearchData.properties),
144+
explainImage: Helper.setExampleImage('Binary Search Image', binarySearchData.explainImage),
145+
flowChart: Helper.setExampleImage('Flowchart of Binary Search', binarySearchData.flowChart),
146+
advantage: Helper.setListwithTitleHtml('Advantages of Binary Search', binarySearchData.advantage),
147+
disadvantage: Helper.setListwithTitleHtml('Disadvantages of Binary Search', binarySearchData.disadvantage),
148+
applications: Helper.setListwithTitleHtml('Applications of Binary Search', binarySearchData.applications),
149+
other: Helper.setListwithTitleHtml('Performance of Binary Search', binarySearchData.performance),
150+
workingProcedure: Helper.setListwithTitleHtml('Follow the below steps to solve the problem', binarySearchData.workingProcedure),
151+
}
152+
153+
// ----------------------------------- End ----------------------------------------------//

0 commit comments

Comments
 (0)