-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinearSearch.ts
41 lines (36 loc) · 1.36 KB
/
linearSearch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Tier, Status } from './tier';
import Util from './util';
/**
* Linear Search tier
* Main resources used: Memory (sequential access) and cache
*/
export default class LinearSearch extends Tier {
goal: number;
/**
*
* @param goal number that will be searched in the array. By default it uses a random generated number.
* This function uses as search array the unsorted array found at the class Util. The size of the array and the goal number impact in the performance of the search.
*/
constructor(goal = Util.randomNumber()){
super();
this.goal = goal;
}
protected executeTask(): Status {
let index = this.linearSearch(Util.unsortedArray, this.goal);
if(index !== -1){
return new Status(`The value ${ this.goal } was found in the unsorted array in the index ${ index }.`);
}
return new Status(`The value ${ this.goal } was not found in the unsorted array.`);
}
/**
* It executes a linear search in an unsorted array
* @param array array that will be used for the search
* @param goal number that will be searched
*/
private linearSearch(array: [number], goal: number): number {
for(let i=0; i<array.length; i++){
if(array[i] === goal) return i;
}
return -1;
}
}