算法學習

算法學習

遞歸

  1. 調用自身
  1. 終止條件

漢諾塔問題

  • python實現(xiàn):

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n13" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def hanoi(n, a, b, c):
if n > 0:
hanoi(n-1, a, c, b)
print("moving from %s to %s" % (a, c))
hanoi(n-1, b, a, c)

hanoi(3, 'A', "B", "C")</pre>

  • js實現(xiàn)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n17" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">const hanoi = (n, a, b, c) => {
if (n > 0) {
hanoi(n - 1, a, c, b)
console.log(from ${a} to ${c})
hanoi(n - 1, b, a, c)
}
}</pre>

二分查找

  • python實現(xiàn):

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n22" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">def binary_search(li, val):
    left = 0
    right = len(li) - 1
    while left <= right:
    mid = (left + right) / 2
    if li[mid] == val:
    return mid
    elif li[mid] < val:
    left = mid + 1
    else:
    right = mid - 1
    else:
    return None</pre>

  • js實現(xiàn)

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n25" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit;">function binarySearch(arr, val) {
    let left = 0,
    right = arr.length - 1
    let mid
    while (left <= right) {
    mid = Math.floor((left + right) / 2)
    if (arr[mid] === val) {
    return mid
    } else if (arr[mid] < val) {
    left = mid + 1
    } else {
    right = mid - 1
    }
    }
    return null
    }</pre>

列表排序

  • 排序:將一組"無序"的記錄序列調整為"有序"的記錄序列

  • 列表排序:將無序列表變?yōu)橛行蛄斜?/p>

[圖片上傳失敗...(image-81ee26-1612586956561)]

選擇排序

  • python實現(xiàn)

<pre mdtype="fences" cid="n38" lang="python" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def select_sort(li):
for i in range(len(li)-1):
min_loc = i
for j in range(i+1, len(li)):
if li[j] < li[min_loc]:
min_loc = j
li[i], li[min_loc] = li[min_loc], li[i]</pre>

  • js實現(xiàn)

<pre mdtype="fences" cid="n51" lang="javascript" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">function selectSort(arr) {
for (let i = 0; i < arr.length - 1; i++) {
let min_log = i
for (let j = i + 1; j < arr.length - 1; j++) {
if (arr[j] < arr[min_log]) {
min_log = j
}
}
;[arr[min_log], arr[i]] = [arr[i], arr[min_log]]
}
}</pre>

插入排序(可以理解為摸牌插入到手中牌的過程)

  • python實現(xiàn)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n71" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def insert_sort(li):
for i in range(1, len(li)): # i 表示摸到的牌的下標
tmp = li[i]
j = i-1 # j 指的是手里的牌的下標
while j >= 0 and li[j] > tmp:
li[j+1] = li[j]
j -= 1
li[j+1] = tmp
</pre>

  • js實現(xiàn)

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" cid="n57" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">function insertSort(arr) {
for (let i = 1; i < arr.length; i++) {
let tmp = arr[i] // 摸到的牌
let j = i - 1
// 當手中的牌大于摸到的牌并且j>=0
while (j >= 0 && arr[j] > tmp) {
;[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
j -= 1
}
arr[j + 1] = tmp
}
}</pre>

快速排序(歸位過程)

  • python實現(xiàn)

<pre mdtype="fences" cid="n103" lang="python" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def partition(li, left, right):
tmp = li[left]
while left < right:
while left < right and li[right] >= tmp:
right -= 1
li[left] = li[right]
while left < right and li[left] <= tmp:
left += 1
li[right] = li[left]
li[left] = tmp
return left

def quick_sort(arr, left, right):
if left < right:
mid = partition(arr, left, right)
quick_sort(arr, left, mid - 1)
quick_sort(arr, mid + 1, right)</pre>

  • js實現(xiàn)

<pre mdtype="fences" cid="n92" lang="javascript" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">function quickSort(arr, left, right) {
if (left < right) {
let mid = partition(arr, left, right)
quickSort(arr, 0, mid - 1)
quickSort(arr, mid + 1, right)
}
}

function partition(arr, left, right) {
let tmp = arr[left]
while (left < right) {
while (left < right && arr[right] >= tmp) {
right--
}
;[arr[left], arr[right]] = [arr[right], arr[left]]
while (left < right && arr[left] <= tmp) {
left++
}
;[arr[right], arr[left]] = [arr[left], arr[right]]
}
arr[left] = tmp
return left
}</pre>

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市晌杰,隨后出現(xiàn)的幾起案子菩彬,更是在濱河造成了極大的恐慌霞捡,老刑警劉巖廊宪,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摹闽,死亡現(xiàn)場離奇詭異,居然都是意外死亡泽示,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門蜜氨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來械筛,“玉大人,你說我怎么就攤上這事飒炎÷裼矗” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長赤赊。 經(jīng)常有香客問我闯狱,道長,這世上最難降的妖魔是什么抛计? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任哄孤,我火速辦了婚禮,結果婚禮上吹截,老公的妹妹穿的比我還像新娘瘦陈。我一直安慰自己,他們只是感情好波俄,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布晨逝。 她就那樣靜靜地躺著,像睡著了一般懦铺。 火紅的嫁衣襯著肌膚如雪捉貌。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天冬念,我揣著相機與錄音趁窃,去河邊找鬼。 笑死刘急,一個胖子當著我的面吹牛棚菊,可吹牛的內容都是我干的。 我是一名探鬼主播叔汁,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼统求,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了据块?” 一聲冷哼從身側響起码邻,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎另假,沒想到半個月后像屋,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡边篮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年己莺,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片戈轿。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡凌受,死狀恐怖,靈堂內的尸體忽然破棺而出思杯,到底是詐尸還是另有隱情胜蛉,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站誊册,受9級特大地震影響领突,放射性物質發(fā)生泄漏。R本人自食惡果不足惜案怯,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一君旦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧殴泰,春花似錦于宙、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽至会。三九已至离咐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間奉件,已是汗流浹背宵蛀。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留县貌,地道東北人术陶。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像煤痕,于是被迫代替她去往敵國和親梧宫。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內容

  • 原理 直接插入排序的基本思想:每次將一個待排序的記錄摆碉,按其keyword的大小插入到前面已經(jīng)排好的子序列中的適當位...
    MacXin閱讀 232評論 0 0
  • 原理: 選擇排序是一種原址比較算法塘匣,大致思路是在第一輪迭代中找到數(shù)據(jù)結構中的最小值并將其放置在第一位,第二輪迭代中...
    MacXin閱讀 202評論 0 0
  • 算法原理: 1.比較相鄰的元素巷帝。如果第一個比第二個大忌卤,就交換他們兩個。 2.對每一對相鄰元素作同樣的工作楞泼,從開始第...
    MacXin閱讀 264評論 0 0
  • 1. 常用數(shù)據(jù)結構 數(shù)據(jù)結構是指相互之間存在著一種或多種關系的數(shù)據(jù)元素的集合和該集合中數(shù)據(jù)元素之間的關系組成 驰徊。 ...
    孔雨露閱讀 697評論 0 1
  • (以下資料來自百度百科) 查找過程: 首先,假設表中元素是按升序排列堕阔,將表中間位置記錄的關鍵字與查找關鍵字比較棍厂,...
    MacXin閱讀 181評論 0 0