search module for fs
This commit is contained in:
33
src/rt/shell/fs/search.ts
Normal file
33
src/rt/shell/fs/search.ts
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
interface Search {
|
||||||
|
binary: <T>(list: T[], find: T) => T | undefined,
|
||||||
|
linear: <T>(list: T[], find: T) => T | undefined,
|
||||||
|
}
|
||||||
|
const search = {} as Search
|
||||||
|
|
||||||
|
search.binary = function(list, find) {
|
||||||
|
list.sort()
|
||||||
|
let start = 0
|
||||||
|
let end = list.length-1
|
||||||
|
while (start<=end) {
|
||||||
|
const median = (start+end)>>1
|
||||||
|
if (list[median] === find) {
|
||||||
|
return find
|
||||||
|
} else if (list[median]<find) {
|
||||||
|
start = median+1
|
||||||
|
} else {
|
||||||
|
end = median-1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
search.linear = function(list, find) {
|
||||||
|
for (const item of list) {
|
||||||
|
if (item === find) {
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
export default search
|
Reference in New Issue
Block a user