two useful sorting functions
|
|
|
|
since javascript will bound variable scope to its declaration,
or says it will keep the local context.
let’s define a function that will return a function for step add.
then we can get an iterator by call the function
and javascript will keep it as a function in numberWithStep2
each time we call the function numberWithStep2
pretty cool! huh?
如果我們想要找
一個 已排序由小到大的數列 array
, 數列左最小, 數列右最大
有 n 個元素
中 指定數值 c
的 第一個出現位置 fst
和 最後一個出現位置 lst
我們可以怎麼做呢?
方法一
我們可以用 loop 整個數列 array
, 然後遂個比對元素, 發現等於 c
, 記下來 fst
繼續 loop 發現不等於 c
, 記下來 lst
這樣的時間複雜度我們標記成
因為 n
個元素, 做 n
次比對
可能覺得一般般, 這效率好像蠻合理的
但如果我們碰到 n
為 1 million 呢?
如果我們今天是把這個演算邏輯套到比較複雜的物件搜尋上的時候呢?
假設每次比對要花上 1 秒, 找一次要花 11 天才算的出結果
等到花兒都謝了…. 😱
這時我們可以套用比較常見一點的演算法…
just add an External Resources
😙
with following link https://getfirebug.com/firebug-lite-debug.js
then you are ready to go with console.log
exactly like testing in FireBug of FireFox.
鑑於最近 mongodb 的裸奔事件頻傳
我也來設定一下我的 mongodb
順便玩一下 MongoHub for Mac 這套 GUI