Skip to content

Commit c488a61

Browse files
committed
adding Searching Algorithms readme
1 parent e159e7f commit c488a61

File tree

3 files changed

+74
-15
lines changed

3 files changed

+74
-15
lines changed

13-chapter-Searching-Algorithms.js renamed to 13-chapter-Searching-Algorithms/13-chapter-Searching-Algorithms.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const BasicSorting = require('../12-chapter-Sorting-Algorithms/12-chapter-basic-sorting-algorithms');
2+
13
class Search {
24

35
static sequentialSearch(dataStore, data) {
@@ -78,19 +80,6 @@ function setData(numElements = 100) {
7880
return dataStore;
7981
}
8082

81-
function insertionSort(dataStore) {
82-
let temp, inner;
83-
for (let outer = 1; outer <= dataStore.length-1; outer++) {
84-
temp = dataStore[outer];
85-
inner = outer;
86-
while (inner > 0 && (dataStore[inner-1] >= temp)) {
87-
dataStore[inner] = dataStore[inner-1];
88-
--inner;
89-
}
90-
dataStore[inner] = temp;
91-
}
92-
}
93-
9483
// Implementation
9584
// ##########################################################################
9685
const fs = require('fs');
@@ -105,7 +94,7 @@ function readMyData(file) {
10594
console.log(`Sequential search took ${end - start} milliseconds.`);
10695
console.log();
10796

108-
insertionSort(data);
97+
BasicSorting.insertionSort(data);
10998
start = new Date().getTime();
11099
position = Search.binarySearch(data, word);
111100
end = new Date().getTime();
@@ -121,7 +110,7 @@ console.log();
121110
console.log(`The minimum value is: ${Search.minValue(dataStore)}`);
122111
console.log(`The maximum value is: ${Search.maxValue(dataStore)}`);
123112
console.log();
124-
insertionSort(dataStore);
113+
BasicSorting.insertionSort(dataStore);
125114
const resBin = Search.binarySearch(dataStore, number);
126115
const count = Search.countOccurrences(dataStore, number);
127116
const present = `${number} is present in the set and is in the position ${resBin} and has ${count} occurrences`;
File renamed without changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Search Algorithms
2+
3+
There are two ways to search for data in a list: sequential search and binary search. A sequential search is used when the items in a list are in random order; a binary search is used when the items in a list are in sorted order. Binary search is the more efficient algorithm, but you also have to take into account the extra time it takes to sort the data set before being able to search it for a value.
4+
5+
JavaScript has built-in functions for searching values in an array, that are the following:
6+
7+
- indexOf() returns the index of the value to find if the value exists otherwise returns -1.
8+
- find() returns true if the value exists otherwise returns false.
9+
- findIndex() return the same as indexOf.
10+
11+
find() and findIndex() are ES6 features and needs extra parameters to perform the search.
12+
13+
**Sequential Search**
14+
15+
The most obvious way to search for data in a list is to begin at the first element and move to each element in the list until you either find the data you are looking for or you reach the end of the list. This is called a sequential search, sometimes also called a linear search. It is an example of a brute-force search technique, where potentially every element in the data structure is visited on the way to a solution.
16+
17+
**Binary Search** (in linear data structures)
18+
19+
Binary search is used to perform a very efficient search on sorted dataset. The time complexity is O(log2N). Idea is to repeatedly divide in half the portion of the list that could contain the item, until we narrow it down to one possible item.
20+
21+
![](https://blog.penjee.com/wp-content/uploads/2015/04/binary-and-linear-search-animations.gif)
22+
23+
Some applications are:
24+
25+
- When you search for a name of song in a sorted list of songs, it performs binary search and string-matching to quickly return the results.
26+
- Used to debug in git through git bisect
27+
28+
**Searching Textual Data** (in JavaScript)
29+
30+
breaking up the text into words using the split() function, which uses the space between each word as the delimiter. This code is not perfect because punctuation is left in the file and is stored with the nearest word, but it will suffice for our purposes. Once the textual data is stored in an array, we can begin searching through the array to find words, using Sequential or Binary Search.
31+
32+
33+
**Binary Search Tree Searches**
34+
35+
There are three types of searches typically performed with a BST:
36+
37+
- Searching for a specific value
38+
- Searching for the minimum value
39+
- Searching for the maximum value
40+
41+
Searches in a BST for the minimum and maximum values stored are relatively simple procedures. Since lower values are always stored in left child nodes, to find the minimum value in a BST, you only have to traverse the left edge of the BST until you get to the last node.
42+
43+
To find the maximum value stored in a BST, the function must simply traverse the right links of nodes until the function reaches the right end of the BST. The value stored in this node must be the maximum value.
44+
45+
Searching for a specific value in a BST requires that a comparison be made between the data stored in the current node and the value being searched for. The comparison will determine if the search travels to the left child node, or to the right child node if the current node doesn’t store the searched-for value.
46+
47+
![](https://blog.penjee.com/wp-content/uploads/2015/11/binary-search-tree-sorted-array-animation.gif)
48+
49+
**Depth/Breadth First Search** (in Graph data structures)
50+
51+
DFS and BFS are tree/graph traversing and searching data structures.
52+
53+
![](https://i2.wp.com/codingsec.net/wp-content/uploads/2016/03/dfs-bfs-codingsec.gif)
54+
55+
Applications:
56+
57+
- Used by search engines for web-crawling
58+
- Used in artificial intelligence to build bots, for instance a chess bot
59+
- Finding shortest path between two cities in a map and many other such applications
60+
61+
### Code Examples
62+
63+
[JavaScript built-in findIndex(), find()](../03-chapter-List.js)
64+
[Sequential/Binary Search example](./13-chapter-Searching-Algorithms.js)
65+
[Binary Search Tree Searches](../10-chapter-Binary-Trees.js)
66+
[Depth/Breadth First Search](../11-chapter-2-adjecency-list-Graphs.js)
67+
68+
#### Resources
69+
70+
- [7 algorithms and data structures every programmer must know](https://codingsec.net/2016/03/7-algorithms-data-structures-every-programmer/)

0 commit comments

Comments
 (0)