Skip to content

Commit e0a21dd

Browse files
committed
Searching algorithms chapter added
1 parent 2ac0433 commit e0a21dd

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

13-chapter-Searching-Algorithms.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
class Search {
2+
3+
static sequentialSearch(dataStore, data) {
4+
for (var i = 0; i < dataStore.length; i++) {
5+
if (dataStore[i] === data) {
6+
return i;
7+
}
8+
}
9+
return -1;
10+
}
11+
12+
static binarySearch(dataStore, data) {
13+
let upperBound = dataStore.length - 1;
14+
let lowerBound = 0;
15+
while (lowerBound <= upperBound) {
16+
let mid = Math.floor((lowerBound + upperBound) / 2);
17+
if(dataStore[mid] < data) {
18+
lowerBound = mid + 1;
19+
} else if(dataStore[mid] > data) {
20+
upperBound = mid - 1;
21+
} else {
22+
return mid;
23+
}
24+
}
25+
return - 1;
26+
}
27+
28+
static countOccurrences(dataStore, data) {
29+
let count = 0;
30+
let position = this.binarySearch(dataStore, data);
31+
if(position > -1) {
32+
count += 1;
33+
for (var i = position; i > 0; i--) {
34+
if(dataStore[i] === data) {
35+
count += 1;
36+
} else {
37+
break;
38+
}
39+
}
40+
for (var i = position + 1; i < dataStore.length; i++) {
41+
if (dataStore[i] === data) {
42+
count += 1;
43+
} else {
44+
break;
45+
}
46+
}
47+
}
48+
return count;
49+
}
50+
51+
static minValue(dataStore) {
52+
let min = dataStore[0];
53+
for (var i = 1; i < dataStore.length; i++) {
54+
if (dataStore[i] < min) {
55+
min = dataStore[i];
56+
}
57+
}
58+
return min;
59+
}
60+
61+
static maxValue(dataStore) {
62+
let max = dataStore[0];
63+
for (var i = 1; i < dataStore.length; i++) {
64+
if (dataStore[i] > max) {
65+
max = dataStore[i];
66+
}
67+
}
68+
return max;
69+
}
70+
71+
}
72+
73+
function setData(numElements = 100) {
74+
let dataStore = [];
75+
for (let i = 0; i < numElements; i++) {
76+
dataStore[i] = Math.floor(Math.random() * (numElements + 1));
77+
}
78+
return dataStore;
79+
}
80+
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+
94+
// Implementation
95+
// ##########################################################################
96+
const fs = require('fs');
97+
98+
function readMyData(file) {
99+
let data = file.split(' ');
100+
const word = 'rhetoric';
101+
let start = new Date().getTime();
102+
let position = Search.sequentialSearch(data, word);
103+
let end = new Date().getTime();
104+
console.log((position > 0) ? `${word} is present in the set and is in the position ${position}` : `${word} is not present in the set`);
105+
console.log(`Sequential search took ${end - start} milliseconds.`);
106+
console.log();
107+
108+
insertionSort(data);
109+
start = new Date().getTime();
110+
position = Search.binarySearch(data, word);
111+
end = new Date().getTime();
112+
console.log((position > 0) ? `${word} is present in the set and is in the position ${position}` : `${word} is not present in the set`);
113+
console.log(`Binary search took ${end - start} milliseconds.`);
114+
}
115+
116+
const number = 11;
117+
let dataStore = setData();
118+
const result = Search.sequentialSearch(dataStore, number);
119+
console.log((result > 0) ? `${number} is present in the set and is in the position ${result}` : `${number} is not present in the set`);
120+
console.log();
121+
console.log(`The minimum value is: ${Search.minValue(dataStore)}`);
122+
console.log(`The maximum value is: ${Search.maxValue(dataStore)}`);
123+
console.log();
124+
insertionSort(dataStore);
125+
const resBin = Search.binarySearch(dataStore, number);
126+
const count = Search.countOccurrences(dataStore, number);
127+
const present = `${number} is present in the set and is in the position ${resBin} and has ${count} occurrences`;
128+
console.log((resBin > 0) ? present : `${number} is not present in the set`);
129+
console.log();
130+
131+
fs.readFile('paragraph.txt', 'utf8', (err, data) => {
132+
if (err) {
133+
if (err.code === "ENOENT") {
134+
console.error('myfile does not exist');
135+
return;
136+
} else {
137+
throw err;
138+
}
139+
}
140+
readMyData(data);
141+
});

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The purpose of this repo is to update the exercises to ES6 standards and to corr
2525
| 12.- | [Simple Graphs](./11-chapter-1-MatrixAdjencency-Graphs.js) | 2 | A graph consists of a set of vertices and a set of edges. A map is a type of graph where each town is a vertex, and a road that connects two towns is an edge. Edges are defined as a pair (v1, v2), where v1 and v2 are two vertices in a graph
2626
| 13.- | [Complete Graphs](./11-chapter-2-ArrayListAdjecency-Graphs.js) | 2 | A complete graph is where all vertices are conected with each other.
2727
| 14.- | [Sorting Algorithms](./12-chapter-Sorting-Algorithms.js) | 6 | Two of the most common operations performed on data stored in a computer are sorting and searching. The sorting algorithms are Bubble Sort, Selection Sort, Insertion Sort, Shell Sort and Quick Sort.
28+
| 15.- | [Searching Algorithms](./13-chapter-Searching-Algorithms.js) | 6 | 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.
2829

2930

3031
### To run the examples we need the following:

0 commit comments

Comments
 (0)