Professional Documents
Culture Documents
Dsa by DR - Mma (P.V)
Dsa by DR - Mma (P.V)
Conclusion 337
ပညာတ ွေ သင်က ာြားဖို့ အခွေငဲ့်အတ ြားတပြားခဲ့က တသာ မသာြားစ၊ ငယ်စဉ်မှစပပြား ယခအခ န်အထ ပညာမ ာြား
သင်က ာြားတပြားခဲ့က တသာ သင်ဆ ာ၊ မမင်ဆ ာ၊ က ာြားဆ ာမ ာြား၊ စာသင်က ာြားမခင်ြား လပ်မဖစ်တအာင်
ဆယ်စနှစ်တ ာ် င်တအာင် စာသင်က ာြားဖို့ အခွေငဲ့်အတ ြားတပြားခဲ့က တသာ၊ IT Field မှာ င်လည်ဖို့
အခန်း (0)
Computer System
Computer System ဆတ
ို ာ ဘာလဲ မပ ာခင် computer တစ်လိုုံးရဲဲ့ အလို ်လို ် က
ို ို
အရင်ပ ာ ါ့မယ်။
Processing
Memory
1|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ “ပခ င်ုံးပတွထူ ပ
ူ ေပအာင် ဘယ်သူလဲ စဉ်ုံးစာုံးတယ်” ဆိုတာ processing ။ ဘယ်လို
မတ်ဉာဏ်ပ ျာက်တါ့သ
ဲ ူပတွဆိုရင် ဘာမ မမတ်မဘူုံးဆိုတာ သမ်ုံးထာုံးတာပတွ
ယိုယင
ွ ်ုံးကိုေ်လိုဲ့ ရာမပတွဲ့ပတာါ့တါ့သ
ဲ ပဘာ ။ လူပတွမာ စဉ်ုံးစာုံးတာ တွက်ချက်တာ
Unit) က အဲဒအလို ်ပတွကို လို ်ပဆာင် တယ်။ ဒ ပကကာငါ့် CPU ကို brain of
computer လိုဲ့ ပခေါ် တယ်။ CPU ာ computer ရဲဲ့ အပရုံးကကုံးဆိုုံး အစတ်အ ိုင်ုံး
ရ တယ်။
▪ တက
ို ်စစ်လိုဲ့ ပတွဲ့သွာုံးပ ဆိုပတာါ့ ပ ုံးပ ုံးကကုံး အမည်ကို ပရရွတ်လိုက်တယ်ဆိုတာ
output ကို ုံးစ ်ကပေ ထိုတ်လိုက်တာ ဖစ် တယ်။ Computer လိုပ ာမယ်
ဆိုရင်ပတာါ့ မျက်နောရယ်၊ ုံးစ ်ရယ်ဆိုတဲါ့ output device ၂ ခိုကို အသိုုံး ပ ုံး output
ထိုတ်သွာုံးတာ ။
2|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒလို ဲ၊ computer ဆိုတာ လူေဲဲ့ ဆင်တူ တယ်။ သူရဲဲ့ input devices ပတွကပတာါ့ mouse,
ဆိုပ ုံး အသပလုံးမည်မည်ပ ုံး ဖတ်တဲါ့စက်ပလုံး)၊ scanner(တစ်ချ ဲ့ရိုုံးပတွမာ လက်ပ ွတိုဲ့ မျက်နောတဲ့ိုေဲဲ့
1. ROM (read only memory) – read only ဆိုတဲါ့ အတိုင်ုံး ဖတ်လိုဲ့ ဲ ရ တယ်၊ သွာုံး င်တာ
data(စာပတွ၊ သချင်ုံးပတွ၊ ပ
ို တွ စသည် ဖငါ့်) ပတွကို ထေ်ုံးသမ်ုံးထာုံးနိုင် တယ်၊ power
မေ်သမျှကို main memory ပ ေါ်ဆွဲတင်ပ ုံး အလို ်လို ် တယ်။ သူကပတာါ့ power
3|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
စက်ဝယ်တိုေ်ုံးက ဆိုင်ကပေပ ုံးပတာါ့ စာရိုက်တါ့ဲ software (e.g. Microsoft word) ကို စက်ထဲ
ထညါ့်ပ ုံးလိုက်တယ် ဆို ပတာါ့။ အဲဒ ဆိုရင် အဲဒ software ာ စက်ထဲ အပမဲရပေမာ ၊ ဘယ်အချေ်
အလို ်လို တ
် ာ ဖစ်လိုဲ့ Main Memory ပ ေါ်ပရာက်လာတာ ။ စာရိုက်တယ် စာရိုက်တယ် Main
ရိုက်လက်စ စာပတွ ပ ျာက်သွာုံးမာ ဖစ် တယ်။ ဘာလိုဲ့လဲ ဆိုပတာါ့ Main Memory ဆိုတာ power
System ဆိုတာ input devices (input components), output devices (output components),
output က အပ ဖ ေ်ထိုတ် မယ်။ အဲဒမာ input ၊ processing နငါ့် output devices ပတွ ာ data
4|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
memory ပတွ ာ အလို ်လို ်နိုင်စွမ်ုံး (speed) ေဲဲ့ သမ်ုံးဆည်ုံးနိုင်တါ့ဲ မာဏပတွ (size) ပတွ
မတူညကက ဘူုံး။
အရ ဘ
် ယ်ပလာက်ရည်သလဲ ဆိုရင် လက်မ(inches)၊ ပ (feet)၊ မတာ (meter) တဲ့ေ
ို ဲဲ့
တင
ို ်ုံးတယ်၊ ခနဓောကိုယ် အပလုံးချေ်ကို kg, lb တိုဲ့ေဲဲ့ တင
ို ်ုံး တယ်။ အဲဒလို ဲ memeory ပ ေါ်မာ
တစ်ပ မာ ၁၂ လက်မ ဆိုတဲါ့ ပ ေဲဲ့ လက်မကကာုံး ဆက်သွယ်ချက် ရသလို ဲ bit, byte, KB, MB,
ရ တယ်။
***အလွယအ
် ပေေဲပ
ဲ့ တာါ့ 1024 အစာုံး 1000 လိုဲ့ပ ာပလါ့ရ တယ်****
5|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
စာအို ်အကကမ်ုံးပလုံးပတွက
ပရုံးလိုကရ
် င်လည်ုံး အပေတာ ဲ၊
ပ ုံးပတာါ့ အဲဒထဲမာ
မတ်စပ
ို လုံးပတွပသချာပရုံး၊ ိုစဆာပလုံးပတွ ပသချာတွကလ
် ိုကလ
် ိုဲ့
ခစာုံးချက်ကို သ ်သပဘာကျတာ။”
6|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁)
Numbering Systems
1 Binary 2 0,1
2 Octal 8 0,1,2,3,4,5,6,7
3 Decimal 10 0,1,2,3,4,5,6,7,8,9
4 Hexadecimal 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
စနစ် ဲပြစ် ါတယ်။ သူဟှာ base 10 ါ။ ဒါန ကှာငော့် ဂဏန်ျိုး 10 လျိုးက ဲ ခွငော့်ပ ါတယ်။ 10 လျိုးက 0
လို့နခေါ်တဲော့ ကန်ျိုးဂဏန်ျိုးတစ်လျိုးမှာ digit 3 လျိုး ါနန ါတယ်။ 1 ေယ် 2 ေယ် 9 ေယ် ါ။ ေှာဂဏန်ျိုး
စတင်နေတွက်လက်နတှာော့ 1 မှာ 2 လျိုးပ ညော့် ါတယ်။ ဒါန ကှာငော့် 0,1 ကသှာ binary မှာ ခွငော့်ပ ါတယ်။
7|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
စတင်နေတွက်လက်နတှာော့ 7 မှာ 8 လျိုးပ ညော့် ါတယ်။ ဒါန ကှာငော့် 0,1,2,3,4,5,6,7 ကသှာ octal မှာ
ခွငော့်ပ ါတယ်။
စတင်နေတွက်လက်နတှာော့ 15 မှာ 16 လျိုးပ ညော့် ါတယ်။ အဲဒီမှာ 0 to 9 ကနတှာော့ 0 to 9 လို့ နေျိုးပ ီျိုး၊ 10
လက်မက န ြွွဲ့ဲ ၊ န က လက်မြွဲွဲ့ နဲို့ လအ ်သလ နပ ှာင်ျိုးပ ီျိုး တွက် ကေသလို့ Numbering
▪ Decimal ကလှာေင် နပမှာက် ါမယ်။ binary ကနနလှာေင် binary ေဲွဲ့ base ပြစ်တဲော့ 2
▪ Decimal ကသွှာျိုး စှာျိုး ါမယ်။ ဘှာနဲို့စှာျိုးမလဲ ဆေင် binary ကသွှာျိုးေင် binary ေဲွဲ့ base
နဲို့စှာျိုး လက်ရ ါ ဲ။
8|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Binary to Decimal
(1101.101)2 = (?)10
စဆှာက base 2, binary ကနန base 10, decimal က နပ ှာင်ျိုးခင်ျိုးတှာပြစ်လို့ 2 power နတွနဲို့
နပမှာက် ါမယ်။ 2 power နတွနဲို့ နပမှာက်တဲော့ အခါ ဒဿမ နေွဲ့ က ်ေက်က ်ကန်ျိုး 0 ကနနပ ီျိုး
Octal to Decimal
(120.1)8=(?)10
= 1* 82 + 2* 81+ 0* 80 + 1* 8-1
= 1*64 + 2 * 8 + 0 * 1 + 1/8
= 64 + 16 + 0 + 0.125 = (80.125) 10
9|Page
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Hexadecimal to Decimal
(1A9)16= (?)10
(123)10 = (?)2
2 123
2 30(61 က 2 ပြငော့် စှာျိုးလေို့ နသှာ စှာျိုးလဒ်), 1(61 က 2 ပြငော့် စှာျိုးလေို့ နသှာ အကကွင်ျိုး)
2 15, 0
2 7, 1
2 3, 1
2 1, 1
2 0, 1 (စာ်းလဒ 0 ဖြစသာ်းလျှငရပပါ)
10 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
= 64 + 32 + 16 + 8 + 2 + 1
= (123) 10
် 4,……..
= 1*64 +1* 32 +1* 16 +1* 8 +0*4+1* 2 +1* 1 (၁ ပြစ်တဲော့ နနေှာနတွ ယူန ါင်ျိုးလက်လည်ျိုးေ ါတယ်)
= 64 + 32 + 16 + 8 + 2 + 1 = (123) 10
11 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Decimal to Octal
(123)10 = (?)8
8 123
8 15, 3
8 1,7
8 0,1
(123)10 = (173)8
(173)8 = 1* 82 + 7* 81 + 3* 80
= 1*64 + 7*8 +3
= 64 + 56 + 3 = (123) 10
1 7 3
= 64 + 56 + 3 = (123) 10
12 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Decimal to Hexadecimal
(123)10 = (?)16
16 123
16 7, 11 (B)
16 0, 7
(123)10 = (7B)16
7 B
16 (161) 1 (160)
=112 + 11 = (123)10
ပြစ် ါတယ်။ ဒဿမ နေွဲ့အ င်ျိုးကနတှာော့ ျိုးစအတင်ျိုး စှာျိုးပ ီျိုး၊ ဒဿမ အနနောက် င်ျိုးကနတှာော့
13 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
(123.125)10 = (?)2
ဒဿမ နေွဲ့ အ င်ျိုးကနတှာော့ အေင်အတင်ျိုး စှာျိုးလဒ် 0 ေသည်အ ်ခါ ်ခါ စှာျိုး ါမယ်။
2 123
2 61, 1
2 30, 1
2 15, 0
2 7, 1
2 3, 1
2 1, 1
2 0, 1
ဒဿမ နနောက် င်ျိုးကနတှာော့ ်ခါ ်ခါ နပမှာက် ါမယ်။ Binary ကသွှာျိုးမှာ ပြစ်လို့ 2နဲို့ နပမှာက် ါမယ်။ Octal
ဆ 8, hexadecimal ဆ 16 န ါ ော့။
14 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
နပမှာက်လေ
ို့ လှာတဲော့အနပြေဲွဲ့ ဒဿမနနောက် မှာ 0 မ ွက်နသျိုးလို့ ်နပမှာက်မယ်ဆေင် ပ န်နပမှာက်ြို့
နပမှာက်လေ
ို့ လှာတဲော့အနပြေဲွဲ့ ဒဿမ နနောက်မှာ 0 ဲေနတှာော့ေင် ေ ် ါမယ်။ တစ်ခ ွဲ့ဟှာနတွမှာ
တွကတ
် တ်နလှာက် ါပ ။ီ နအှာက်က စဆှာနလျိုးနတွ တွက် ကညော့် ကညော့် ါ။
(2345.140625)10 = (?)2
(2345.140625)10 = (?)8
(2345.140625)10 = (?)16
(246.8)10 = (?)2
(246.8)10 = (?)8
(246.8)10 = (?)16
15 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လျိုးေ ါမယ်။
22 = 4 21 = 2 20 = 1
Octal to Binary
(127)8 = (?)2
16 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Binary to Octal
001 111 011 (နနောက်ကနနစပ ီျိုး ၃ လျိုးတစ်ပြတ် ပြတ် ါ ော့မယ်။ လအ ်ေင် အနေွဲ့မှာ 0 ညော့် ါ)
001 = 1
111 = 4+2+1 = 7
011 = 2+1 = 3
23 = 8 22 = 4 21 = 2 20 = 1
Hexadecimal to Binary
(A7)16 = (?)2
A (10) = 8+2 = 1010 (အန ေါ်ကဇယှာျိုးက ကညော့် ါ 10 ဆနတှာော့ 8+2 ယူ ါမယ်၊ 1,4 နနေှာ မယူ ါ)
17 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Binary to Hexadecimal
(1111011)2 = (?)16
0111 1011 (နနောက်ကနန စပ ီျိုး 4 လျိုးတစ်ပြတ် ပြတ် ါ ော့မယ်။ 4 လျိုးမပ ညော့်ေင် နေွဲ့မှာ 0 ညော့)်
Hexadecimal နဲို့ Octal အခ င်ျိုးခ င်ျိုး အပ န်အလန်နပ ှာင်ျိုးခ င် ကေင် Binary က ကှာျိုးခပ ီျိုး
နပ ှာင်ျိုးလို့ေ ါတယ်။
Octal to Hexadecimal
(173)8 = (?)16
18 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Hexadecimal to Octal
(7B)16 = (?)8
ညော့်လေ
ို့ ါတယ်)
(7B)16 = (173)8
Character
ကျွန်မတို့ နနို့စဉ် နေှာင်ျိုးတယ် ဝယ်တဲော့ စတဲော့ တွက်ခ က် ကတဲန
ော့ နေှာနတွမှာ အသျိုးပ
နန ကတှာက Decimal System ပြစ် ါတယ်။ Computer ကနတှာော့ on/off ဲေတှာမို့ on ဆေင် 1, off
ဆ၇င် 0 စသည်ပြငော့် သတ်မတ်တှာန ကှာငော့် 0 နဲို့ 1 ခွငော့်ပ တဲော့ binary system က အသျိုးပ ါတယ်။
အလ ်မလ ် ါဘူျိုး။ A (A အကကီျိုး)ေဲွဲ့ သတ်မတ် ှာျိုးတဲော့ တန်ြျိုး 65 က binary နပ ှာင်ျိုးပ ီျိုး 0100 0001
19 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Character Decimal
A 65
B 66
…
a 97
b 98
ဝင် ကညော့်လေ ါတယ်။ အဲဒီ ဲမှာ Decimal တန်ြျိုး၊ Octal တန်ြျိုးနတွ ါ ါတယ်။ Decimal ကနန
https://en.wikipedia.org/wiki/List_of_Unicode_characters
Color
Web design င်ျိုးက နလော့လှာလို့ website နတွ နေျိုးသှာျိုးြန်တီျိုးတဲော့ အခါမှာ color ကနြေါ်ပ တဲော့
နနေှာမှာ red, green, blue ဆတဲော့ အနပခခ အနေှာင် ၃ နေှာင်န ေါ်မှာ အလ ်လ ် ါတယ်။ r (red),
Hexadecimal နပ ှာင်ျိုး ကညော့်ေင် FF ေ ါတယ်၊ ဒါန ကှာငော့် decimal တစ်လျိုးမှာ hexadecimal 2 လျိုးနဲို့
နေျိုးလို့ေ ါတယ်။
20 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ပ ီျိုးေင် heaxadecimal နဲို့ နေျိုးမယ်ဆေင် နေွဲ့က # ညော့်နေျိုးေ ါတယ်၊ ဒါန ကှာငော့် web site မှာ
color အနီလို့ နေျိုးခ င်ေင် - rgb (255, 0 , 0) or #FF0000 လို့ နေျိုးလို့ေ ါတယ်။
အနီ နဲို့ အစမ်ျိုးစ ်ေင် လနမ္ှာ်ေ ါတယ် အနအေငော့်ကနတှာော့ 0 ကနန 255 အတွင်ျိုး အတျိုးနလ ှာော့
rgb (255, 165, 0) => #FFA500 , rgb (238, 154, 0) => #EE9A00
အဲဒီနစ်ခေဲွဲ့ အနေှာင် ကွှာပခှာျိုးခ က်က ကညော့်ခ င်ေင် internet မှာ rgb တန်ြျိုးပြစ်ပြစ်၊
အနေှာင်စ တ
် ှာက စတ်ဝင်စှာျိုးြို့ အင်မတန် နကှာင်ျိုး ါတယ်။ အနေှာင်စ ်တဲော့ အနပခခလည်ျိုး
https://cloford.com/resources/colours/500col.htm
အသျိုးပ ါတယ်။ အဲဒီ ဲကမ character code နဲို့ color နစ်ခ ဲ နမူနောပ
21 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အကန်လ ်ခော့ေ
ဲ ဲွဲ့လှာျိုးနတှာော့မသဘူျိုး။
မ ကှာခင်နမော့မှာပြစ် ါတယ်။
အပ တ်ပြတ်ခန
ော့ဲ ျိုး ါ”
22 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၂)
Boolean algebra
Logical Operators
Truth Table
0(False) တှိို့ုပဲဖြစပါတယ။ 1 ဟာ true နင တူပပ်း၊ 0 ဟာ false နဲို့ တူပါတယ။ 01 နဲို့ ရရ်းလည်း ရသလှို၊
23 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Not A
A Not A (!A)
0 1
1 0
ဒီလ table တလားဆတ က truth table လဲ့ တခေါ်ပါတယ်။ A ဆတ တလား တစ်ခပ ပါလဲ့
A and B, A or B, A xor B
တစ်ဝက်ဝက်တတ ော့ 2 ခရပါတယ်။ ဒါရ ာင A ှို 0(False) နစလု်း၊ 1(True) နစလု်း ရရ်းမာ
ဖြစပါတယ။ 2 ခု ှို ထပပပ်း တစဝ ဝ ရတာ 1 ဒါရ ာင B ှို 0(False) တစလု်း၊ 1(True)
A B A and B A or B A xor B
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
▪ And ကတတ ော့ အ ားလားမှနမ
် ှ True(1)
24 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
တစ်ဝက်ထပ်ဝက်တတ ော့ 2 ရပါတယ်။ ဒါတ က ငော့် B အတက် 0(False) 2 ခ၊ 1(True) 2 ခဆပပီား ထပ်ထပ်
De Morgan’s law
တအ င်ပါမယ်၊ တစ်ဘ သ ကျတ နဲ့ စ တမားပ ကျမယ် ဆပါတတ ော့။ ဒါဆ တအ င်တယ်ဆတ ၃
25 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Not(Inversion) ဆတ ကတတ ော့ ဆနဲ့်ကျင်ဘက်တ ပ င်ားတ ပါ။ true ှို not လုပရင false
ဖြစပပ်း၊ false ှို not လုပရင true ရပါတယ။ ထပမတရမာ and ှို not လုပရင or ရပပ်း၊ or ှို
တအ င်တ က တရားရင်
ကျတ က တရားတအ
ော့ ခါ တစ်ဘ သ ကျရင် ကျတ ြစ်လဲ့
တအ င်တ က not လပ်ရင် ကျတ ရပါတယ်။ တအ င်တ ရဲ့ and က not လပ်တတ ော့ or
ရပါတယ်။ တစ်ခနဲ့ တစ်ခ နှိုင်ားယှဉ်ရင် ြစ်နင်တချ ၃ ခပ ရှပါတယ်။ ငယ်တ ၊ ညီတ နဲ့ ကကီားတ တဲ့
ြစ်ပါတယ်။ ဒီတတ ော့ တအ င်တ ရဲ့ >= က not လပ်တတ ော့ ကကီားတ သမ
ဲ့ ဟတ် ညီတ မဟတ်ရင်
ငယ်တ ပ ကျန်ပါတတ ော့တယ်။ ဒါတ က ငော့် <40 ြစ်တ ပါ။ ကျတ ရဲ့ ဆနဲ့်ကျင်ဘက်ကလည်ား
တအ င်တ ြစ်လဲ့ ကျတ က not လပ် ကညော့်ပါ။ တအ င်တ အတက် တရားထ ားတ ရပါလမော့်မယ်။
! (A ∧ B) =! A ∨ !B
! (A ∨ B) = !A ∧! B
26 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
A B A∧B !(A ∧ B) !A !B !A V !B
0 0 0 1 1 1 1
0 1 0 1 1 0 1
1 0 0 1 0 1 1
1 1 1 0 0 0 0
INVERSION law
!(!A)=A
A !A ! (!A)
0 1 0
1 0 1
Commutative law
A∧B=B∧A
AVB=BVA
27 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Associative law
(A ∧ B) ∧ C = A ∧ (B ∧ C)
(A V B )V C = A V (B V C)
Distributive law
A ∧ (B V C) = (A ∧ B) V (A ∧ C)
A V (B ∧ C) = (A V B) ∧ (A V C)
And law
A ∧ 0= 0
And က အ ားလားမှနမ
် ှ မှန်တ ြစ်တော့အတက် 0 နဲ့ and လပ်မှတတ ော့ တစ်ခက 0 ြစ်တနတော့အတက် 0 ပ
ထက်မှ ြစ်ပါတယ်။
A ∧ 1= A
And က အ ားလားမှနမ
် ှ မှန်တ ြစ်တော့အတက် တစ်ခက 1 ြစ်တနတတ ော့ A က 0 ဆ 0 ထက်မှ ြစ်ပပီား၊
A က 1 ဆ 1 ထက်မှ ြစ်ပါတယ်။
28 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
A ∧ A= A
A ∧! A= 0
A နဲ့ !A and လပ်မှတတ ော့ တစ်ခက 0 ဆ ကျန်တစ်ခက 1 ဒီတတ ော့ နှစ်ခလား 1 မဟတ်လဲ့ အတ ြက 0၊
မင်သ တအ င် ပရရင်
0 0 0 1 0 1 0 0
1 0 0 1 1 0 1 0
Or law
A V 0= A
A V 1= 1
Or က အနည်ားဆား တစ်ခမှနတ
် နဲ့ မှနတ
် ြစ်တော့အတက် 1 နဲ့ Or လပ်မှတတ ော့ တစ်ခက 1
A V A= A
29 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
A V! A= 1
A နဲ့ !A or လပ်မှတတ ော့ တစ်ခက 0 ဆ ကျန်တစ်ခက 1 ဒီတတ ော့ တစ်ခ 1 ြစ်လဲ့ အတ ြက 1၊ တစ်ခက
Operator Precedence
Priority Operators
1 ()
2 !
3 And (∧)
4 Or(∨)
အတပေါ်က Table မှ ပထ ားတော့ အစဉ်လက်အတင်ား အလပ်လပ်ပါတယ်။ ဆလတ က ()-
လက်သည်ားကင်ား ဟ ဦားစ ားတပား နပါတ်တစ် ြစ်ပါတယ်။ သပပီား ရင် not က ဒတယ ဦားစ ားတပား၊ ပပီားမှ
သတ်မှတခ
် ျက်တတဟ အနည်ားငယ် က ပ ား တတ်ပါတယ်။
A∧B∨C
30 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
A ∧ B က အရင် လပ်ပပီား ရလ တော့ အတ ြကမှ C နဲ့ v လပ်မှ ြစ်ပါတယ်။ and က or ထက် priority
A ∧ (B ∨ C)
!A ∧ (B ∨ C)
bar တလားနဲ့တရားပါတယ်။
!(A ∧ B ∧ C) V !(!A V B)
= (ABC) + (A + B)
= (A + B + C) + A B
= A + B + AB + C
= A + B (1 + A) + C
= A +B 1 + C
= A +B + C
=(!A V !B V !C)
31 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
!(A ∧ B V A ∧ C) V (!A ∧ !B ∧ C)
= (AB+AC) + (A B C)
= ( A + B) (A + C) + (A B C)
=AA+AC+ A B+B C+ A B C
=A ( 1 + C + B + B C) + B C
= A. 1 + B C
=A + B C
အခတက် ပတော့ ပစဆ နှစ်ပဒ်က တစ်ပဒ်ချင်ားရဲ့ တမားခန်ားနဲ့ ရှင်ားထ ားတော့ အတ ြနှစ်ခတညီတ က င်ား Truth
a. A ∧ B ∧ C ∧ D,
b. A V B V C V D,
32 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
a. (A ∧ B ∧ C) V (A ∧ B ∧ C ∧ D) V (A ∧ B)
b. !(A V B) V !A V !D ∧ (C ∧ D)
c. !(!A ∧ !B) ∧ (A V B V C)
▪ အ ားလားမှနမ
် ှ အလပ်လပ်ချင်တယ်ဆရင် and, အနည်ားဆား တစ်ခမှန်တ နဲ့ အလပ်လပ်ချင်ရင် or,
▪ တစ်ခနတ
ဲ့ စ်ခ နှိုင်ားယှဉ်ရင် ြစ်နင်တချ သားခပရှပါတယ်၊ ငယ်တ ၊ ညီတ နဲ့ ကကီားတ တဲ့ ြစ်
ပါတယ်။
33 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၃)
သခ္ချာဆိုငရျာ အခ ခခံမချာ်း
Integer, Double
ကိန့််းပြည့်ကိို integer လိိုို့ ခ ေါ်ပြ်းခ ၊ ဒဿမကိန့််းကိို float or double လိိုို့ခ ေါ်ြါ ယ့်။
Absolute Values
Absolute ကခ အခြါင့််းကိန့််းပ စ့်ပ စ့်၊ အနှု ့်ကိန့််းပ စ့်ပ စ့် ြက ိ န့် ိုိ်း(အခြါင့််း)ကိို
34 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
(-3 နှင့် -4 မှ -4 ကငယ့် ပ စ့်လိိုို့ floor မှ -4 ရပြ်း, ceiling မှ -3 ရ ပ စ့်ြါ ယ့်၊ round
က ထို်းစအ င
ိို ့််း.5 ခအ က့်က ဒဘက့် .5 နြဲို့ အထက့်က ဟိိုဘက့် ဆိိုပြ်း န်း ကိိုယူ ြါ။)
Arithmetic Operators
+ 2+3=5
- 5–2=3
* 2 * 5 = 10
/ 7/2 = 3 (စ ားလဒ်)
Mod 7 mod 2 = 1 (အကြွင်ား)
ခြေါ်မူ ည့်ပြ်း အလိုြ့်လိုြ့်ြို ကွေြဲပြ ်း ့် ခလ်း သ ိထ ်းခစ ျင့်ြါ ယ့်။ Java မှ ဆိိုရင့်
ကိန့််းပြည့်ကိို ကိန့််းပြည့်နြဲို့ စ ်းရင့် ကိန့််းပြည့်ရပြ်း (e.g. 5/2 =2)၊ အနည့််းဆို်း ဒဿမ စ့် ိုြါ နြဲို့
ဒဿမ ထွေက့်ြါ ယ့် (e.g. 5.0/2 = 2.5, 5/2.0= 2.5, 5.0/2.0 = 2.5)။ ဒါခြမြဲ PHP မှ ဆိိုရင့်ခ /
35 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Summation Symbol
စ့် ိုနြဲို့ စ့် ိုကက ်း အခြါင့််း လကခဏ ထည့်လိိုက့်ခ ည ဘက့်ပ မ့််းမှ ပ နို့့်ပြထ ်း ြဲ အ ိိုင့််း
ကိို 3 ါ ခြါင့််း ဟ c*3 နြဲို့ အ ူြါြြဲ။ ကယ့်လိိုို့ n=5 ဆိို ြဲရင့် c+c+c+c+c ပ စ့်ြါမယ့်။
အြဲဒါကလည့််း c*5 နြဲို့ အ ူြါြြဲ။ ထပခါ ထပခါ ခပါင်းတျာက ခ မျာကတျာနဲ့တူပပ်း၊ ထပခါ ထပခါ
အြဲဒလိိုနြဲို့ သူခပြ ြဲ 5 အထိ အစ ်းထိို်းသွေ ်းြါ ယ့်။ ∑ ဆိို ြဲ အ ွေက့် ကက ်းထြဲမှ အခြါင့််း
36 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လကခဏ ထည့်သွေ ်းြါ ယ့်။ ဒခလ က့်ဆိို ∑ (summation symbol) အခကက င့််း
နော်းလည့်ခလ က့်ြါပြ။
-1 + - 2 + -3 + -4 + -5 + -6 + -7
1 + 2 + 3 + 4+ 5 = 15
ြခ
ို သနည့််းနြဲို့ ထည့် ွေက့်ကကည့်ြါမယ့်။ n ဆိို ခနောက့်ဆို်းဂဏန့််း (အကက်းဆို်းဂဏန့််း)
န့် ိို်း ပ စ့်ြါ ယ့်။ ၁ ကခန ၅ အထိခြါင့််း ြဲ ခနရ မှ ၅ သည့် အကက်းဆို်း ခနောက့်ဆို်းကိန့််းပ စ့်လိိုို့ n
37 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1 ကစ မျိြု်းမဟို ြ
့် ြဲ စ ျင့် ြဲ ဂဏန့််းကခနစပြ်း အစဉ့်လိိုက့်ပ စ့် ြဲ ကိန့််းဂဏန့််းခ ွေ
ခြါင့််း အ
ြဲ ါမှ အလွေယ့်သို်းလိိုို့ရ ြဲ formula ပ စ့်ြါ ယ့်။ Formula မှ ခ စ ြဲ ဂဏန့််းကိို m
ြမ
ို ှနအ
့် င
ိို ့််း ွေက့်ကကည့်ြါမယ့်။
5 + 6 + 7+ 8 + 9 + 10 = 45
နှစ့်ထြ့်အထိလိိုို့ ခပြ အ
ြဲ ွေက့် n န့် ိို်းသည့် 5 ပ စ့်ြါမယ့်။ ြခ
ို သနည့််းထြဲ အရင့်ထည့်
ွေက့်ကကည့်ရခအ င့်ြါ။
ြမ
ို ှန့် ွေက့်ကကည့်လည့််း 55 ြါြြဲ။12 + 22 + 32 + 42+ 52 = 1+ 4+ 9+ 16 + 25 = 55
38 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒါကခ 3 ထြ့်ကိန့််း အစဉ့်လိိုက့် ခြါင့််း ြဲ Formula ပ စ့်ြါ ယ့်။ ကြဲ ၁ ကခန 5 အထိ 3
ြမ
ို ှန့် ွေက့်ကကည့်ကကရခအ င့်။ 1 + 8 + 27 +64 + 125 = 225
ွေက့်လိရ
ိုို့ ြါ ယ့်။ စ က m, ဆို်း က n၊ ဒခ
ွေက့်လိိုို့ ရခနခသ်း ြါ။ ကယ့်လိိုို့ ၁ ကခန ၁၀၀၀ ၊ိိုို့ ၁ ကခန ၁၀၀၀၀ ို့ဆ
ိို ိိုို့ ြမ
ို ှန့် ျ ွေက့် ို့ိို
မလွေယ့်ြါဘူ်း။ Calculator နြဲို့ ွေက့်မယ့်ဆိိုရင့်ခ င့် ၁ ကခန ၁၀၀၀ ထိဆိိုရင့် ရိိုက့်ခနရ ကိို
မလွေယက
့် ူလှြါဘူ်း။ အ ျိန့်အခ ့်ယူြါလိမ့်မယ့်။ မှ ်းရိိုက့်မိ လည့််း ပ စ့်နင
ိို ့်ြါခသ်း ယ့်။
ဒအ ွေက့် ယ ိုခပြ ပြ ြဲ အခပ Formula ခ ွေက သိထ ်း ို့ိို လိိုြါ ယ့်။ လူခ မ
ွေ ှ စ့် ို ျင့််း
39 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အ ူ ူြါြြဲ။ ဒါခကက င့် စ့် ိုမှ ့်ထ ်းခစ ျင့် က computer program ခ ွေခရ်း ြဲ အ ါမှ
ထြ့် ါ ထြ့် ါ စ့် ို ျင့််း ွေက့် ထက့် Formula သို်း ွေက့် ပမန့် ြဲအ ွေက့် ့်နင
ိို ့်သမျှ
1. 1 + 2 + 3+ 4+ ……+10000
2. 45+ ……+999
5. Find ∑20
1 5 and ∑45 10.
70
Factorial
n! = n * (n-1) * ….* 3 * 2 * 1
0! = 1, 1! = 1
2! = 2 * 1 = 2 * 1! = 2
3! = 3 * 2 * 1 = 3 * 2!
4! = 4 * 3* 2 * 1 = 4 * 3!
40 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒကခန မှ ့်လိရ
ိုို့ က n! = n (n -1)! ပ စ့်ြါ ယ့်။ ခပြ င့််းပြန့်ပြန့် ွေက့်လိလ
ိုို့ ည့််း ရြါခသ်း ယ့်။
4! = 4 * 3 * 2 * 1
3! = 3 * 2 * 1 = 4! / 4
2! = 2* 1 = 3!/3
(𝒏+𝟏)!
𝒏! =
(𝒏+𝟏)
ယူပြ်း ွေြဲ ပ စ့်ြါ ယ့်။ Permutation က order ကိို ထည့်ပြ်း စဉ့််းစ ်းြါ ယ့်။ Combination
Permutation Combination
n=3,r=3 N=3,k=3
41 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Fibonacci Series
F3 = F2 + F1
F4 = F3 + F2
Fn = Fn-1 + Fn-2
F2 = F1 + F0, F2 = 1 + 0 =1
42 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒခနရ မှ သ ိထ ်းခစ ျင့် က - (အနို ့်) န့် ိို်းကိို 1/3 ထြ့်ရှ လိိုို့ရြါ ယ့်။ ဘ ခကက င့်လြဲ
1
ဆိိုရင့် အနို ့်ကိို မကိန့််းထြ့် င့်ရင့် အနို ့်ရလိိုို့ အနို ့်ကိန့််းကိို 1/3, 1/5 စ ြဲ ထြ့်ခ ွေ
မြွိနား်
သိခ
ိုို့ သ ့် -41/2 ဆိိုရင့်ခ အခပ ရှ လိိုို့ရမှ မဟို ြ
့် ါဘူ်း။ အနို ့်ကိို စိုကိန့််းထြ့် င့်ရင့် + ြြဲ
1
ရြါ ယ့်။ ဒါခကက င့် အနို ့်ကိို ထြ့် င့်ပြ်း ွေက့်လိိုို့မရြါဘူ်း။
စုံြွိနား်
y= logb x ➔ by = x
log2 8 = 3 ➔ 23 = 8
logb 1 = 0 ➔ b0 = 1
logb b = 1 ➔ b1 = b
43 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
3. 2-5, 82/3, 16-4/3, log2 16, log10 10000, log2 (1/8), log2 0.125
4. ခအ က့်ြါ ို့က
ိို ိို log ြစ
ို ပ င့် ပြန့်ခရ်းပြြါ။
a) 104
b) 25
c) 1/1000
d) 1/27
ဂဏန့််းအ ခ
ွေြဲ ြါင့််း ဘယ့်နှစ့်လို်းရမလြဲဆိို ွေက့်ခြ်းြါ။
44 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
10. လိုြ့်ငန့််း ွေင့်မှ လူ 15 ခယ က့်ရှိြါ ယ့်။ သူ ိုို့ိထြဲက 5 ခယ က့်ကိိုအ ွေြဲှေ့ ှေ့ွေြဲ ပြ်း အလိုြ့် စ့် ိုလိုြ့် ို့ိို
Chapter အနှစခချုပ
အ န့််းအကျယ့်ကက်း စ့် န့််းထြဲ ိုခ ွေ န့််းစပြ်း ထည့်မယ့်။ ဘယ့်နှစ့် ို ဆလြဲသိ ျင့် ယ့်။
သ ္ျ မရ ြဲသူက ဘယ့်နှစ့် ိုဆသလြဲသိရခအ င့် ိုခ ွေအမျ ်းကက်းကိို အ ျိန့်ကိုန့် ၊လူြင့်ြန့််း ခရှေ့ပြ်း
င
ိို ့််း ယ့်၊ စ့် ိုနြဲို့ စ့် ိုကက ်း ဘယ့်ခလ က့်ခနရ ပ ်းမလြဲ င
ိို ့််း ယ့်။ ပြ်းခ စ ရက့်ခြေါ်မှ
ျ က
ွေ ့်လိိုက့် ယ့်။ အခပ န့််းထွေက့်လ ယ့်။ ိုခ ွေ ခရှေ့ကကည့်စရ မလိိုခ ဘူ်း။
မှ ်း ့်သလိို Program စ့် ိုကိို ခရ်းသ ်း ြဲအ ါမှ ထြ့် ါ ထြ့် ါ လိုြ့် မျိြု်းခ ွေက
ွေက့် ျက့် ြဲ အ ျိန့် ကက ခစြါ ယ့်။ Formula နြဲို့ ွေက့် က ပမန့်ခစြါ ယ့်။ ဒါခကက င့် ထြ့် ါ
ဒါခကက င့် Formula ခလ်းခ ွေ သိထ ်းသင့်ြါ ယ့်။ ခပြ ပြ်းသ ်း ထြ့်ခပြ ရရင့် စ အိုြထ
့် ြဲ
ျ က
ွေ ့်ကကည့်ြါခနော့်။ ပြ်းခ ထြ့် ါ ထြ့် ါ ခြါင့််း က ခပမ က့် နြဲို့ ူပြ်း၊ ထြ့် ါ ထြ့် ါ
45 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သ ျင့််းခလ်းက ွေင့်ထ ်း
သိြမ
့် က
ိို ့် ြြဲ”
46 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၄)
Set Theory
န ာက်ကပတ
ို ွင် Set A မှာ ပါတာက 1,3,5၊ Set B မှာ ပါတာ 6၊ ဒီနတာ တူတာ မရှပါဘူီး။ ဒါန ကာင်
ခပထာီးတဲပမ
ို ှာ Set A ေဲဲ့ Set B မှာ 5 ဆိုတာန ီး ဘိုပါနေတာ ခြစ်တဲ တွက် Overlapping
ခြစ်နေတာပါ။
47 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဆိုတဲ ဓပပါယ်ပါ။
▪ Interset (∩) ဆိုတာ and ေဲဲ့ တူပါတယ်။ and ဆိုတာ ာီး ိုီးမှာ ပါရမယ် ဘိုတူတဟာ
ဲ ဆိုတဲ
ဓပပါယ်ပါ။
48 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Probability
Probability Example:
ြဲချပ် Cards နတွဟာ probability တွက် နကာင်ီးဆိုီး ေမူေ ခြစ် ိုဲ့ ဒီမှာ ဲ ဲဒါေဲပ
ဲ့ ဲ
ပမ
ို ှာ row ိုက် ကည် ကည် ိုက်ရင် ြဲထိုတ် တစ်ထိုတ်မှာ Clubs 13 ချပ်၊ Diamonds 13
ချပ်၊ Hearts 13 ချပ် ေဲဲ့ Spades 13 ချပ် ဆိုနတာ စိုစိုနပါင်ီး ြဲချပ်နပါင်ီး 52 ချပ်ရှပါတယ်။
Row ေဲဲ့ column တွဲ ကည် ိုက်ရင် Clubs 13 ချပ်မှာမှ Ace ပါတာ 1 ချပ်၊ 2,3,4,5,6,7,8,9,10
ပါတာ 1 ချပ်စီ၊ Jack က 1 ချပ်၊ Queen က 1 ချပ် ေဲဲ့ King က 1 ချပ် တိုဲ့ ခြစ်ပါတယ်။ Diamonds,
ပပီီးနတာ ပမ
ို ှာ ကည် ိုက်ရင် သသာတာက Clubs နှင် spades ဟာ ေက်နရာင်နတွခြစ်ပပီီး၊
49 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Probability Formulas
စာ်းရတာ ေါ။
𝑃(𝐴) + 𝑃(À) = 1
𝑃(À) = 1 − 𝑃(𝐴)
1, 2, 3, 4, 5, 6 ဆိုပပီီး 6 မျက်နှ ရှတဲ ေ်စာတီးို တစ်တိုီးကို ပစ် ိုက် ိုဲ့ 2 ဂဏေ်ီး ကျနင
ို ်တဲ
50 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သမ
ိုဲ့ ဟိုတ် ၆) ကျနင
ို ်တဲ ရာခိုင်နှုေ်ီး 5/6*100 = 83.33% ခြစ်ပါတယ်။
16.67% + 83.33% = 100% ခြစ်ပါတယ်။ ရာခိုင်နှုေ်ီး ာီး ိုီးနပါင်ီးရင် 100 ပါပဲ။ 100
ထက်မနကျာ်ပါဘူီး။
P (‘2’ of spades) =?
No of ‘2’ of spades =1
( နပေါ်ကပက
ို ိုသွာီး ကည်ပါ spades row ထဲမှာ 2 ခြစ်တာ 1 ခိုပဲရပါတယ်
ှ )
51 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
12 52 − 12 40 10
𝑃(𝑎 𝑛𝑜𝑛_𝑓𝑎𝑐𝑒 𝑐𝑎𝑟𝑑) = 1 − = = =
52 52 52 13
Or logic in Probability
P (A or B) or P(A∪B) = P(A)+P(B)
(မမှတမ
် ရင် နပေါ်မှာ နခပာခဲတဲ ပက
ို ိုခပေ် ကည်ပါ။ King ထဲမှာ Queen ပါနေတာ၊ Queen
ပန
ို သေည်ီးကို သိုီးပပီီး တွက်ရပါတယ်။)
52 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ပန
ို သေည်ီးေဲဲ့ တွက်ပါမယ်။)
P(spades ) = 13/52
P(jack)= 4/52
P(A ∩ B)
P(A⁄𝐵 ) = , P(A ∩ B) = P(A/B) * P(B)
P(B)
P(A ∩ B)
P(B⁄𝐴) = , P(A ∩ B) = P(B|A) * P(A)
P(A)
53 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ခပေ်မထညပ
် ါဘူီး။ ပထမတစ်ကဒ်က Club ခြစ်ပပီီး၊ ဒိုတယ တစ်ကဒ်က spade ခြစ်တဲ probability ကို
တွက်ပါ။
P(A) = 13/52
ိုန
ဲ့ ခပာတဲ တွက် dependent formula ေဲဲ့ တွက်ပါမယ်။ တကဒ်ယူပပီီးသာီးခြစ် ိုဲ့ 51 ကဒ်ပဲ
13 13 169
= ∗ =
52 51 2652
ခပေ်ထည်ပါမယ်။ ပထမတစ်ကဒ်က Club ခြစ်ပပီီး၊ ဒိုတယ တစ်ကဒ်က spade ခြစ်တဲ probability ကို
တွက်ပါ။
Let Event (A) = the first card is Club, Event (B) = the second card is spade
P(A) = 13/52
54 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
13 13 1 1 1
= ∗ = ∗ =
52 52 4 4 16
Mean (μ)
နရ တွက်ေဲဲ့ စာီးတာနပါ။
μ = ΣX / N
ΣX = 10 + 20 + 30 = 60, N = 3
μ = 60/3 = 20
Medium
စာီးနပီးရမှာ ခြစ်ပါတယ်။
55 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
σ = √σ 2
(𝑥𝑖 − μ)2
𝑣𝑎𝑟𝑖𝑎𝑛𝑐𝑒 = σ2 = Σ
𝑁
1, 3, 5, 7
ΣX = 1 + 3 + 5 + 7= 16, N = 4
μ = 16/4 = 4
(𝑥𝑖 − μ)2
σ2 = Σ
𝑁
56 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
9+ 1+1+9 20
σ2 = = =5
4 4
σ = √5 = 2.2
1, 3, 9, 11
ΣX = 1 + 3 + 9 + 11= 24, N = 4
μ = 24/4 = 6
(𝑥𝑖 − μ)2
σ2 = Σ
𝑁
25 + 9 + 9 + 25 68
σ2 = = = 17
4 4
σ = √17 = 4.1
Range
ခြစ်ပါတယ်။
1, 3, 4, 5, 5, 6, 7, 11, Range = 11 – 1 = 10
57 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1, 3, 5, 7, 9, 11, 13, 15
Q1 = (3+5)/ 2 = 4
Q2 = (7+9)/2 = 8
Q3 = (11+13)/2 = 12
Q2 က 8, Q3 က 12 ရ ိုဲ့ တတယ ပင
ို ်ီး => 9,11 နှင် စတိုထထ ပင
ို ်ီး => 13,15 ဆိုပပီီး ညီ မျှ
4 ပင
ို ်ီး ပင
ို ်ီးနပီးသွာီးတာ ခြစ်ပါတယ်။
58 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1. A = {2, 4, 6, 8, 10, 12, 14, 15, 16, 18, 20}, B ={0, 5, 10, 15, 20}. Find A U B, A ∩ B, A – B
and B-A.
နရီးချယ်မှာခြစ်ပါတယ်။
ရှာနပီးပါ။
ခပေ်မထည်ပါဘူီး။ ပထမတစ်ကဒ်က Red Face ခြစ်ပပီီး၊ ဒိုတယ တစ်ကဒ်က Black Face ခြစ်တဲ
ကို တွက်ပါ။
59 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
မနေပါေဲန
ဲ့ ေ ်။ နရှဲ့သင်ခေ်ီးစာက summation Formula နတွကို သိုီးချပါ။
10. 2, 4, 6, 8, 10, 12, 14, 16, 18, 10 ရဲဲ့ Medium နှင် Standard deviation ကို ရှာနပီးပါ။
11. 1, 5, 30, 45, 60, 770, 888, 990 ကေ်ီးစဉ်တေ်ီးရဲဲ့ Range ကိုရှာပါ။ ပပီီး ျှင် IQR ကိုသိုီးပပီီး 4
ပင
ို ်ီးပိုင်ီးနပီးပါ။
Chapter အနှစခ ျု
ထာီးန ရှတယ်။ ူေ နတွ ရမ်ီးမျာီးတဲ တွက် ချက် က်နတွက ည်ီး မျာီးမှာ မှေပ
် ါပဲ။
တွက်ချက်မတ
ှု စ်ခိုခို ိုပ်ပပီီး ဘာနရာဂါ ကခဏာနတွ ရှရင် ဘာနရာဂါ ိုဲ့ ထိုတ်ခပရင်နတာ ကျွေ်မတဲ့ို
ိုဲ့နခေါ်ပါတယ်။
60 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
data နခေါ်ပပီီး၊ ဲဒီ data နပေါ်မှာ တွက်ချက်မှု ိုပ်ပပီီး မျာီးေ ီး ည်တဲ သိုီးဝင်တဲ ပစ
ို ေဲဲ့
ပဲခြစ်ပါတယ်။ ထို တူပဲ ထွက် ာတဲ information ကို တေ်ီးပပီီး နပီးသိုီး ိုဲ့မရပါဘူီး၊ ဲဒီ
တွက်ချက်တနေရာနတွ
ဲ မာှ ွေ် သိုီးခပ ပါတယ်။
ြတ်ရိုေန
ဲဲ့ တာ န ာ် င်ီး ဆိုပပီီး ပပီီးသွာီးမှာပါ။ ဘာမှ မှတ်မမှာ မဟိုတပ
် ါဘူီး။
61 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၅)
Data Structure ဆတ
ို ာဘာလဲ။
လူတ ွေ စ်တ ောက်နဲ့ စ်တ ောက် တ ောဆိုဆက်ဆံ ဲ့အခါမော ြစ် ြစ်၊ စောတ ေးသောေးပ ေး
ဆက်သွေ ် ဲ့အခါမော ြစ် ြစ် မန်မောလူမ ေးအခ င်ေးခ င်ေးက မန်မောလို တ ေးသောေးတ ောဆိုဆက်ဆံမ ်။
ထိုင်ေးလူမ ေးအခ င်ေးခ င်ေးက ထိုင်ေးလို တ ေးသောေးတ ောဆို ဆက်ဆံမ ်။ အဒလို အခ င်ေးခ င်ေး
မော မန်မော၊ ထိုင်ေး စသည် ြငဲ့် မ ောေးစွေော သလို programming language တ ွေလမ ောေးစွေော ါ ်။
2. အဒ data တ ွေကတန ကို ်လိုခ င် ဲ့အတ ြကို ြိုဲ့ ဘ ်လို algorithm တ ွေနဲ့ အလို ်လို ်လိုဲ့
( ွေက်ခ က်လို)ဲ့ မလ
62 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Data တ ွေကို သမ်ေး ဲ့အခါ လိုအ ်ခ က်အ န်ြိုေး စ်ခိုထသမ်ေးခ င် ောမ ေး သလို၊
န်ြိုေးတ အ
ွေ လ
ွေ ိုက်သမ်ေးခ င် ောမ ေးလည်ေး ါ ်။
လက် အသက် ွ ဆ
် ို ော စ်ခို ည်ေး ါ ်။ ဒလို စ်ခိုထ ဲ့ န်ြေးို ကို သမ်ေးခ င် ်
ဆို င်တ ောဲ့ primitive data types တ ွေထက စ်ခို ကို အသံိုေး ါ ်။ Primitive data type ဆို ဲ့
တမွေေးခ င်ေးတမောင်နမတ ွေကိုသမ်ေးခ င် ် ဆို င်တ ောဲ့ စ်ခ ဲ့က တမောင်နမ မ ဘူေး၊ စ်ခ ဲ့က
အမ ောေးကကေး သမ်ေး ဲ့ စ
ံို ံမ ေးနဲ့ သမ်ေး ါလမဲ့်မ ။် ဒလို အ လ
ွေ ိုက်အမ ောေးကကေး သမ်ေးခ င် င်တ ောဲ့ array,
lists ဲ့အ
ို င် အ လ
ွေ ိုက်သမ်ေးလိုဲ့ ဲ့ဟောတ ွေ တနောက်ထ ် အမ ောေးကကေး ါတသေး ်။
Primitive data type ြစ် ြစ် array, stack, queue, lists ဲ့ို ြစ် ြစ် စ်ခိုခ င်ေးစမော
အောေးနည်ေးခ က်၊ အောေးသောခ က်တ ွေ ါ ်။ အောေးနည်ေးခ က်၊ အောေးသောခ က်တ ေါ်မူ ည်ပ ေး ဘ ်လို
တလဲ့လော ော ြစ် ါ ်။ စ်ခိုခ င်ေးစ ဲ့ အောေးနည်ေးခ က် အောေးသောခ က်တ ွေကို သထောေး ခင်ေးအောေး ြငဲ့်
63 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ကို တ
် ေးတန ဲ့ code ဲ့ လိုအ ်ခ က် အတ ခတနတ ေါ်မူ ည်ပ ေး သံိုေးစွေ သ
် ွေောေး င် code
တ ေး အ
ဲ့ ခါမော အ မ်ေးသက်သော လွေ ်ကူမော ြစ် ါ ်။
အလို ် စ်ခိုပ ေးတ မောက်ြိုဲ့ စ်ဆငဲ့်ပ ေး စ်ဆငဲ့်အလို ်လို ် ံို နည်ေးအောေး ြငဲ့် ဘောပ ေး င်
၄-မတ ောဲ့၊ မမော အတနတ ော် ထမင်ေး တအောင် တဆေးပ ေးသောေးဆန်နငဲ့် သငဲ့်တ ော် ဲ့တ မောဏကို
64 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
၆ - မေးခလို ြ
် ွေငဲ့် ါ။
၈- ထမင်ေး သံိုေးတဆောင်လိုဲ့ ါပ
ောကကညဲ့်ကကညဲ့် ါ)
တက ောင်ေး က် န
ို ်ေးက သူင ်ခ င်ေးနစ်တ ောက်နဲ့ အတဆောင်မော အ ူတန ါ ်။ စ် က်မော
အဆင်သငဲ့် ထညဲ့်တ ေးခဲ့ ်။ ပ ေးတ ောဲ့ တနောက်က မ ခ က်တလ မနက် ူ ူစောေး တလမိုဲ့ သူင ်ခ င်ေးကို
ထိုေးလိုက်တနော်” လိုဲ့မောခဲ့ ါ ်။
မနက်ထမင်ေးစောေးမ လ
် ို ်တ ောဲ့ တ ါင်ေးအိုေးထမော ထမင်ေးအစောေး မခ က် တသေး ဲ့ ဆန်
အတစဲ့လိုကတ
် ွေ တ ွေဲ့ ါ ်။ အဒတ ောဲ့မ သူင ်ခ င်ေးက “ လ ်တ ါက်တ ောဲ့ထိုေး ် မေးခလို ်
ြွေငဲ့်ြိုဲ့ တမဲ့တန ောဟ” ဲ့တလ။ က မေးခလို ်ြွေငဲ့်ဆို ဲ့ အဆင်ဲ့ စ်ဆငဲ့် တမဲ့က န်ခဲ့ ောနဲ့
စောေးလိုဲ့ ထ
ဲ့ မင်ေးဆို ဲ့ လိုခ င် ဲ့ result မ တ ောဲ့ ါဘူေး။
65 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒါတ က ငဲ့ မှတထ ်း မှ က algorithm ဆိိုတ က step by step procedure လိိုို့တပပ ပါတယ။
ဒနစ်ခိုဟော သြလ
ိုဲ့ ို ဲ့ လိုအ ်ခ က် ြစ်ပ ေး၊ ဆန်၊ တ ၊ ထမင်ေးတ ါင်ေးအိုေး၊ လျှ ်စစ်မေး ကတ ောဲ့
ြလ
ိုဲ့ ို လ
ဲ့ ိုအ ်ခ က် ြစ် ါ ်။ ြိုဲ့ လို ဲ့ လိုအ ်ခ က် တနမ အဒ algorithm ကို သံိုေးသငဲ့် ါ ်။
သြလ
ိုဲ့ ို ဲ့ လိုအ ်ခ က်ကိုတ ောဲ့ input လိုဲ့တခေါ် ါ ်။ စောေးလိုဲ့ တသော ထမင်ေး ဆို ောကတ ောဲ့
၁ - မေးတမေးထောေး ါ
၅ - ခ က်မညဲ့အ
် ိုေးထသိုဲ့ တဆေးပ ေးသောေးဆန်ထညဲ့် ါ၊ မေးြတ
ို ေါ်အိုေးကို င်ထောေးလိုက် ါ။
66 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ငဲ့ စ်မောမဲ့ို တ ကိုအ မ်ေးကကေး ွေက်ခ က်ထညဲ့်တနစ ောမလို ါဘူေး၊ ကက က်သတလောက် ထညဲ့်လိုဲ့
ါ )်
ညဲ့်တအောင် လညဲ့်လညဲ့်ပ ေး အ ူတ ေး ါ။
၈- ထမင်ေး သံိုေးတဆောင်လိုဲ့ ါပ
ဒနစ်ခိုဟော သြလ
ိုဲ့ ို ဲ့ လိုအ ်ခ က် ြစ်ပ ေး ထမင်ေးခ က်ြဲ့ိုအိုေး၊ မေးတမေးြဲ့ို ထင်ေး သမ
ိုဲ့ ဟို ် မေးတသွေေး ကတ ောဲ့
ြလ
ိုဲ့ ို လ
ဲ့ ိုအ ်ခ က် ြစ် ါ ်။ ဒါတကကောငဲ့် အဒ ြိုဲ့လို ဲ့ လိုအ ်ခ က်တ ွေမ င် ဒ algorithm ကို
စောေးနိုင် ဲ့ မောဏတ ွေကတ ောဲ့ input ြစ်ပ ေး စောေးလိုဲ့ တသော ထမင်ေး ဆို ောကတ ောဲ့ လိုခ င် ဲ့ result
67 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ လူသက်သော ်။
မော ော ြစ်နင
ို ် ်။
▪ ထမင်ေးတ မငဲ့ အ
ဲ့ ွေက် တ ခန်ေးခ က် ော ြစ် အ
ဲ့ ွေက် စောသံိုေးမညဲ့်ထမင်ေးထမော အဟော
ညဲ့်ဝ စွေော ်။
▪ ဆန်နဲ့ သ မ
် ညဲ့်တသော ဆေးခ သမောေးတ ွေ အ ွေက်တ ောဲ့ တ ါင်ေးအိုေးနဲ့ခ က် ောဟော
ဒိုတယ
ိ တစခိုက
အခ န်တ ေးြဲ့လ
ို ို ါ ။်
ထညဲ့်လိုဲ့ ါ ်။
▪ လျှ ်စစ်မေး ြမ
ိုဲ့ လိုဘူေး။
▪ ငထ
ဲ့ ောေးတသောတ (ထမင်ေးတ ) ကို ဆောေးတလေး သကကောေးတလေးထညဲ့်ပ ေး သေးသနဲ့် တသောက်လိုဲ့ ်။
▪ ငထ
ဲ့ ောေး ဲ့ ထမင်ေးတ ကို ဟင်ေးခ ခ က် ဲ့တန ောမောလည်ေး သံိုေးလိုဲ့ ်။
68 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အထူေးတ ောခ င် ော စ်ခိုကတ ောဲ့ ဒို algorithm မော မေးတမေး ောကို ဆန်ကတ
ို ဆေး
ခ က်မညဲ့အ
် ိုေးထထညဲ့်ပ ေးမ လို ်လိုဲ့ တ မဲ့ ဆန် မောဏ မ ွေက် တသေးခင် က ည်ေးက
မေးအောေးတကောင်ေးလောတအောင် အခ န် ူြလ
ိုဲ့ ို ်။ မန် မန် က်ခ င် င်တ ောဲ့ က်ခ ်တ ေးြဲ့လ
ို ို ါ ်။
ဒတ ောဲ့ မေးကိုအ င်တမေးထောေးပ ေးတ ောဲ့မ ခ က် မညဲ့် ဆန် မောဏ ွေက်မ ၊် ဆန် ူမ ်၊
ထမင်းချကနည်းအမျိြု်းမျိြု်း သ
ှိ လိို၊ သခ္ျ ပိုစဆ တစပိုဒမှ တွေကနည်းအမျိြုမျိြု်း ှိသလိို၊ ကိစစ
ထမင်ေးခ က်ြဆ
ိုဲ့ ို လျှ ်စစ်မေးလိုအ ် ်။ ဒတ ောဲ့ လျှ ်စစ်မေးမ တသေး ဲ့ တဒသမော အဒ ခ က်နည်ေးကို
69 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သံိုေးလိုဲ့မ ဘူေး။ ဒါတကကောငဲ့် အောေးသောခ က်၊ ကကောခ န်၊ လိုအ ်ခ က် စ ောတ ွေအောေးလံိုေးကို သမသောလျှင်
ကို ဲ့အ
် တ ခတနဲ့ ဘ ် algorithm ကိုသံိုေး မလ ဆို ော တကောင်ေးမွေနစ
် ွေော ဆံိုေး ြ ်နိုင်မော ြစ် ါ ်။
Chapter အနှစချြုပ
ဘ ်လို ိုစ
ံ ံနသ
ဲ့ မ်ေး င်တကောင်ေးမလ၊ အဒ data တ ွေကို ွေက်ခ က် ဲ့အခါမော ဘ ် algorithm
နဲ့ ွေက်သင်ဲ့သလဆို ော သြိုဲ့ data သမ်ေးဆည်ေးနည်ေးတ ွေနဲ့ data တ ွေကို ွေက်ခ က်နည်ေး
အဆငဲ့်ဆငဲ့် step by step procedure လိုဲ့တခေါ် ဲ့ algorithm ကို တလဲ့လော ောကို Data
structure လိုဲ့ တခေါ် ါ ်။ ဒတလောက်ဆို င် data structure ဆို ော ဘောလ သတလောက်ပ လဲ့ို
ထင် ါ ်။
▪ တနောက်ပ ေး algorithm ဲ့ ြလ
ိုဲ့ ိုအ ်ခ က်တ ွေ ကို ဲ့်မော တနမ သံိုေးလိုဲ့ မ ်။ သြဲ့လ
ို ို ဲ့
လိုအ ်ခ က် အောေးလံိုေးကို input လိုဲ့တခေါ်ပ ေး၊ လိုခ င် ဲ့ အတ ြ result ကိုတ ောဲ့ output၊
လို ်နည်ေး၊ ွေက်ခ က်နည်ေး ကိုတ ောဲ့ process လိုဲ့တခေါ် ါ ်။ ဒါတကကောငဲ့် algorithm စ်ခိုမော
1. input ၊
2. Process
3. output ဆိုပ ေး အ င
ို ်ေး ၃ ိုင်ေး ါ ါ ဲ့မ ်။
70 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
71 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၆)
Algorithm မိတဆက
Variables
Algorithm အက ကြောင်းကို မက ြောက ်းခင Variable ကို အရင က ြော ါ့မယ။ Variable ဆိိုတာ
ဆိုတြော ၅ ကို လကမြော မ်းလိုကတြော၊ အဒကတြောါ့ မ်းြေဲ့ို ကနရြော ၂ ကနရြောလိုတြောက ါ့၊ computer
မ်းြေဲ့က
ို နရြော ကနရြော ၃ကနရြော လို ွေြော်းတြော ဆိုကတြောါ့ variable ၃ ခို လိုမြောက ါ့။ ဒ ဆို variable ဆိုတြော
72 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ကဘြောငအတွေင်းမြော ြေစရ မယ။ Language တစခိုနဲ့ တစခို variable name မြော ဘြောကတွေ လိုဲ့ရ လ
ဆိုလိုတြောက variable name မြော a to z (အက ်း၊ အကက်း) ရယ၊ 0 to 9 ရယ၊ underscore
ရယ လရ
ိုဲ့ တယ။ အဲဒီ သူခွငဲ့ပ ြုထာ်းတဲဲ့ ၃ ခိုက ွဲပ ီ်း ကျနတဲဲ့ space ပခာ်းတာတိို့ို၊
ပ ါင်းနိုတပပမာကစာ်း၊ #, ! စတဲဲ့ special symbol တိိုို့ တစခိုမ ါ ို့ိိုမရ ါဘူ်း။ ပ ီ်းပတာဲ့ ါ ို့ိိုရတဲဲ့ ၃
ခိုထဲက digit နဲို့ စ ိိုို့ မရ ါဘူ်း။ letter သိိုို့မဟိုတ underscore (_) နဲို့ စပ ီ်းရငပတာဲ့ ပနောကက digit,
num1 (မန n ဆိုတါ့ letter နဲ့စပ ်း၊ ကနောကကလည်း letter နဲ့ digit လိုဲ့)
_year1 (မန - underscore နငါ့ စပ ်း၊ ကနောကက letter နဲ့ digit လိုက)
Algorithm ဆိိုတာ
73 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အြော်း ြောခခက၊ အြော်းနည်းခခကကို ကလါ့လြောပ ်း မမ အဆငက တြောကို ကရ်းခခယရ မယ။ ဒါပ ကာငဲ့
က ြောခါ့ ပ ်း ွေြော်း ပ ။
ပပ ာခဲဲ့ပ ီ်း ါပ ီ။
ဆို ကတြောါ့။ ဒ ဆို ကျွနမတဲ့ို ူကို ဘြော နကမ်း ကမလ။ အ ကမ်းြေခင်းတွေကမယဆိုရင ကမွေ်းတါ့
ပဖစတဲဲ့အတွက input ပဖစ ါတယ။ ပ ်းကတြော ဘယလိုတွေကမလ။ ယခို လကရ ခိုနစထကကန ကမွေ်းတါ့
ခိုနစကို နှုတပ ်း တွေကမြောက ါ့။ ဒ ဟြော တွေကခခကတြော ြေစလိုဲ့ process ။ နှုတ ိိုို့ရ ာတဲဲ့
74 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
(တွက ိိုို့ရ ာတဲ)ဲ့ အပပဖကိို user ဆီ ပ နပပ ာတာက output ါ။ ဒ ဆို input, output, process ကို
ကဘြောက ကကလြောက ပ ။
ပ ်းကတြောါ့ ဒမြော input လကခမြောက တစခို - ကမွေ်းတါ့ခိုနစ၊ output ထိုတက ်းရမြောက တစခို -
အ က ဆိုကတြောါ့ input တစခို၊ output တစခို၊ computer အကနနဲ့ က ြောရရင စိုစိုက င်း variable
နစခိုလို ါ့မယ။
2. Read birth_year.
5. Exit.
1. အရငဆို်း user ကို အ ကရိုကထညါ့ ဆိုပ ်း Enter your age လိုဲ့ user ကို က ြောခခငတြော၊ User
မငကအြောင ထိုတ ခခငတြော ြေစလိုဲ့ write ဆိုပ ်း output ထိုတ လိုက တယ။
3. ယခို ခိုနစက 2021 ြေစလိုဲ့ 2021 ထက user ရိုကထညါ့လိုကတါ့ တနြေို်း birth_year ကို နိုတပ ်း
75 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
4. တွေကလရ
ိုဲ့ လြောတါ့ အက ြေကို ကို စ
ို တစခိုနဲ့ ထိုတ ခခင တယ၊ ဥ မြော တွေကလရ
ိုဲ့ လြောတါ့ တနြေို်းက
20 ဆိုရင Your age is 20 လိုဲ့က ေါ်ခခင တယ။ output ထိုတ မြောမဲ့ို Write, Your age is ဆိုတါ့
စြော ြော်း တက
ို ရိုကက ေါ်ခခငတြော ြေစလိုဲ့ single quote အတွေင်းမြော ကရ်း တယ။ double quote ထ
တက
ို ရိုကက ေါ်ခခငတြော မဟိုတဘူ်း၊ age ထက မ်းထြော်းတါ့ တနြေို်း က ေါ်ခခငတြောမိုဲ့ single quote,
5. User လိုခခငတါ့ အက ြေ ထိုတ ပ ်းပ ြေစလိုဲ့ program ကို ရ လိုကတြောက exit ြေစ တယ။
အက ြေရလြောကတြောါ့ အက ြေကို တစခ တည်း output ထိုတ တြော ြေစ တယ။ တစခ တည်း output
ထိုတ လိုကကတြောါ့ သူထိုတပ တဲဲ့အတိိုင်း ပမငရ ါမယ။ user က ဘာမ ဆက ို ိိုို့ မရ ါဘူ်း။
တကယလိုဲ့ user ကို စတကက ကလို ခွေငါ့က ်းခခငရင return နက ်းြေဲ့ိုလို တယ။ return
အပပဖကိို သူကကိြုကတဲဲ့ စ
ို နဲို့ output ထိုတ ိိုို့ရသ ိို၊ အဲဒီရ ာတဲဲ့ အပပဖပ ေါ်မာ မူတညပ ီ်း ဆကပ ီ်း
ကနောကတစခိုက အ င
ို ်းကလ်း ခွေကရ်းတြောကို function လိုဲ့ကခေါ် တယ။ function ခွေကရ်း ခင်းအြော်း ြေငါ့
တစ ခြော်းကနရြောကတွေကကန ကခေါ်ပ ်း အ ို်း လိုဲ့ ရ ွေြော်းမြော ြေစ တယ။ ဒ က ကြောငါ့ function ကလ်းကတွေ
76 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ကညါ့ ။
ageCalculator(birth_year)
return age
function ကရ်း က
ို ရ်းနည်း ကို နရင်းရမယဆိုရငကတြောါ့ ကအြောကမြော ကြေေါ် ထြော်းတါ့ အ
ို တင
ို ်း
ကရ်းရ တယ။
functionName(parameter)
Body
က ်းလိုဲ့ ရ တယ။ ကနောကတစခို ကစခခငတြောက variable name နဲ့ function name လိုမခ ်း
Parameter – အလွေယက ြောရင input ။ တစခိုထက ိုခါ့ရငကတြောါ့ တစခိုနဲ့ တစခို ကြော်း comma (,)
Body - ကကတြောါ့ တွေကတြော ခခကတြော၊ အက ြေထွေကလြောရင return နတြောကတွေ လို မြော ြေစ တယ။
77 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အက ေါ်က function ကို ွေြော်း အလို လို ပ ်းကတြောါ့ လက ည်းကွေင်းထမြော ထညါ့က ်းလိုကတါ့ 2000 ဟြော
ကကန တွေကခခကပ ်း return နက ်းထြော်းတါ့ အက ြေကို ans ထထညါ့မယလိုဲ့ က ြောတြော ြေစ တယ။ ans
birth_year ထကို 2002 ကရြောက ွေြော်းမြော ြေစပ ်း၊ 2021 ထက 2002 နိုတလရ
ိုဲ့ တါ့ အက ြေကို function က
ဝင ွေြော်းမြော ြေစ တယ။ ဒါဆိို function က return ပ နရင မ်းပခေါ်သို်းတဲဲ့ ပနရာကပနပ ီ်း အပပဖ
အစအဆို်း စ
ို တစခို၊ function စ
ို တစခို၊ စိုစိုက င်း နစမခ ်းနဲ့ ကိုယဟြောက
ါ့ ိုယ
မ တွေကလမ
ိုဲ့ ရ ဘူ်း။ ဒ က ကြောငါ့ user ဆ ကမ်းရမြောက အလခြော်း နဲ့ အန၊ တွေကက ်းရမြောက area ။
ဒကလြောကဆို ကရ်းတတကလြောက ပ ။
78 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Chapter အနစချြု
ဒ ဆို ူတိုဲ့ ဘိုနော်းလညတြော ဘြောလ ဆိုကတြောါ့ algorithm ကတွေ၊ pseudocode ကတွေ၊ Flowchart
ကတွေ ြေစ တယ။ algorithm ဆိုတြော programmer ကတွေ အြော်းလို်းနော်းလည တယ။ Algorithm
က ြောင်းကရ်းမြောက ါ့ကလ။
79 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၇)
ြစ်ပါတယ်။
ေမောအဆေေို့ မိုေ်သ
ို့ ွောျိုးစာျိုးမယ်ဆိုပါဆတာ။ ဘာစာျိုးမလ အရင်စဉ်ျိုးစာျိုးရမယ်၊ ပပီျိုးရင်
ပက
ို ်ဆံအတ်က အပမတမ်ျိုး ပေ်ယမာလာျိုး ဆိုဆတာ မဟိုတပ
် ါဘျိုး။ ဆမက ေ်ခမ ပေ်ယမာပါ။ အဒီလိုို့
ြစ်မ လိုပ်မာ၊ မ ြစ်ရင် မလိုပ်ဘျိုး ဆိုတ အဆ ခဆေမ ျိုးကို conditional or selection statement လိုို့
ဆခေါ်ပါတယ်။
true false
ပိုကဆ
် ံအတ်
က ေ်ဆေခသလာျိုး။
80 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒီပိုဆ
ံ လျိုးကို flowchart လိုို့ဆခေါ်ပါတယ်။ ကကည်လိုက်ရင် ရင်ျိုးသွောျိုးမာပါ။ ပက
ို ်ဆံအတ်
ပက
ို ်ဆံအတ် ဆမက ေ်ခတာ ြစ်တအတွေက် “ ပေ်ယမယ်” ဆိုတ အလိုပ် ကို လိုပ်ပါတယ်။ false
အလိုပလ
် ိုပ်ဆဆာင်တာဆတွေ ြစ်တအတွေက် process ြစ်ပါတယ်။ process ကို flowchart မာ
true false
ပိုကဆ
် ံအတ်
ပါခသလာျိုး။
ဒီပိုက
ံ အဆပေါ်ကပံိုကို ဆောက်တစ်မ ျိုးေို့ ပေ်ဆွေထာျိုးတာ ြစ်ပါတယ်။ အဆပေါ်က ပံိုကဆတာ
“ပက
ို ်ဆံအတ်ဆမက ေ်ဆေခသလာျိုး” ဆိုတာကို condition စစ်ထာျိုးတာ ြစ်ပပီျိုး၊ ဒီပိုံကဆတာ
“ပက
ို ်ဆံအတ်ပါခသလာျိုး” ဆိုတ ရူ့ဆထာင်ကဆေ condition စစ်ထာျိုးတာ ြစ်ပါတယ်။ ဒါဆကကာင်
ဒီပိုမ
ံ ာဆတာ true ဆိုတာ ပက
ို ်ဆံအတ် ပါခတယ် ဆိုတ အဓပပါယ် ြစ်တအတွေက် “ဆိုင်ကို
ကိုယက် က က်တ ရူ့ဆထာင်ကဆေ စစ်လိုို့ရတယ် ဆိုတာ ေို့ true နင် false ထွေက်တာဆတွေကို
သဆဘာဆပါက်ဆလာက်ပပီ ထင်ပါတယ်။
81 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အဒီ ပံန
ို စ်ပိုက
ံ ို algorithm အဆေေို့ ဆရျိုးကကည်ပါမယ်။ ပထမပံိုဆိုရင်
ပေ်ယမယ်။
Else:
ဆိုင်ကို ဆက်သွောျိုးမယ်။
ဒိုတယပံိုဆိုရင်
If ပိုက်ဆံအတ်ပါခသလာျိုး then:
ဆိုင်ကို ဆက်သွောျိုးမယ်။
Else:
ပေ်ယမယ်။
စာဆမျိုးပတစ်
ွေ ခိုမာ Myanmar, English, Math ဆိုတ ဘာသာရပ် 3 ခိုဆ ြရတယ်။
သံိုျိုးဘာသာလံိုျိုးဆအာင်ရင် စာဆမျိုးပဆအာင်
ွေ မယ်၊ တစ်ဘာသာက တာေို့ စာဆမျိုးပွေ က မယ်။
ဆအာင်မတ်က 40 ြစ်ပါတယ်။
Else:
82 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဆရျိုးကကည်ကကည်ပါမယ်။ ောျိုးလည်မလ
ှု ွေပပီျိုး တစ်ခိုခိုဆို ၂မ ျိုးဆရျိုးဆပျိုးရတယ် ထင်မဆေပါေအ
ို့ ံိုျိုး။ တစ်မ ျိုးပ
ငယ်ရင် က မာ ြစ်ပါတယ်။
Else:
စာဆမျိုးပတစ်
ွေ ခိုမာ Myanmar, English, Math ဆိုတ ဘာသာရပ် ၃ ခို ဆ ြရတယ် ဆိုပါဆတာ။
အဒီမာ က တသကို Fail လို ထိုတ် ပခ င်တယ်။ အေည်ျိုးဆံိုျိုး ၁ ဘာသာ ဂိုဏ်ထျိုးထွေက်ရင် with D လိုို့
ထိုတ် ပခ င်တယ်။ ဂိုဏ်ထျိုးမထွေက်ပ ရိုျိုးရိုျိုးဆအာင်တ သဆတွေကို Pass လိုို့ ထိုတ် ပခ င်တယ် ဆိုပါဆတာ။
83 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Output “Fail”
English>=75 or
Math>=75
Myanmar>=40 and English>=40 and Math>=40 ဆိုပပီျိုး and ေို့ စစ်ရပါမယ်။ Fail က
က တစ်ခို စစ်ရင် ရပါပပီ။ ဘာလိုို့လ ဆိုရင် မဆအာင်ရင် က ပပီျိုး၊ မက ရင် ဆအာင်လိုို့ ြစ်ပါတယ်။
84 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Flowchart မာ input, output ကို parallelogram ေို့ ဆွေပါတယ်။ တစ် ခာျိုး symbol သံိုျိုးတာလ
ရပါဆသျိုးတယ်။ ဒီပိုမ
ံ ာ ဆအာင်သလာျိုးဆိုတ ဘက် ခမ်ျိုးမာ မ condition ထပ်လာတာဆကကာင်၊
အဆေေို့ ဆရျိုးရင်ဆတာ
Write “With D”
Else:
Write “Pass”
Else:
Write “Fail”
ကကည်ကကည်ရဆအာင်ပါ။
Write “Fail”
Else:
Write “With D”
Else:
Write “Pass”
85 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Output “Fail”
true
false Myanmar >=75
or English>=75
or Math>=75
Output “With
Output “Pass”
D”
Nested အစ ်း နနောကတစနည်း
Write “Fail”
Write “With D”
Else:
Write “Pass”
86 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အလွေယ်မတ်လရ
ိုို့ တာက စစ်ရမာ 3ခိုေို့အထက်ဆိုရင် ဆရျို့ဆံိုျိုးတစ်ခိုက If ေို့ ဆရျိုး၊ ဆောက်ဆံိုျိုး
တစ်ခိုက Else ေို့ ဆရျိုး၊ အလယ်က က ေ်တာဆတွေက Else if ေို့ ဆရျိုး။ Else တစ်ခိုကလွေပပီျိုး If ဆရာ၊ Else
if ပါ condition လိုက်ပါတယ်။
num ဆိုတ ကေ်ျိုးဆလျိုးကို even positive လာျိုး၊ even negative လာျိုး၊ odd positive လာျိုး၊ odd
ဒါဆကကာင် ကေ်ျိုးတစ်လံိုျိုးဟာ အဒီ ၄ မ ျိုးထက တစ်မ ျိုးမ ျိုး ြစ်မာ ြစ်ပါတယ်။ ဆရျို့ဆံိုျိုးတစ်ခိုကို if ေို့
စစ်မယ်၊ ဆောက်ဆံိုျိုးတစ်ခိုကို else ေို့ စစ်မယ် ဆိုဆတာ အလယ်မာ ၂ ခိုက ေ်ပါတယ်။ အဒီ ၂ ခိုကဆတာ
else if ေို့ ဆရျိုးရမာ ြစ်ပါတယ်။ Else တစ်ခိုကလွေပပီျိုး က ေ်တ if ဆရာ၊ else if ဆတွေပါ condition ပါရမာ
ြစ်ပါတယ်။ even ဆိုတာ 2 ေို့ စာျိုး ပတ်တာ ြစ်ပပီျိုး၊ odd က 2 ေို့ စာျိုးလိုို့ မ ပတ်(အကကွေင်ျိုး 0 ေို့မညီတာ)၊
87 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Else:
အလိုပ်လိုပပ
် က
ံို
ဆကကာင် ြစ်ပါတယ်။ သဆ
ိုို့ သာ် ဆောက်ထပ် If တစ်ခို ထပ်လာခရင်ဆတာ အလိုပ်လိုပမ
် ာ
▪ If ဆောက်က condition မာျိုးတယ်ဆိုမ ပထမ else if ကို ဆရာက်လာပပီျိုး သူ့ရျို့ conditon ကို
စစ်မာ ြစ်ပါတယ်။ သူ့ condition မေ်ခ ရင် သူ့ဆအာက်က Write num, “ is even negative”
▪ ပထမ else If ဆောက်က condition မာျိုးတယ်ဆိုမ ဒိုတယ else if ကို ဆရာက်လာပပီျိုး သူ့ရျို့
conditon ကို စစ်မာ ြစ်ပါတယ်။ သူ့ condition မေ်ခ ရင် သူ့ဆအာက်က Write num, “ is
88 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခိုဆ ပာသွောျိုးတ even positive လာျိုး၊ even negative လာျိုး၊ odd positive လာျိုး၊ odd
ြင်ဆရျိုးခရင်
အလိုပ်လိုပတ
် ာ ြစ်လိုို့ လိုပ်သွောျိုးမာ ြစ်ပါတယ်။ အမေ်က 1 ေို့ 100 ကကာျိုးဆိုတ အဆ ခဆေဟာ >=1 နင်
<=100 ဆိုတ အဆ ခအဆေ နစ်ခိုလံိုျိုး မေ်မ လိုပ်ခ င်တာ ြစ်လိုို့ and ေို့ စစ်ရမာ ြစ်ပါတယ်။ ဒါဆကကာင်
89 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ရမတ် ၉၀ ေို့ အထက် ဆိုရင် grade A, ၈၉ -၇၀ ကကာျိုးဆို grade B, ၆၉ - ၄၀ ဆိုရင် grade C, ၄၀
If mark>=90 then:
Write “grade A”
Else If mark<90 and mark>=70 then:
Write “grade B”
Else If mark<70 and mark>=40 then:
Write “grade C”
Else:
Write “grade D”
တစ်ခိုေို့ တစ်ခို နိုင်ျိုးယဉ်ရင် ြစ်နင
ို ်ဆခ ၃ ခိုပရပါတယ်။ ငယ်တာရယ်၊ ညီတာရယ်၊
If mark>=90 then:
Write “grade A”
Else mark>=70 then:
Write “grade B”
Else If mark>=40 then:
90 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Write “grade C”
Else:
Write “grade D”
ဆိုပပီျိုး ြစ်သွောျိုးပါမယ်။ Condition တစ်ခိုဆလ ာသွောျိုးလိုို့ စစ်ရတာ အခ ေ်ကိုေ် သက်သာပါမယ်။
Flowchart Symbols
Off-page ပံဆ
ို ွေလိုို့ တစ်မ က်နာေို့ မဆံဆတာရင် တစ်မ က်နာေို့
Connector ဆောက် တစ်မ က်နာ ကို ခ တ်ဆက်ဆကကာင်ျိုး ပခ င်ရင်
သံိုျိုးပါတယ်။
variableName ပံစ
ို ံနင် ဆကကာ ငာဆပျိုးရပါတယ်၊ Declaration လိုပ်တယ်လိုို့ ဆခေါ်ပါတယ်။ ဒီမာဆတာ
91 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Myanmar, English, Math ဆိုတ variable ၃ ခို သံိုျိုးပါမယ်။ ၃ ခိုလံိုျိုးဟာ အမတ်သမ်ျိုးမာ ြစ်လိုို့ data
Declarations
Num Myanmar,English,Math
Output “Fail”
or English>=75
or Math>=75
End
92 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
န ေ့ကျငေ့ကကညေ့ရနအ င။
စာျိုး ပတ်လျှင် leap year ြစ်ပါတယ်။ စာျိုး ပတ်တယ်ဆိုတာ အကကွေင်ျိုး (mod တွေက်တာ 0) 0
ရတာ ြစ်ပပီျိုး စာျိုးမ ပတ်ဘျိုး ဆိုတာ အကကွေင်ျိုး 0 ေို့ မညီတာ ြစ်ပါတယ်၊ (== ညီတာ, != မညီတာ)
2000 => 400 ေို့ စာျိုး ပတ်လိုို့ 2000 is leap year လိုို့ ထိုတ်မယ်
1900 => 400 ေို့လည်ျိုး မ ပတ်ဘျိုး။ 4 ြင် ပတ်ပပီျိုး 100 ေို့ ပါစာျိုး ပတ်ဆေလိုို့ 1900 is not leap year
1904 => 4 ြင် ပတ်ပပီျိုး 100 ေို့ မ ပတ်လိုို့ 1904 is leap year
Looping or Iteration
93 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ Initialization - ဘယ်ကစမာလ၊
▪ Update - တစ်ကကမ်ပပီျိုးတင
ို ်ျိုး ဘာလိုပ်မာလ။
(<99) အတတပါပ။
infinite loop လိုို့ဆခေါ်ပါတယ်။ ဒါဆကကာင် infinite loop မဖြစရနအ င update မနမေ့ြုို့ ုပါတယ။
က စလိုက်ကကရဆအာင်။
94 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
N=1 Initialization
true false
N < 99
Condition
Output N, “,”
Update
N=N+2 Output N
ပံမ
ို ာ initialization, condition, update ကို မင်သာဆအာင် label တပ်ဆပျိုးထာျိုးပါတယ်။
မ ုကခင Flowchart ကု စ အုပထ ချဆထ ်းပါ၊ ပပ်းမ trace ုကပါ။ ဒါမ မျကစမ ညမ ပါ။
95 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ပထမ စစခ င်ျိုး N=1 ဆိုပပီျိုး N ထကို 1 ထည်လိုက်တယ်။ ပပီျိုးဆတာ N<99 ဆိုတ condition ကို
3, ထပ်ထိုတ်ဆတာ 1,3, ဆပို ပီျိုး output ထွေက်ပပီျိုးသာျိုး ြစ်မယ်။ ပပီျိုးရင် update လိုပ်ပပီျိုး 2 တျိုးို ဆတာ N
တေ်ြိုျိုး 5 ြစ်မယ်။
1, 3, 5, 9, … , 97,
မာျိုးတယ်၊ မာျိုးဆတာ looping ကဆေ ထွေက်ပပီျိုး false ဘက် ခမ်ျိုးသွောျိုးလိုပ်ဆတာ N ကို comma မပါပ
ထိုတ်တယ်။
1, 3, 5, 9, … , 97, 99
ဆိုပပီျိုး ြစ်သွောျိုးမယ်။ ဒီဆလာက်ဆိုရင် looping ဆိုတာရယ်၊ looping နင် condition ကွော ခာျိုးခ က်ရယ်
N =1 (initialization)
96 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Write N, “,”
N = N + 2 (update)
End of Loop
Write N
Write N
န ေ့ကျငေ့ကကညေ့ရနအ င။
(0 ဆောက်မာ comma မပါလိုို့ 0 ကို သပ်သပ်ြယ်ထာျိုး၊ looping ပပီျိုးမ လိုပ်မယ်။ ဒါဆို looping
ဘယ်ကစ 100 ကစ၊ ဘယ်အထလ 5 အထ၊ ဆရျို့ကို ပေ်ကကည်ဆတာ 100 တိုို့ 95 တို့ဟ
ို ာ 5
97 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1+ 1/2 + 1/3 + 1/4 + ……+ 1/n (n တေ်ြိုျိုးကို user ထံမ ဆတာင်ျိုးပါ၊ n တေ်ြိုျိုး 5 ဆရ
ို င် 1/5
Nested loop
looping ပေ်ပါတာ ြစ်ပါတယ်။ အ ပင်ဘက်က looping ကို outer loop လိုို့ ဆခေါ်ပပီျိုး၊ အတွေင်ျိုးထက
အလိုပ်လိုပပ
် က
ံို ဆတာ outer loop တစ်ကကမ်မာ inner loop ပပီျိုးဆအာင်လိုပ၊် outer loop
တစ်ကကမ် ဆိုပပီျိုး လိုပ်ခ င်တ ဆကာင်သည် outer loop ြစ်ပပီျိုး၊ အဒီ တစ်ကကမ်ထမာ အမ ာျိုးကကီျိုး
No 1.
Question 1
Question 2
Question 3
Question 4
98 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
No 2.
Question 1
Question 2
Question 3
Question 4
No 3.
Question 1
Question 2
Question 3
Question 4
သွောျိုးတာ ြစ်ပါတယ်။ ဒါဆကကာင် တစ်ကကမ်စီ လိုပ်တ No သည် outer loop ြစ်ပပီျိုး၊ Question ကဆတာ
inner loop ြစ်မာ ြစ်ပါတယ်။ No အတွေက် variable တစ်ခိုေို့ Question အတွေက် variable တစ်ခို
quote ထဆရျိုး၊ တေ်ြိုျိုးဆပေါ်ခ င် ဒီတိုင်ျိုးဆရျိုး၊ အဒီနစ်ခိုကို ဆက်ပပီျိုး ဆပေါ်ခ င်ရင် comma ေို့ဆက်။
99 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
8. End of Loop
9. N=N+1
10. End of Loop
က while ေို့ ဆရျိုးထာျိုးတာဆလျိုးကို trace လိုက် ပပါမယ်။ trace လိုက် ပရင် ဆ ပာရတာ လွေယ်ဆအာင်
အပတ် ြစ်လိုို့ inner loop ရျို့ condition ကို ပေ်တက်မာ ြစ်ပါတယ်။ Line 10 ကဆတာ outer loop
ရျို့ အပတ် ြစ်တ အတွေက်ဆကကာင် outer loop ရျို့ condition ကို ပေ်တက်မာ ြစ်ပါတယ်။
1 1 N ထကို 1 ထည်တယ်။
4 1 Q ထကို 1 ထည်တယ်။
ပထာျိုးတ အတင
ို ်ျိုး ထိုတ် ပပပီျိုး ဆအာက်တစ်ဆကကာင်ျိုး
ဆင်ျိုးမယ်။
7 2 Q=Q+1
100 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
7 3 Q=Q+1
7 4 Q=Q+1
7 5 Q=Q+1
9 2 N=N+1
101 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ပေ်တက်မယ်။
4 1 Q ထကို 1 ထည်တယ်။
ပထာျိုးတ အတင
ို ်ျိုး ထိုတ် ပပပီျိုး ဆအာက်တစ်ဆကကာင်ျိုး
ဆင်ျိုးမယ်။
7 2 Q=Q+1
7 3 Q=Q+1
7 4 Q=Q+1
102 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
7 5 Q=Q+1
9 3 N=N+1
ပေ်တက်မယ်။
4 1 Q ထကို 1 ထည်တယ်။
103 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ပထာျိုးတ အတင
ို ်ျိုး ထိုတ် ပပပီျိုး ဆအာက်တစ်ဆကကာင်ျိုး
ဆင်ျိုးမယ်။
7 2 Q=Q+1
7 3 Q=Q+1
7 4 Q=Q+1
7 5 Q=Q+1
104 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
9 4 N=N+1
ပေ်တက်မယ်။
ဆတွေျို့ရပါလမ်မယ်။
105 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အကိုေ်ခ တွေက်ပါ။
Week 1
1 2 3 4 5 6 7
Week 2
8 9 10 11 12 13 14
Week 3
15 16 17 18 19 20 21
Week 4
23 24 25 26 27 28
Chapter အနစချျုပ
▪ တညီတ အလိုပ်ကို ထပ်ခါ ထပ်ခါ လိုပ်တာကို looping or iteration လိုို့ ဆခေါ်ပါတယ်။ looping
မာ while ေို့ for ဆိုပပီျိုး ဆရျိုးေည်ျိုး ၂ ေည်ျိုးရပါတယ်။ ဘယ်ဆရျိုးေည်ျိုး ြစ် ြစ် အပင
ို ်ျိုး ၃ ပင
ို ်ျိုးပါပါတယ်။
106 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ Nested loop မာ အ ပင် loop ကို outer loop လိုို့ဆခေါ်ပပီျိုး အတွေင်ျိုးထက ဆရျိုးတ loop ကို inner
loop လိုို့ ဆခေါ်ပါတယ်။ outer loop တစ်ကကမ်မာ inner loop ပပီျိုးဆအာင်လိုပ်ပါတယ်။ ဒါဆကကာင်
ဆကာင်က outer loop ြစ်ပပီျိုး၊ အမ ာျိုးကကီျိုး လိုပ်တ ဆကာင်ကဆတာ inner loop ြစ်ပါမယ်။
တို့လ
ို ိုပ programmer တင
ို ်ျိုး ောျိုးလည်ပါတယ်။
107 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဗိုကက
် ဆာဆေပပီ ဘယ်နစ်ောရီမထိုျိုးဆသျိုးလိုို့ ထမင်ျိုး မစာျိုးဆသျိုးဘျိုးတ၊ိုို့
စေစ်ဇယာျိုးေို့ စာအိုပ်ကကီျိုးသမာျိုးဆတွေဆပါ။
တစ်ခခ
ို ိုဆို သတိုို့ သတ်မတ်ခ က်ဆတွေေို့ condition စစ်လမ
ိုို့ ပပီျိုးဆတာဘျိုး၊
108 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၈)
Efficiency of Algorithm
က ောင််းမွနတ
် ဲ့ algorithm တစ်ခုဟော မှန် န်မရ
ှု ှှိတယ်၊ ပှိပု ပ်းတ ယ်က ောင််းတဲ့ algorithm
မှနလ
် ည််းမှနတ
် ယ်၊ efficient လည််းဖြစ်တယ်။ Efficient ဖြစ်ြဆ
ှိုို့ ှိုရင် “minimum use of
resources” ဖြစ်ရပါမယ်။ time တှိုို့၊ memory spaceတှိုို့ စတဲ့ resourcesကတွ ှို အနည််းဆု်း
အသု်းဖပြုရမှော ဖြစ်ပါတယ်။
ဆှိုက ပါစှိုို့ သခ္ခောပုစောဆ တစ်ပုဒ် ှို က ခောင််းသော်းနှစ်ကယော ်တွ ်တယ်။ နှစ်ကယော ်လု်းဟော
မှနတ
် ယ်၊ ဒါကပမဲ့ တစ်ကယော ် 15 min နို့ ပပ်းပပ်းကတောဲ့ ခန်တစ်ကယော ် 25 min
(အခခှိန်တတ
ှို ှို အတွင််း) ပပ်းတဲ့ 15 min ပ က ောတဲ့ က ခောင််းသော်း ပှိဖု မန်တောကပါ ဲ့။ ဒါကက ောငဲ့် time
အလုပ်လုပတ
် ယ်၊ စ ်ကရ်းတယ်ကပါ ဲ့၊ ခန် game ကတောဲ့ စ ်သှိပမ
် ကရ်းဘူ်း၊ ကပါ ဲ့တယ်၊ memory
109 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အလုပ်လုပက
် လ ဖြစ်ပါတယ်။
ပါတယ်။
နှိုင််းယှဉ်ဖပြှိအ
ုို့ တွ ် သု်းပါတယ်။
▪ Best Case – Worst case နို့ ဆနို့် ခင်ဘ ် အနည််းဆု်းက ောမညဲ့်အခခှိန် ှို တှိင
ု ််းတော တော
Prime Number
မဖပတ်ကသော ှိန််း ှို Prime Number (သုဒဓ ှိန််း) လှိုို့ ကခေါ်ပါတယ်။ Prime Number ဟာ 2
110 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
စာ်းကကညော့ရပါ ော့မယ။ ပပတသည အထိ စာ်းကကညော့ပါမယ။ စာ်းလိိုို့ ပပတသွာ်းတော့ အချိနမှ စာ်းတော့ဂဏန်း နို့
မဖပတ်ဘူ်း၊ ဒကတောဲ့ စော်းတဲ့ ှိန််း ှို 1 တှိ်းု ပပ်းစော်းက ညဲ့်မယ်။ 3 နို့ ထပ်ပပ်းစော်းက ညဲ့်တယ် မဖပတ်ဘူ်း၊ 4
အစော်းခရတ ှိန််း နို့ စော်းတဲ့ ှိန််း နှစ်ခု ှို တှို ်က ညဲ့်လှို ်ကတောဲ့ 5 ခခည််း ဖြစ်ကနတဲ့အတွ ်
ဖြစ်တယ်။
8 ှို prime number ဟုတ်မဟုတ် စစ်က ညဲ့်မယ်ဆှိုရင် 2 ဖြငဲ့် စပပ်း စော်းက ညဲ့်ပါမယ်၊ စော်းလှိုို့
ဖပတ်တဲ့အတွ ် အစော်းခရတဲ့ ှိန််းနှငဲ့် စော်းတဲ့ ှိန််း ှို တှို ်က ညဲ့်တဲ့အခါ 8 ရယ် 2 ရယ် ဆှိုကတောဲ့
1. isPrime(num)
2. If num<=1 then:
3. Return false
4. d=2
5. Repeat while (num mod d) != 0:
6. d = d+1
7. End of loop
8. If num == d then:
9. Return true
111 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
10. Else:
11. Return false
Trace လှို ်မဖပခင် Algorithm ှို နည််းနည််းကလော ် အရင် ရှင််းဖပပါမယ်။ Prime number
ဟုတမ
် ဟုတ် user စစ်ခခင်တဲ့ ှိန််း ှို num ဆှိုပပ်း parameter လ ်ခလှို ်ပါတယ်။
မဟုတပ
် ါဘူ်း။ ဒါ input error ှိုနမူနနာကဖပောဖပတောပါ။ တွ ်လှို ်မှ error တပ်တယ်၊
number မဟုတဘ
် ူ်း။ အဒါကလ်း ှို function စစခခင််း တည််း စစ်ထော်းကပ်းတယ်။ ဒကတောဲ့ 2
ကအော ်ငယ်တောကတွ ထညဲ့်ခဲ့ရင် တန််းပပ်း false return ဖပန်မှောကက ောငဲ့် ကအော ် အလုပ်ကတွ
ဆ ်ပပ်းလုပစ
် ရောမလှိုကတောဲ့ပါဘူ်း။ return ပပနတယဆိိုတာ function ထကနနထွက၊ လှမ်းနခေါ်တော့
112 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒကတောဲ့ d ှို 1 တှိ်းု ပပ်း 3 နို့စော်းတယ် မဖပတ်ဘူ်း၊ ဒလှိုနို့ တှိ်းု သွော်းလှို ်တော 1 ှို
လိိုပါတယ။
ဆှိုကတောဲ့ မဖပတ်မခခင််း (num ှို d ဖြငဲ့်စော်းတော အက ွင််း 0 နှငဲ့် မညမခခင််း) ပတ်ပါမယ်။ num mod d
!= 0 ဟော condition ဖြစ်ပါတယ်။ မဖပတ်ရင် စော်းတဲ့ d ှို 1 တှိ်းု တှို်းပပ်း စော်းမှောဖြစ်လှိုို့ d = d +1 ဆှိုတော
ဘောမှ မလုပ်ခခင်တဲ့အတွ ် while looping ထမှော လုပ်ခခင်တဲ့ body မပါ ပါဘူ်း၊ update ပပါ ပါတယ်။
တူရင် prime number ဖြစ်တဲ့အတွ ် true return ဖပန်ပါ ဲ့မယ်။ မတူရင် prime number မဟုတလ
် ှိုို့
false return ဖပန်ပါ ဲ့မယ်။ ဒကလော ်ဆှို algorithm ှို နာ်းလည်ကလော ်ပပ ဖြစ်လှိုို့ trace
113 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Ans = isPrime(0)
1 0
Return ဖပန်လှို ်တဲ့ false ဟော function ထ ကနထွ ်ပပ်း function call ကခေါ်ထော်းတဲ့ ကနရော ှို
Ans = isPrime(5)
1 5
return ှိုအလုပမ
် လုပ်ကတောဲ့ဘူ်း။
4 2 d=2
မှနက
် တောဲ့ line 6 ှို သွော်းမယ်။
6 3 d = d +1
ှိုဖပန်တ ်မယ်။
114 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
မှနက
် တောဲ့ line 6 ှို သွော်းမယ်။
6 4 d = d +1
ှိုဖပန်တ ်မယ်။
မှနက
် တောဲ့ line 6 ှို သွော်းမယ်။
6 5 d = d +1
ှိုဖပန်တ ်မယ်။
Return ဖပန်လှို ်တဲ့ true ဟော function ထ ကနထွ ်ပပ်း function call ကခေါ်ထော်းတဲ့ ကနရော ှို
115 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Ans = isPrime(6)
1 5
4 2 d=2
ှို သွော်းမယ်။
function ထ ကန ထွ ်သွော်းမယ်။
Return ဖပန်လှို ်တဲ့ false ဟော function ထ ကနထွ ်ပပ်း function call ကခေါ်ထော်းတဲ့ ကနရော ှို
ဟုတတ
် ောရယ်ဆှိုပပ်း case 3 ခုရှှိပါတယ်၊ ဒမှော ၃ ခုလု်း trace လှို ်ဖပထော်းပါတယ်။ Testing လိုပရင
116 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1. isPrime(num)
2. If num<=1 then:
3. Return false
4. j=2
5. Repeat while j<[(num/2)+1] and (num mod j) != 0:
6. j=j+1
7. End of loop
8. If num mod j==0 then:
9. Return false
10. ELSE:
11. Return true
စော်းြှိို့အ
ု တွ ် အသု်းဖပြုမညဲ့် variable name ှို j လှိုို့ကပ်းထော်းတယ်။ j ထ ်စောရင် အကပေါ်
တိင
ို ်း လိိုကကိို လိိုကနာသငော့တော့ အနလော့အကျငော့နကာင်းနတွထက တစခို ပြစပါတယ။
condition စစ်ထော်းတက
ဲ့ နရောမှော ဖြစ်ပါတယ်။ Algorithm1 စော်းလှိုို့ မဖပတ်မခခင််း looping
117 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အလုပ်လုပမ
် ယ်)၊ စော်းလှိုို့လည််း မဖပတ်ဘူ်း ဆှိုတဲ့ အခခ ်နှစ်ခခ ် ှို စစ်ထော်းပါတယ်။ and ဖြငဲ့်
အသု်းခခထော်းတောဖြစ်လှိုို့ သူဟော အမခော်းဆု်း ဝင်လောတဲ့ ှိန််းရဲ့ တစ်ဝ ်ထှိ ပ တွ ်ခခ ်မောှ ဖြစ်လှိုို့
သူ အလုပ်လုပရ
် တဲ့ time သ ်သောမှော ကသခခောကပါ ်ပါပ။ အကပေါ်မှောကဖပောခဲ့တဲ့ time complexity
condition 2 ခု ှို and နို့ ဆ ်ထော်းလှိုို့ ပထမ algorithm ရဲ့ while condition ထ ်
▪ မှိမှိ ှိုယတ
် င
ှို ်နို့သော စော်းဖပတ်တဲ့ ှိန််းဂဏန််းကတွမှောကတောဲ့ ှိန််းဂဏန််းတန်ြု်းှိ
997 ဆှိုရင် ပထမ algorithm 2 ကန 997 ထှိ စော်းက ညဲ့်မောှ ဖြစ်ပပ်း၊ ဒုတှိယ
algorithm ကတောဲ့ 997 ရဲ့ တစ်ဝ ် 498 ထှိပ တွ ်မှောမှိုို့ တစ်ဝ ်တတ
ှိ ှိ တွ ်ရတော
118 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
နကာင်းတာ သိသာထငရှာ်းတာပြစပါတယ။
worst case မှာ တန်းပပ်း သိသိသာသာ အနပြနပေါ်နလော့ရှိပါတယ။ ဒါနကကာငော့ algorithm နတွကိို worst
အတိန
ို ရ်းနည်း
နာ်းလည်ပပ်းသော်း ဖြစ်ပါလှိမမ
ဲ့် ယ်၊ Return num==d ဆှိုတဲ့ ဒုတယ
ှိ တစ်ခုပ ှို ရှင််းဖပပါမယ်။
119 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ num တန်ြှို်းဟော 6 ဆှိုရင် d တန်ြှို်း 2 နို့ တင် စော်းဖပတ်တယ်။ ဒကတောဲ့ return 6==2 ဆှိုပပ်း
▪ num တန်ြှို်းဟော 5 ဆှိုရင် d တန်ြှို်း 5 နို့ စော်းမှ ဖပတ်ကတောဲ့ return 5==5 ဆှိုပပ်း ဖြစ်မယ်၊ 5==5
အလုပ်လုပသ
် ွော်းတောဖြစ်လှိုို့ အလုပ်လုပ် ပု အတူတူပ ဆှိုတော သကဘောကပါ က
် လော ်ပါပပ။
အလုပ်လုပပ
် တ
ု ူရင် အတှိုကရ်းနည််း ကရ်းရတော သ ်သောတယ်၊ မှို ်တယ်။ Code complexity
ဖြစ်သွော်းရကအောင် အတှိက
ု ရ်းနည််းကလ်းကတွ ှို တခုတ်တရ ကရ်းြှိို့လ
ု ှိုပါတယ်။
ဖြစ်နင
ှို ်သမျှ တပ်လောနှိုင်တဲ့ error ဆှိုတော ှို ဖမင်သောထင်သော ရှှိကအောင် 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0
ဆှိုတဲ့ quadratic equation ှို စဉ််းစော်းက ညဲ့်က ရကအောင်ပါ။ အဒ equation ကန x တန်ြှို်း ုှိ
တွ ်ခခင်ရင်
−𝑏 ± √𝑏 2 − 4𝑎𝑐
𝑥=
2𝑎
root ရှောလှို ်တဲ့အခခှိန် error တပ်မောှ ဖြစ်ပါတယ်။ ဒါကက ောငဲ့် 𝑏 2 − 4𝑎𝑐 ှို အရင်တွ ်ပပ်း condition
120 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1. အနှုတ်ဖြစ်ခရ
ဲ့ င် အကဖြမရှှိတဲ့ no solution ပုစောဆ ဖြစ်ပါတယ်။
2. 0 ထွ ်ခဲ့ရင် 0 ှို square root ရှောရင် 0 ပထွ ်မောှ ဖြစ်လှိုို့ ± တွ ်စရောမလှိုကတောဲ့ပ -b/2a
အကဖြနှစ်ခု ထုတ်ဖပရမှောဖြစ်ပါတယ်။
quadraticEquation( a, b, c)
1. Set Temp = b * b – 4 * a * c
2. If Temp < 0 then:
3. Write “No Solution”
4. Else if Temp == 0 then:
5. Set ans = -b /(2*a)
6. Write “One Solution :”, ans
7. Else:
8. Temp = sqrt (Temp)
9. Ans1 = (-b + Temp) / (2*a)
10. Ans2 = (-b – Temp)/ (2*a)
11. Write “Two Solutions :”, Ans1, “ and “, Ans2
ဒလှို ကရ်းလှို ်ဖခင််းအော်းဖြငဲ့် 𝑏 2 − 4𝑎𝑐 တန်ြှို်း >0 ဖြစ်မှ square root ှို ရှောမှောဖြစ်တောကက ောငဲ့် 𝑏 2 −
4𝑎𝑐 အနှုတ်ထွ ်ခဲ့ရင် square root တွ ်စရောမလှိုကတောဲ့တော ဖြစ်လှိုို့ error တ ်စရော အကက ောင််း
121 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Summation Algorithm
1. summation(num)
2. total = 0
3. n=1
4. Repeat while n < =num:
5. total = total +n
6. n=n+1
7. End of loop
8. return total
1 5
initialize လုပ်ကပ်းရပါတယ်။)
3 1 n=1
122 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
6 2 n = n+1 (update)
6 3 n = n+1 (update)
6 4 n = n+1 (update)
6 5 n = n+1 (update)
6 6 n = n+1 (update)
123 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Return ဖပန်လှို ်တဲ့ တန်ြှို်းဟော call မှော ြမ််းထော်းတဲ့ total ထဝင်သွော်းမှော ဖြစ်ပါတယ်။ ဒမှော
လှမ််းကခေါ်တဲ့ကနရော ဂဏန််း 5 ထညဲ့်ကပ်းလှို ်လှိုို့ trace လှို ်တဲ့ ဇယော်းမှော 5 က ှိမ် loop
ပတ်သွော်းတော၊ 5 က ှိမတ
် ွ ်သွော်းရတော ဖြစ်ပါတယ်၊ 100 ထညဲ့်ရင် အက ှိမ် 100 loop ပတ်ပပ်း အက ှိမ်
1. summation(num)
2. total = num * (num+1) /2
3. return total
total = summation(5)
1 5
3 Return 15
124 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Summation(1) မှော
1. n=1
2. Repeat while n < =num:
3. total = total +n
4. n=n+1
5. End of loop
ပတ်ပပ်းကပါင််းရတအ
ဲ့ တွ ်
ဒါကက ောငဲ့် O(n) လှိုို့ ကဖပောလှိုို့ရပါတယ်။ Big O ဟော input တန်ြှို်း n အတွ ်ကရ်းကလဲ့ရှှိပါတယ်။
ဘောလောလော ဒတစ်ကက ောင််းပတွ ်တောဖြစ်လှိုို့ O(1) လှိုို့ ကဖပောလှိုို့ရပါတယ်။ num တန်ြှို်း 1 ဖြစ်လ
တစ်က ှိမပ
် တွ ်မယ်။ num တန်ြှို်း 2 ဖြစ်လ 1 က ှိမ်ပတွ ်မယ်ဆှိုတဲ့ အဓှိပပါယ်ဖြစ်ပါတယ်။ O(1)
125 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Factorial Algorithm
factorial ရှောြှိို့အ
ု တွ ်ဆှိုရင် user ကပ်းတဲ့ ဂဏန််းဟော 0 နို့ အထ ် ဖြစ်ရပါမယ်။
Algorithm: Factorial
1. factorial(n)
2. If n<=1 then
3. Return 1
4. ans = 1
5. Repeat while n>0:
6. ans = ans * n
7. n=n-1
8. End of loop
9. return ans
Line 1 မှော /* နှငဲ့် */ အတွင််း ကရ်းတော ှို comment ကရ်းတောလှိုို့ကခေါ်တယ်။ comment ဟော
ှိုယဘ
် ောကတွ ကရ်းထော်းခဲ့မှန််း မမှတမ
် ှိကတောဲ့ပါဘူ်း။ ဖပင်တောထ ် ှိုယ်ရ
ဲ့ ဲ့ code ှို နာ်းလည်ကအောင်
ဖပန်လုပရ
် တော အခခှိန်အကတော်ယူပါတယ်။ Code ှို စတင်ကရ်းသော်းခဲ့တဲ့ အခခှိန် သော comment
ဖပင်ြလ
ုို့ှိ ှိုတယ်၊ ခန်ခတ
ဲ့ ဲ့ programmer ဖပင်ရကတောဲ့မောှ ဖြစ်ပါတယ်။ ှိုယ် သော comment
126 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Line 2 & 3 ကတောဲ့ user >=0 ှိုပ input သွင််းြှိို့ု ကဖပောထော်းတောဖြစ်လ၊ှိုို့ n<=1 ဆှိုတော 0! နို့ 1!
ကဖမော ်လှိရ
ုို့ တဲ့ အကဖြ ှို ans ထ ထညဲ့်ပါတယ်။ တ ယ် n ကန 2 အထှိပ loop ပတ်ရင်ကတောင်
ရပါတယ်။ ဘောလှိုို့လဆှိုရင် 1 ထှိ loop ပတ်ထော်းပပ်း 1 နို့ ကဖမော ်တော အကဖြ ှို
မကဖပောင််းလကစတအ
ဲ့ တွ ်ကက ောငဲ့် ဖြစ်ပါတယ်။
Line 9 ကတောဲ့ looping ပပ်းသွော်းတဲ့ အခခှိန်မောှ အကဖြ ans ှို return ဖပန်ကပ်းတော ဖြစ်ပါတယ်။
127 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Permutation
Algorithm: Permutation
1. permutation(n,r)
2. If n<r then
3. Return 0
4. ans = factorial (n)/factorial (n-r)
5. return ans
အဒမှောမှ ဝင်လောတဲ့ တန်ြှို်း ၂ ခု ှို စော်းပပ်း ရလောတဲ့ အကဖြ ှို ans ထ ထညဲ့်လှို ်တောဖြစ်ပါတယ်။
ဒတစ်ခါ trace လှို ်ရမှော လည််း စောြတ်သူရဲ့ အလှညဲ့်ပ ဖြစ်ပါတယ်။ Trace လှို ်ြုို့ှိ မပခင််းပါနို့။
ဖြစ်ပါတယ်။
128 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
result = permutation(2,3)
result = permutation(3,2)
result = permutation(3,3)
နလော့ကျငော့ကကညော့ကကရနအာငပါ။
1. 1 ကန 100 အတွင််းမှော ရှှိတဲ့ prime number ကတွ ှို output ထုတ်ဖပကပ်းပါ။ လုပ်မှော 1 ကန
100၊ သှိက
ုို့ သော် prime number သည် 2 စတောဖြစ်လှိုို့ 2 ကန 100 အထှိ looping
129 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Chapter အနှစချြိုပ
▪ မှနတ
် ောဖခင််းတူခရ
ဲ့ င် ဖမန်သလော်း(time complexity) နို့ ကပါ ဲ့သလော်း (space complexity) ကတွနို့
နှိုင််းယှဉ်ကလဲ့ရှှိပါတယ်။
▪ Complexity တွ ်တဲ့ကနရောမှော worst case, average case နို့ best case ဆှိုပပ်း ၃ မခှိြု်းရှှိပါတယ်။
စှိတ်ဝင်တစော်း စောအုပထ
် မှော ခခပပ်း trace လှို ်ပါ။
▪ အဓှိပပါယ်ရှှိတဲ့ variable name ကပ်းတော၊ comment ကရ်းတော၊ formula ကတွ ှို အသု်းခခ
130 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
“ရှင််းကအောင်ကဖပောရရင်
သူ လ
ှို ောမကခေါ်ရင် ဘယ်သူူ့မှ အြ ်မလုပ်ဘူ်း၊
131 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၉)
Recursion
Recursion ဆိုတာ မမ function ထဲကနေပ ြီး မမကို မမ ပ ေ်ပ ြီး function call
န ေါ်တာပြစ် ါတယ်။ recursion ဆိုတာ ဘာနဲ့ တူသလ ဆိုရင looping နဲ့တူပါတယ။ looping ရဲ့
Factorial
n! = n * (n-1)!
အဲဒမာဆိုရင် n factorial ကနေ n-1 factorial ကို ပ ေ်န ေါ်ထာြီးတာပြစ် ါတယ်။ factorial
Algorithm: Factorial
1. factorial(n)
2. If n<=1 then:
3. Return 1
4. Else:
5. Return (n * factorial(n-1))
ဒ algorithm မာ line 5 က recursion ပြစ် ါတယ်။ return statement ထဲမာ factorial (n-
132 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Exit point ကနတာော့ 0! ေဲို့ 1! အတွက် loop တ်စရာမလို ဲ အနပြ 1 ကို return ပ ေ်န ြီးြို့ို
f = factorial (5)
1 5
133 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ကို ကကညော့် ါ။
Exit point ကနေ ထွက်လာပ ြီး return 5*4*3*2*1 ဆိုနတာော့ return 120 ဆိုပ ြီး မူရင်ြီး
F = factorial (7)
134 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Triangle
Line 1
Line 2
Line 3
Line 4
န
ို လြီးက line 4 အထ ဲ ေမူော ဆွဲပ ထာြီးတာ ပြစ် ါတယ်။ Line 1 တစ်နကကာင်ြီးထဲဆိုရင်
အတြီးို နလြီး ၁ တိုြီးထဲ ပြစ် ါမယ်။ Line 2 ထ ဆိုရင် အတြီးို အနရအတွက်က 2 + 1 (line 2+ line 1)
ပြစ် ါမယ်။
Line အနရအတွက် 1 2 3 4
triangle(1) = 1
triangle(2) = 2 + 1 = 2 + triangle(1)
135 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Algorithm: Triangle
1. triangle(l)
2. If l==1 then:
3. Return 1
4. Else:
5. Return (l + triangle(l-1))
ဒတစ် ါနတာော့ Trace ကို အတို ြီးပ ြီး လိုက်ပ ရင် သန ာန ါက်နလာက် ါပ ။
= 4 + 3 + triangle(2)
= 4 + 3 + 2 + triangle(1) = 4 + 3 + 2 + 1 = 10
ရလ ျငက ညက ရရအာင။
သန ာကနတာော့ n တေ်ြိုြီး input လက် ၊ ပ ြီးရင် n ကနေ စပ ြီး count down လို ်မယ်။ 0
နရာက်တာေဲို့ Happy New Year လိုို့ ထိုတ်မယ်။ ဒနတာော့ 0 ဟာ exit point ပြစ် ါမယ်။
2. Power function ကို recursion နရြီး ါ။ Parameter 2 ို လက် ါမယ်။ b ေဲို့ p ပြစ် ါတယ်။
power(2,0) = 1
power(2 , 1) = 2
power(2, 2) = 2* 2 = 2 * power (2, 1)
136 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
power(2, 3) = 2* 2* 2 = 2* power(2,2)
3. နရဲ့က သ ္ ာအ ေ်ြီးမာ နပ ာ ဲော့တော့ဲ Fibonacci အတွက် recursion function နရြီး ါ။
0 1 1 2 3 5 8 13
Chapter အနစချ ပ
▪ Function တစ် ို လမ်ြီးန ေါ်လိုက်ရင် memory န ေါ်မာ နေရာယူပ ြီးနတာော့၊ function ထဲက နေ
ထာြီးတာ မပ ြီး င်၊ အနပြမထွက်နသြီး င် အဲဒ call အတွက် အနပြထွက်ြို မမ function ကို မမ
မ
ို ာြီး ါတယ်။
ရှု န
် ထွြီးမှုကနတာော့) ေည်ြီး ါတယ်။
137 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
“တ ဲ့လန
ူ တွ
‘နပ ာပ ေ်ရင် ဟန
ို ေို့က စကာြီးထ ်နပ ာရအိုြီးမယ်’
ရစ်တာနတာင် ဟန
ို ေို့က နပ ာ တ
ဲော့ ဲော့စကာြီးကို
138 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၀)
Data မ ်းသိမ်းဆည်းပုံ
Data သိမ်းဆည်းခခင်း
တ ာခဲ့ပ ်း ြစ ါ ယ။
Variable Declaration
139 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
JavaScript ဲ့ို ြစ ါ ယ။
ါလည်းရ ယ၊ မ ါလည်းရ ယ ဆို ဲ့ အဓ ပါယ ြစ ါ ယ။ value ဆို ဲ့ နြို်း assign လို ချင
VariableName[=value];
ဆို ာကတ ာဲ့ loosely typed language စခို ြစ ဲ့ PHP မာ variable declaration
တ ်းရ ါ ယ။ English စာတ ကာင်း စတ ကာင်းမာ ‘.’ နဲ့ အဆို်းသ သလို ြစ ါ ယ။ ဒါတ မဲ့ ချ ျို့
Language စခိုနဲ့ စခို ကွေ ာ်း ယ ဆို ာရယ၊ algorithm ဆို ာ programmer
140 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အာ်းလို်းန ်းလည ဲ့ အရာဆို ာရယ ဒတလာက သတဘာတ ါကရင ရ ါ ပ ။ ဒမာတ ာဲ့ လိုအ ဲ့
နြို်း စခိုသာ သမ်းဆည်းနိုင ဲ့ data type တ ွေကို Primitive Data Types လိုဲ့
ခွေငဲ့ ထာ်း ဲ့ data types တ ွေ မ ူည က ါဘူ်း။ ဒတ ာဲ့ ဒမာ အသို်းမျာ်း ဲ့ primitive data types
▪ Interger – data ရျို့ နြို်းက ကန်း ညဲ့ဆိုရင interger data type ါ။ အသကဆိုရင ၄ နစ
၅ နစ စသည ြငဲ့ ကန်း ညဲ့တ ာတလဲ့ရ ါ ယ။ ဒဿမနဲ့ တ ာတလဲ့မရ ါဘူ်း။ ဒါတ ကာငဲ့
int age = 5;
ဒါကတ ာဲ့ Java မာ variable declaration လို ာတလ်း နမူန တရ်း ထာ်း ာ ြစ ါ ယ။
int age;
$age=5;
141 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ကို ကညဲ့ပ ်း integer data type လိုဲ့ auto သ မ သွော်းမာ ြစ ါ ယ။ PHP ရျို့
တ က ငာမယဆိုရင
single quote ထမာ ထညဲ့ပ ်းတရ်းတ ်းရ ါ ယ။ A ဆို စလို်း သမ်းလိုဲ့ရ ါ ယ၊ AB နစလို်း
char ch = ‘A’;
သတဘာတ ါကတလာကပ ထင ါ ယ။
အဒထက
▪ String
142 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ File
▪ Linked list
▪ Tree
String
လူန မည၊ ပမ ျို့အမည၊ အမလ စာ စ ာတ ွေ ြစ ါ ယ။ value ကို တရ်းရင double quote နဲ့
တရ်းရ ါ ယ။
$city = “Mandalay”;
ဒါကတ ာဲ့ java နဲ့ PHP မာ String data type တရ်းနည်းကို နမူန တရ်း ာ ြစ ါ ယ။ data type
ထညဲ့တရ်းထာ်း ာက Java နဲ့ တရ်း ထာ်း ာ ြစပ ်း၊ data type မ ါဘ variable name $ (dollar
143 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
File
2 Su Su CT Second Year
Student Table
ဒလို သမ်း ဲ့ စ
ို ကို File လိုဲ့တခေါ် ါ ယ။ တခါင်းစဉတ ွေ ြစ ဲ့ Roll_No, Name, Major, Year ဲ့က
ို ို
attribute or field or properties လိုဲ့ တခေါ် ါ ယ။ 1 , Aye Aye, CT စသည ြငဲ့ table မာ ထမ row
Table မာဆိုရင record 4 ခို ါ ါ ယ။ အဒလို records တ ွေ တ ါင်းထာ်း ာကို File လိုဲ့ တခေါ် ါ ယ။
Table ကို ကညဲ့ ါ။ Aye Aye ဆိုပ ်း အမည ူ နစတယာကရ ါ ယ။ န မညဆို ာ ူနင
ို ထ နိုငလိုဲ့
unique မ ြစ ါဘူ်း။ ဒါတ မဲ့ roll-no ကတ ာဲ့ မထ ါဘူ်း။ unique ြစ ါ ယ။ ဒါတ ကာငဲ့ ဒ table
144 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Aye Aye, CT, Second Year ကို တ ာမန်း န်းပ ်း သ ာမျ ်း ါ။ အဒလို unique ြစ ဲ့တကာငကို
2 Su Su CS Second Year
အတ ေါ်က student table ဟာ CT major စခို ည်း ာ ြစလိုဲ့ roll-no မထ ါဘူ်း။ ယခို table
ကတ ာဲ့ CT, CS ဆို ဲ့ major ၂ ခို တရာသမ်းထာ်း ာ ြစလိုဲ့ roll-no 1 ဆိုလည်း 2 တယာက၊ roll_no
2 ဆိုလည်း 2 တယာက ြစတန ါ ယ။ ဒါတ ကာငဲ့ roll-no ဟာ unique မ ြစဘူ်း။ roll-no တ ာရိုနဲ့
ဘယသူလ မသနိုင ါဘူ်း။ ဒါတ မဲ့ CS roll-1 ဆိုရငတ ာဲ့ unique ြစ ါ ယ။ Aye Aye ကို
တ ာမန်းသ ါ ယ။ ဒါတ ကာငဲ့ ဒ table ရျို့ primiary key ကတ ာဲ့ Roll_no နငဲ့ Major နစခို ွေ
ထာ်း ာ ြစ ါ ယ။
ဒ File ၂ ခိုမာ roll-no က 1,2,3 စသည ြငဲ့ သမ်း ာ ြစလိုဲ့ int data type ြစ ါမယ။
1.
145 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
2.
သို်းပ ်း သမ်း ါမယ။ ဥ မာ ကိုတထွေ်းမာ Ko Ko, Ko Let, Nyi Nyi ဆို ဲ့ တမွေ်းချင်း ၃
တယာကရ ယဆို ါစ။ိုဲ့ ဒါဆို သူူ့မာ ညအစကို ၃ တယာကရလဲ့ို Array အခန်း ၃ ခန်း တနရာယူ ါ ဲ့မယ။
0 Ko Ko
1 Ko Let
2 Nyi Nyi
siblings
146 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ Values တ ွေ ြစ ဲ့ Ko Ko, Ko Let, Nyi Nyi ိုဲ့ ဆို ာ စာသာ်းတ ွေ၊ နည်းအာ်း ြငဲ့
Two-Dimensional Arrays
ဦ်းလမာ XYZ လိုဲ့တခေါ် ဲ့ စ ်းို ဆိုငခွေတ ါင်း 4 ခိုရ ယ။ အမညတ ွေကို ြွေငဲ့ ဲ့ အစဉလိုက
XYZ1, XYZ2, XYZ3, XYZ4 လိုဲ့တ ်းထာ်း ယ။ စ ို်းဆိုင စခိုချင်းစရျို့ စလချင်းစရျို့ အ မ ကို
သမ်းချင ယ။ ဒါဆိုရင စ ်းို ဆိုင အမ ၁ ရျို့ January လ အ မ ကို သချင ယဆိုရင XYZ1,
147 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ ဒါတ ကာငဲ့ စခိုသရြိုဲ့ စ ို်းဆိုင အမညရယ၊ လအမညရယ ဆို ဲ့ index ၂ ခုံပခပ မှ သိမှ
variableName[row][column] ဆိုပ ်း တ ာရ ါ ယ။
▪ အထမာ သမ်းထာ်း ာတ ွေက ဂဏန်းတ ွေက ကန်း ညဲ့တ ွေ ြစလိုဲ့ integer data type ါ။
Linked Lists
ငယစဉကက်းလိုက စပ ်း သမ်းထာ်း ာ ြစ ါ ယ။
Doctor Salesperson
148 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Yi Mon လိုကရာရ ာ မလွေယကူ ါဘူ်း။ ဒါဆို ရာရလွေယတအာင ဘယလို သမ်း ကမလ၊ linked list
စ
ို နဲ့ သမ်း ါ ဲ့မယ။ ကညဲ့ ကညဲ့ရတအာင ါ။
6 U Sai Hla 8
7 U Kyaw Kyaw 0
149 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ြစ ဲ့ သူကို ကညဲ့တ ာဲ့ U Tin Ohn ြစ ါ ယ၊ သူူ့ရျို့ link နြို်း ကညဲ့လိုကတ ာဲ့ 3
ြစတန ါ ယ။ ဒတ ာဲ့ index 3 ကို လိုက ကညဲ့ ါ ယ။ U Tun Kyi ါ၊ link နြို်း
ဆက ကညဲ့ ါမယ။ 5 ဆိုတ ာဲ့ index 5 ကိုသွော်း ကညဲ့ ါ ယ။ Daw Myo Myo ါ၊ သူမရျို့link
ဆက ကညဲ့တ ာဲ့ 0 ြစ ဲ့အ ွေက ဆကသွော်းစရာ မလိုတ ာဲ့ ါဘူ်း။ ဒတ ာဲ့ Yi Mon
ာဝနယူထာ်း ဲ့ ဆရာဝန ၃ ဦ်းရ ါ ယ။ U Tin Ohn, U Tun Kyi နဲ့ Daw Myo Myo
ဲ့ို ြစ ါ ယ၊ U Tin Ohn ကတ ာဲ့ Yi Mon ထမဆို်း စချ ဲ့ ဆရာဝန ြစပ ်း၊ ဒို ယချ ဲ့
ဆရာဝနကတ ာဲ့ U Tun Kyi ြစ ါ ယ၊ တန ကဆို်းချ ာကတ ာဲ့ Daw Myo Myo ါ။
မငသာတအာင မ
ို ာ arrow နဲ့ ထာ်း ါ ယ။
▪ Aung Aung အ ွေကလည်း arrow တလ်းနဲ့ ထာ်းတ ်း ါ ယ။ Aung Aung နဲ့ Su Hlaing အ ွေက
တလဲ့ကျငဲ့ ကညဲ့ ါ။
integer data type ြစပ ်း၊ doctor ကတ ာဲ့ စာသာ်းတ ွေ ြစ ာတ ကာငဲ့ string data type
စဉ်းစာ်း ကညဲ့ ါ။
150 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒအ င
ို ်း ရတနမာ ါ။
3. ဒါဆို အလို စဝင ဲ့ တနဲ့တကာ၊ ဆရာဝနနဲ့ စချ ဲ့တနဲ့တကာ သမ်းချငရင ဘယလိုလို မလ။
အလို စဝင ဲ့တနဲ့က Salesperson နဲ့ ဆိုင ာမိုဲ့ Salesperson table မာ ထညဲ့ပ ်း၊ ဆရာဝနနဲ့
စချ ဲ့တနက ဆရာဝန စဦ်းချင်းစနဲ့ ဆိုင ာမိုဲ့လိုဲ့ Doctor table ဆရာဝနရျို့ တဘ်းမာ
အခိုတ ာသွော်း ဲ့ linked list နမူန ကို အလွေယတ ာရရင one to many relationship
ချ ဆကထာ်း ဲ့ အ ွေက one to many relationship ါ။ အဒလို အတ ခတနမျ ်းအ ွေက linked list
ကို အသို်း ါ ယ။
151 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Student
Hierarchical ဆို ဲ့ အ င
ို ်း အဆငဲ့ဆငဲ့ အထ ထ ခွေခွေသွော်း ဲ့ စ
ို နဲ့ သမ်းချင ဲ့ အခါမျ ်းမာ
01 Student
02 Father Name
02 Mother Name
02 Address
03 Division
03 City
03 House
04 Street
152 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
04 House No.
02 Phone
03 Student Phone
03 Guardian Phone
02 DOB
နြို်းတ ွေက value, အဒ value တ ွေကို row လိုကစိုပ ်း စဦ်း၊ စတယာက၊ စခိုကို
153 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
dimensional array လတ
ိုဲ့ ခေါ် ါ ယ။ 2 dimensional array နဲ့ အထကကို multi-
list ကို သို်း ာ တ ာခဲ့ ါ ယ။ many to many relationship (အမျာ်းမ အမျာ်းသိုဲ့ ဆကစ
▪ အဆငဲ့ဆငဲ့ အထ ထ ခွေခွေသွော်း ဲ့ စ
ို နဲ့ သမ်းချင ဲ့ အခါမျ ်းမာ tree or hierarchical
ကန်း ညဲ့ စလို်း သမ်းရ ယ၊ primitive data type double ဆိုရင ဒဿမ ဂဏန်း
154 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၁)
String Operations
String ဆိုတာဘာလဲ။
ကကြိုက်သတ ာက် ပါ ရ
ိုို့ တယ်ဆိုတာ တ ပာခဲ့ပပြီားပါပပြီ။ တ ောက်တစ်ခိုက တူညြီတဲ့ data type တတွေ
စိုထာားတာကို array ိုို့ တခေါ်ပါတယ်။ ဒါတ ကာငဲ့် string ကို character array ိုို့ ည်ား
တ ပာ ိုို့ရပါတယ်။ Array အခ ်ား ံပါတ်သည် ပံိုမ ် ဆိုရင် 0 ကတ စပါတယ်။ ဒါတ ကာငဲ့် String
I C A N F L Y
0 1 2 3 4 5 6 7 8
ဒြီ ိုပံိုစံ တတွေွေ့ မင်ရမာ ြစ်ပါတယ်။ 0 အခ ်ားက I, 1 အခ ်ားမာက space, 2 အခ ်ားမာက C စသည် ြငဲ့်
အခ ်ားအတရအတွေက်) ဟာ 9 ြစ်ပါမယ်။
Length
155 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
toUpper(string) – string ကို အကကြီားစာ ံိုား upper case ကို တ ပာင်ားတာ ြစ်ပါတယ်။
toLower(string) – string ကို အတသားစာ ံိုား lower case ကို တ ပာင်ားတာ ြစ်ပါတယ်။
Substring
156 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Indexing
INDEX(String, pattern)
“ the” ကို ိုက်ရာပါတယ်။ တတွေွေ့တယ် တတွေွေ့တတာဲ့ “ the” ရွေ့ ပထမဆံိုား character ြစ်တဲ့ space
array အခ ်ား ံပါတ်ဟာ 0 ကစပပြီား ကကြီားကကြီားသွောားတာ ြစ် ိုို့ မတတွေွေ့ရင် မရတဲ့ အခ ်ား ံပတ် -1
ထွေက်တာ ြစ်ပါတယ်။
157 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
LASTINDEX(String, pattern)
ပါတ တဲ့အခ ်ဆို INDEX က ပထမဆံိုားတတွေွေ့တဲ့ တ ရာကို return ပ ်တပားပပြီား၊ LASTINDEX ကတတာဲ့
Concatenation
CONCAT(String1, String2)
Insertion
Insert function မာ parameter 3 ခိုပါပါတယ်။ မူရင်ား string ရယ်၊ ထညဲ့်ခ င်တဲ့ တ ရာရယ်၊
158 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
5 ဆိုတဲ့ position တ ရာဆိုတတာဲ့ Hello ရွေ့တ ောက်ကပ်ရက်မာ “! welcome to” ဆိုတဲ့ စာသာားကို
ထညဲ့်သွောားတာ ြစ်ပါတယ်။
Deletion
Insert function မာ parameter 3 ခိုပါပါတယ်။ မူရင်ား string ရယ်၊ စြ က်ခ င်တဲ့ တ ရာရယ်၊
INDEX(“ABCDEFGHIJKLMNO” ,“FGH”)
ဒါတ ကာငဲ့်
ိုို့တရား ိုို့ရပါတယ်။
INDEX(“ABCDEFGHIJKLMNO” ,“FGH”) = 5
159 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
LENGTH(“FGH”) = 3
DELETE(“ABCDEFGHIJKLMNO”, 5, 3) = “ABCDEIJKLMNO”
Replacement
“ABCDABE” ထက “AB” တတွေွေ့သမျှတ ရာတတွေအာား ံိုားမာ “TTT” ကို အစာားထိုား ိုက်တာ ြစ်ပါတယ်။
လလေ့ကျငေ့ကကညေ့ ကကရလအာင။
1.text = “Life is beauty, admire it. Life is a dream, realize it. Life is a challenge, meet it.”
Str =”Life”
a) Length (text) = ?
b) Length(Str) =?
d) text ထက Str ကိုတတွေွေ့တဲ့ ပထမဆံိုားအခ ်ား ံပါတ် ို့ တ ောက်ဆံိုားအခ ်ား ံပါတ်ကို သငဲ့်တတာ်တဲ့
e) INDEX(text, “TO”) = ?
160 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
2. Word ဆိုတဲ့ စကာား ံိုား တစ် ံိုားရမယ်။ အဒြီထကို ထညဲ့်ခ င်တဲ့ စာသာား ထညဲ့်ထာားမယ်။ အဒြီ word
Works => work, Goes => go, Flies => fly, Worked => work, Working => work
ဆို ိုတာက s, es, ies, ing, ed ဆံိုားခဲ့ရင် ြြိုတ်တာ ြစ်ပါတယ်။ ဆံိုားခဲ့ရင် ဆိုတတာဲ့ conditional
statement သံိုားြို့ို ိုပါတယ်။ ies ဆိုရင် ies ကို ြြိုတ်ပပြီား y ထညဲ့်တပားရပါတယ်။ ပံမ
ို မ
် ဟိုတတ
် ဲ့
အဒြီ algorithm ဟာ word ဆိုတဲ့ string ကို parameter အ ြစ် က်ခံမာ ြစ်ပပြီား ြြိုတ်စရာရတာ
မူရင်ား string ထက သူရာခ င်တဲ့ string ကို ရာပပြီား တတွေွေ့ရာတ ရာ အကို ် ြ က်မာ ြစ်ပါတယ်။
1. deleteEveryOccurrence(Text, str)
2. I = INDEX(Text, str)
3. Repeat while I!=-1:
4. Text = DELETE(Text, I, LENGTH(str))
5. I = INDEX(Text, str)
6. End of Loop
7. Return Text
161 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ထံိုားစံအတင
ို ်ား trace ိုက်ပါမယ်။
1 ABCDABEFABGH AB
2 0 I = INDEX (Text,Str) = 0
ိုပ်မယ်။
Text = DELETE(“CDABEFABGH”, 0, 2)
Text= CDABEFABGH
5 2 I = INDEX(Text, str)
I = INDEX(“CDABEFABGH”, “AB”)
I = 2 ရပါတယ်။ အမ က
် while condition မာ
စစ်ထာားတအ
ဲ့ တွေက် AB ကိုတတွေွေ့တဲ့ index (i) ကို
ိုပ်မယ်။
162 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Text = DELETE(“CDABEFABGH”, 2, 2)
Text= CDEFABGH
5 4 I = INDEX(Text, str)
I = INDEX(“CDEFABGH”, “AB”)
I = 4 ရပါတယ်။ အမ က
် while condition မာ
တ ပာတအ
ဲ့ တွေက် AB ကိုတတွေွေ့တဲ့ index (i) ကို
ိုပ်မယ်။
Text = DELETE(“CDEFABGH”, 4, 2)
Text= CDEFGH
5 -1 I = INDEX(Text, str)
I = INDEX(“CDEFGH”, “AB”)
163 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
7 ကို ိုပ်မယ်။
အတ ြ ပ ်ပိုို့ ိုက်ပါတယ်။
Pattern Matching
Text ထကတ str ကို ိုက်ရာရင် ဘယ် ိုရာ တစ်ခ က် ကညဲ့် ကညဲ့်ရတအာင်။
Text = “abcdefg”
str = “dez”
ိုို့ ဆိုခဲ့ရင် ရာခ င်တဲ့ str ဟာ length 3 ရပါတယ်။ သူူ့ရွေ့ တိုက်တဲ့ ပံတ
ို ား တစ်ခ က်
ကညဲ့် ကညဲ့်ရတအာင်
အတင
ို ်ား ၃ ံိုား ြတ်ယူတယ်။ တရွေ့ဆံိုား character 2
164 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
တက
ို ် ိုက်ရတယ်။
တက
ို ် ိုက်ရတယ်။
တူတတာဲ့ ဆက်တိုက်မယ်။
တူတတာဲ့ ဆက်တိုက်မယ်။
3 ကကမ်တိုက် ိုက်ရတယ်။
တက
ို ် ိုက်ရတယ်။
165 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခ ်ား 5 ကစပပြီား ယူမယ်ဆိုရင် Text မာ ၂ ံိုားပ က ်တယ်။ ဒြီတတာဲ့ length 3 ရတဲ့ str ို့ တူစရာ
တပါင်ား ကညဲ့်ရတအာင်
1 + 1 + 1+ 3 + 1 = 7 ကကမ်တက
ို ်ပပြီားမ မတတွေွေ့ဘူား ဆိုတဲ့ အတ ြထွေက်ပါတယ်။
Text = “ababaaba…..”
Str = “aaba”
တက
ို ်တဲ့ အကကမ် စိုစိုတပါင်ား = 2 + 1 + 2 + 1 + 4 = 10 ြစ်ပါတယ်။ Text ရွေ့ အခ ်ား ံပါတ် 4 ကတ
တက
ို တဲေ့ အကကမအလရအတွက တွကကကညေ့ကကရလအာင။
တရားတာ ြစ်ပါတယ်။)
တရားတာ ြစ်ပါတယ်။)
166 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
မူရင်ား string Text ထက သူရာခ င်တဲ့ string, str ဘယ်နစ်ကကမ်ပါ တရတွေက်မာ ြစ်ပါတယ်။
1. countOccurrence(Text, str)
2. Set count = 0
3. Text = toUpper(Text) /*can also use toLower(Text)
4. str = toUpper(str)
5. Set I = INDEX(Text, str,0)
6. Repeat while I!=-1:
7. count = count + 1
8. I = INDEX(Text, str, I + 1)
9. End of Loop
10. Return count
ထံိုားစံအတင
ို ်ား trace ိုက်ပါမယ်။
Time = countOccurrence(“Life is beauty, admire it. Life is a dream, realize it. Life is a
1 Text = “Life is beauty, admire it. Life is a dream, realize it. Life is a
str = “life”
2 count = 0 0
167 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
3 Text = toUpper(Text)
str တူသွောားြို့ပ
ို ပ
ို ါတယ်။ တစ်ခိုကို အကကြီားတ ပာင်ားရင် က ်တစ်ခို ည်ား
သတ
ိုို့ သာ် တစ်ခိုက အတသား၊ တစ်ခိုက အကကြီားဆို မတူဘူား ရာမတတွေွေ့ဘူား
ြစ်သွောားပါမယ်။
4 str = toUpper(str)
6 I != -1 → 0 != -1 ဆိုတတာဲ့ condition မ တ
် အ
ဲ့ တွေက် while ရွေ့ body ကို
အ ိုပ် ိုပပ
် ါမယ်။
7 Line 6 မာ index (I) ဟာ -1 ို့ မညြီ ိုို့ ဝင် ာတာ ြစ် ိုို့ 1
တစ်ကကမ်တတွေွေ့တအ
ဲ့ တွေက် count ကို ၁ တိုားပါတယ်။
count = count + 1
168 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒါက while loop ရွေ့ update ြစ်ပါတယ်။ ဘာတ ကာငဲ့် ဆိုရင် while loop
6 I != -1 → 27 != -1 ဆိုတတာဲ့ condition မ တ
် အ
ဲ့ တွေက် while ရွေ့ body ကို
အ ိုပ် ိုပပ
် ါမယ်။
7 Line 6 မာ index (I) ဟာ -1 ို့ မညြီ ိုို့ ဝင် ာတာ ြစ် ိုို့ 2
တစ်ကကမ်တတွေွေ့တအ
ဲ့ တွေက် count ကို ၁ တိုားပါတယ်။
count = count + 1
ဒါက while loop ရွေ့ update ြစ်ပါတယ်။ ဘာတ ကာငဲ့် ဆိုရင် while loop
6 I != -1 → 56 != -1 ဆိုတတာဲ့ condition မ တ
် အ
ဲ့ တွေက် while ရွေ့ body ကို
အ ိုပ် ိုပပ
် ါမယ်။
7 Line 6 မာ index (I) ဟာ -1 ို့ မညြီ ိုို့ ဝင် ာတာ ြစ် ိုို့ 3
တစ်ကကမ်တတွေွေ့တအ
ဲ့ တွေက် count ကို ၁ တိုားပါတယ်။
count = count + 1
169 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒါက while loop ရွေ့ update ြစ်ပါတယ်။ ဘာတ ကာငဲ့် ဆိုရင် while loop
170 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၂)
Array ဆိုတာ
အရင် တုန််းကတတ ော့ array ဆုတ တူညီတော့ data type တတွေ စုထ ်းတ ကု array
လုို့တ ေါ်ပါတယ်။ ဆုလုတ က integer array ဆု integer တတွေ ျည််း တန််းစီပပီ်း သမ််းလုို့ရတယ်။ array
က အ န််း 5 န််းယူထ ်းရင် integer 5 လု်း သမ််းလုို့ရတယ်တပါ ော့။ array က အ န််း 100 ယူထ ်းရင်
integer အလု်း 100 သမ််းလုို့ရတယ်တပါ ော့။ ဒီလုပါပ double array ဆု double တတွေ ျည််း
ဒါတ က ငော့်လည််း string ဟ character တတွေ ျည််း တန််းစီပပီ်း သမ််းတ ဖြစ်လုို့ string ကု character
သတ
ုို့ သ ် ဒီတနို့တ တ်မ တတ ော့ array ဟ တူညီတော့ data type တတွေပ သမ််းလုို့ရတ
မဟုတတ
် တ ော့ပ မတူညီတော့ data type တတွေကုပါ စုစည််း သမ််းဆည််းလုို့ရတနပါပပီ။ ဘယ်လုပ ရတနရတန
array ကုတတ ော့ ဒီတနို့အ ျန်ထ တူညီတော့ data type တတွေ သမ််းဆည််းြို့ု အတွေက် အသု်းဖပြု ကတ
မျ ်းပါတယ်။
ပပီ်းတတ ော့ Java လုမျြု်း programming language တတွေမ array အ န််းနပါတ် index ဟ
ကုယက် ကြုက်တ ထ ်းလုို့ရတယ်ဆုတ တရျို့မ တဖပ ော့ပပီ်းပါပပီ။ ဒါတ က ငော့် array ရျို့ length (array
171 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Length = UB – LB + 1
0 Ko Ko
1 Ko Let
2 Nyi Nyi
UB = 2, LB = 0
Length = UB – LB + 1 = 2 – 0 + 1 = 3
5 Aye Aye
6 Bo Bo
… ….
87 Zaw Zaw
UB = 87, LB = 5
Length = UB – LB + 1 = 87 – 5 + 1 = 83
အ ု နမူနာ ဖပထ ်းတော့ array ၂ ုလု်းဟ စ သ ်းတတွေသမ််းထ ်းတ ဖြစ်လုို့ String data type array
တတွေပဖြစ်ပါတယ်။
172 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Linear Array
ဘယ်လုတွေက် ျက် လုပ်ကုင်သွေ ်းသလဆုတ တလ်း တစ် ျက် ကညော့် ကညော့် ကရတအ င်။
address လုို့ တဖပ လုို့ရပါတယ်။ Memory ဆုတ လည််း အဒီလုပါပ။ တရျို့မ တနရ ယူထ ်းတ
Memory တပေါ်က လွေတ်တော့တနရ ကတန စပပီ်း တနရ ယူရတယ်၊ အဒါ array ရျို့ base address ပါပ။
W - ကတတ ော့ number of bytes ပါ။ data type တစ် ုမ ဘယ်တလ က် byte တနရ ယူမလဆုတ
language က ကကြုတင် သတ်မတ်ထ ်းတ ရပါတယ်။ Java မ ဆုရင် char က 1 byte, int က 4
i – index အ န််းနပါတ်ဖြစ်ပါတယ်။
int arr[]={10,20,30,40,50};
173 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒါကတတ ော့ Java နည််းနို့ array တဆ က်ဖပထ ်းတ ဖြစ်ပါတယ်။ int data type ပါ။ array name or
0 10 1000
1 20 1004
2 30 1008
3 40 1012
4 50 1016
ပမ
ု 0,1,2,3,4 က ကျွန်မတို့ု ယူသု်းမညော့် အ န်နပါတ် index, 1000,1004 စတ တတွေဟ တကယော့်
memory address တတွေဖြစ်ပါတယ်။ 1000 ဟ base address ဖြစ်ပါမယ်။ int data type ဖြစ်လုို့
174 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Multi-Dimensional Array
Multi-dimensional array ထကမ Row နို့ Column ဆုတော့ Index 2 ု ရတော့ two-dimensional array
တစ် ျက် ကညော့် ကညော့် ကရတအ င်။ အဒီမ ၂ မျြု်း ရပါတယ်။ Column ကုဦ်းစ ်းတပ်းတွေက်တော့ column-
တတ ်တတ ်မျ ်းမျ ်းကတတ ော့ row ကုဦ်းစ ်းတပ်းတော့ row-major တတွေပဖြစ်ပါတယ်။
Elements Elements
(0,0) (0,0)
(1,0) Column0 (0,1) Row 0
(2,0) (0,2)
(0,1) (1,0)
(1,1) Column1 (1,1) Row1
(2,1) (1,2)
(0,2) (2,0)
(1,2) Column2 (2,1) Row2
(2,2) (2,2)
ပထမပုကတတ ော့ column-major တနရ ယူသွေ ်းတ ဖပတော့ပု ဖြစ်ပပီ်း၊ ဒုတယပုကတတ ော့ row-major
Column-major
Row-major
175 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ရတော့ column အတရအတွေက် ဖြစ်ပါတယ်။ Lc ဆုတ column ရျို့ lower bound, Lr ဆုတ row ရျို့
0 1 2 3 4
0 10 20 30 40 50
1 60 70 80 90 100
Row-major နို့ ပ တဖပ ကညော့် ကရတအ င်။ array အမည်ကု A လုို့ပထ ်းပါတတ ော့။ int data type array
ဖြစ်တယ်။ int ဖြစ်လုို့ တစ် န််းကု 4 bytes စီတနရ ယူမယ်။ row 3 ု colum 4 ုဖြစ်လုို့ 3*4 array လုို့
တဖပ လုို့ရပါတယ်။ Base address က 500 ဆုပါတတ ော့။ ဒါဆုရင် row-major နို့ 140 ဆုတော့ value ရျို့
memory address ကု တွေက် ကညော့်ရတအ င်။ 140 ဆုတတ ော့ A[2][3] ဖြစ်ပါတယ်။
0 1 2 3 4
176 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သွေ ်းတ ဖြစ်ပါတယ်။ တရ က်တော့ အ ျန်မ တတ ော့ output ထုတ်တ ဖြစ်ဖြစ်၊ တွေက်တ ဖြစ်ဖြစ် မမ
နစ်သက်ရ လုပ်လရ
ုို့ ပါတယ်။
0 Ko Ko 3 CST
1 Ko Let 4 CS
2 Nyi Nyi 5 CT
နမူနာ ဖပထ ်းတော့ array 2 ုကု ကညော့် ကညော့်ပါ။ ပထမ array က အ န််းနပါတ် 0 ကတန 2
ထသွေ ်းတယ်။ ဒုတယ array က အ န််းနို့ပါတ် 3 ကတန 5 ထသွေ ်းတယ်။ ဘယ်လုပသွေ ်းသွေ ်း
ခ ြုပပီ်းတဖပ ရရင် lower bound ကတန upper bound ထ သွေ ်းမယ်ဆုရင် အ န််းတပါက်တစော့
တရ က်မ ဖြစ်ပါတယ်။
177 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1. Set index = LB
2. Repeat while index <= UB:
3. Do something with LA[index]
4. Index = index + 1
5. End of Loop
ဒါက while loop နို့ တရ်းတ ဖြစ်ပပီ်း for loop နို့ ထပ်ပပီ်း တရ်းပါ ော့မယ်။
ဒီတနရ ထ ြတ်လ ပပီဆုတတ ော့ ဒီ traversing algorithm တလ က်ကတတ ော့ ကတလ်းကစ ်းသတလ က်
178 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အ န််းနပါတ်က 2000 ကစပပီ်း 2021 အထ သွေ ်းပါတယ်။ အထမ တတ ော့ value အတနနို့ သူူ့ရျို့
2. အတပေါ်က traversing algorithm ကု အသု်းဖပြုပပီ်း 2000 ကတန 2021 အထ၊ ုနစ် နငော့် ဝင်တငွေကု
algorithm ကုတရ်းပါ။
0 Apple 0 Apple
1 Banana 1 Banana
အ န််းနပါတ် 2
2 3
mango တနရ မ 2 Mango 2 Lemon
3
Lemon ကု
3 Pineapple 1 3 Mango
လ ထညော့် ျင်တ
ဖြစ်ပါတယ် 4 Pineapple
179 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
စစ ျင််း 0 to 3 ဆုပပီ်း array န််း 4 န််းရပါတယ်။ အဒီထက 2 အ န််း Mango ရျို့ တနရ ကု
တနာက်အ န််းတတွေဟ တအ က်ကု တစ် န််းစီ ဆင််းတပ်းရမ ဖြစ်ပါတယ်။ အဒီမ Mango ကု အရင်
တပ်းဆင််း ော့ရင် Pineapple တနရ မ Mango ဝင်သွေ ်းတတ ော့ overwrite ဖြစ်ပပီ်း Pineapple
တပျ က်သွေ ်းမ ဖြစ်ပါတယ်။ ဒါတ က ငော့် တရျို့မယ် ဆုရင် တနာက်ဆု်းအ န််းကတန စတရျို့ရပါတယ်။
ပမ
ု ဖပထ ်းတအ
ော့ တင
ု ််းဆု Pineapple အရင်တရျို့၊ ပပီ်းမ Pineapple တနရ ကု Mango တရျို့
ထညော့် ျင်တော့ န််းလွေတ်သွေ ်းပပီဖြစ်လုို့ အဒီ အ န််းမ ထညော့် ျင်တော့ Lemon ကု ထညော့်လုက်ပါတယ်။
ျင််းစီ ဆင််းတပ်းရပါမယ်။
180 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒါက while loop နို့ တရ်းတ ဖြစ်ပပီ်း for loop နို့ကတတ ော့ စ ြတ်တနသူ အတရ်းကျငော့်ရမ ဖြစ်ပါတယ်။
မသွေ ်းပါနို့အု်း။ ဘယ်လု ဖပဖပ အတူတူပါပ။ 2 အ န််းတနရ မ Charles ကု ထညော့် ျင်တယ် ဆုပါတတ ော့
David, Emely,
Ford
3 4 index = UB
181 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
6 3 index = Index -1
လုပ်တ ဖြစ်ပါတယ်။
ကသ
ု ွေ ်းပါမယ်။
6 2 index = index -1
လုပ်တ ဖြစ်ပါတယ်။
ကုသွေ ်းပါမယ်။
182 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
6 1 index = Index -1
လုပ်တ ဖြစ်ပါတယ်။
ကုသွေ ်းပါမယ်။
မ ်းသွေ ်းပပီ ဖြစ်လုို့ end of loop ရျို့ အဖပင် line 8 ကု သွေ ်းမ
ဖြစ်ပါတယ်။
Trace ိုကကကညပါ။
LA => {AA, BB, DD, EE, FF, GG} ထကု CC ကု ထညော့် ျင်ပါတယ်။ ငယ်စဉ်ကကီ်း လုက် စီပပီ်းသ ်း
183 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Linear Array ရျို့ ကုယက် ကြုက်တော့ တနရ က value ကုြျက်တ တလ်း စဉ််းစ ်း ကညော့် ကရတအ င်။
0 Apple 0 Apple
1 Banana 1 Banana
2 Lemon 2 Mango
1
အ န််းနပါတ် 2
3 Mango 3 Pineapple
Lemon ကု
ြျက် ျင်တ 4 Pineapple 2 4
3
ဖြစ်ပါတယ်။
စစ ျင််း 0 to 4 ဆုပပီ်း array န််း 4 န််းရပါတယ်။ အဒီထက 2 အ န််း Lemon ကု ြျက် ျင်တ
ဝင်လ ရမ ဖြစ်ပါတယ်။
ပတ
ု လ်းမ နပါတ်တလ်းတတွေတပ်ဖပထ ်းပါတယ်။ ကညော့် ကညော့်ပါ။
ဖြစ်ပပီ်း၊ delete ကတတ ော့ ြျက်တ ဖြစ်လုို့ ြျက် ျင်တော့အ န််းနပါတ် + ၁ ကတနပပီ်း တနာက်ဆု်း
184 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
တ်းု လ တ (UB+1) ဖြစ်ပပီ်း၊ delete ကတတ ော့ အ န််းတစ် န််းတလျ ော့သွေ ်းတ ဖြစ်လုို့ အလုပ်လုပတ
်
သြုို့ လုတ တတွေက array – LA, ြျက် ျင်တော့ အ န််းနပါတ် - DI, တနာက်ဆု်းအ န််းနပါတ် UB
ရယ် ဖြစ်ပါတယ်။
ဒါက while loop နို့ တရ်းတ ဖြစ်ပပီ်း for loop နို့ကတတ ော့ ြတ်တနသူ အတရ်းကျငော့်ရမ ဖြစ်ပါတယ်။
0 1 2 3 4
185 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
3 2 index = DI = 2
4 index<UB
6 3 index = index + 1
4 index< UB
6 4 index = index + 1
4 index< UB
186 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သွေ ်းပါမယ်။
column မ ကညော့်ပါ။
ထ တရျို့ရမ ဖြစ်ပါတယ်။ အဒီမ Set index = DI လုို့ တရ်းလုက်တတ ော့ Index ဟ အလုပ်လုပရ
် မညော့်
တစခန်းခ ျာပပ်း <UB (UB ထကငယတအထ) ဆိုပပ်း ိုပထာ်းတာ ြစပါတယ။ ဒါမှ Index + 1 ဟာ
LA[index] = LA [index+1]
Index = index + 1
End of Loop
187 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သူဟ သူူ့ရျို့ အတရျို့တစ် န််းထကု ထညော့်တပ်းရမ ဖြစ်ပါတယ်။ ဒါတ က ငော့် ြျက်မညော့်အ န််းရျို့
Set Index = DI + 1
LA[index - 1] = LA [index]
Index = index + 1
End of Loop
ဆုပပီ်း ဖြစ်သွေ ်းမ ဖြစ်ပါတယ်။ တဖပ ျင်တ က ယ ုတရ်းလုက်တ နို့ algorithm ထမ တရ်းထ ်းတ နို့
တနာက်အ န််းယူတ တလ်း ကွေသွေ ်းတ ဖြစ်ပါတယ်။ ဒါတ က ငော့် problem တစ် ုမ တဖြရင််းနည််း
မျြု်းစုရတယ်တဖပ တ ဖြစ်ပါတယ်။
ခအာကပါခတွေကို ခ ကျငကကညပါ။
1. Index ကု အလုပ်လုပမ
် ညော့်အ န််းထက တနာက်အ န််းကု ယူတော့ နည််းနို့ algorithm ကု
အစအဆု်း ဖပန်တရ်းတပ်းပါ။
2. နပါတ် ၁ မ ဖပန်တရ်းထ ်းတော့ algorithm ဖြငော့် LA => {AA, BB, CC, DD, EE, FF, GG} ထမ CC
188 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
တတွေျို့တအ
ော့ န််းနပါတ်ကု return ဖပန်တပ်းမ ဖြစ်ပါတယ်။ တနာက်ဆု်းအ န််းထ ရ မတတွေျို့ ော့ဘူ်း
1. linearSearch(LA, N, x)
2. /*LA – linear array, N – number of elements in array, x – value to search */
3. Set index = 0
4. Set found = -1
5. Repeat while index < N:
6. If LA[index] == x then:
7. Found = index
8. Index = index + 1
9. End of Loop
10. Return found
လုို့တ ေါ်တော့ array အ န််းအတရအတွေက်ကု လက် ထ ်းတ ဖြစ်ပါတယ်။ UB(upper bound) ဆုတ
189 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1,2,3,4 0 -1 Line 1 => LA => {10, 20, 30, 40, 50, 60}, X = 10, N = 6
Line 2 => comment ဆုတတ ော့ အလုပ်မလုပ်
Line 3 and 4 => index =0, found = -1
5 index < N ဆုတတ ော့ 0 < 6 ဆုတတ ော့မန်တယ်။ while condition မန်တတ ော့
while body ကု အလုပ်လုပပ
် ါမယ်။
8 1 index = index + 1
index = 0 + 1 = 1
0 န််းပပီ်း 1 အ န််းရ မ ဖြစ်လုို့ အ န််းနပါတ်ကု 1 တ်းု တ update
လုပ်တ ဖြစ်ပါတယ်။
5 index < N ဆုတတ ော့ 1 < 6 ဆုတတ ော့မန်တယ်။ while condition မန်တတ ော့
while body ကု အလုပ်လုပပ
် ါမယ်။
190 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
8 2 index = index + 1
index = 1 + 1 = 2
1 န််းပပီ်း 2 အ န််းရ မ ဖြစ်လုို့ အ န််းနပါတ်ကု 1 တ်းု တ update
လုပ်တ ဖြစ်ပါတယ်။
5 index < N ဆုတတ ော့ 2 < 6 ဆုတတ ော့မန်တယ်။ while condition မန်တတ ော့
while body ကု အလုပ်လုပပ
် ါမယ်။
8 3 index = index + 1
index = 2 + 1 = 3
2 န််းပပီ်း 3 အ န််းရ မ ဖြစ်လုို့ အ န််းနပါတ်ကု 1 တ်းု တ update
လုပ်တ ဖြစ်ပါတယ်။
5 index < N ဆုတတ ော့ 3 < 6 ဆုတတ ော့မန်တယ်။ while condition မန်တတ ော့
while body ကု အလုပ်လုပပ
် ါမယ်။
8 4 index = index + 1
index = 3 + 1 = 4
3 န််းပပီ်း 4 အ န််းရ မ ဖြစ်လုို့ အ န််းနပါတ်ကု 1 တ်းု တ update
လုပ်တ ဖြစ်ပါတယ်။
191 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
5 index < N ဆုတတ ော့ 4 < 6 ဆုတတ ော့မန်တယ်။ while condition မန်တတ ော့
while body ကု အလုပ်လုပပ
် ါမယ်။
8 4 index = index + 1
index = 4 + 1 = 5
4 န််းပပီ်း 5 အ န််းရ မ ဖြစ်လုို့ အ န််းနပါတ်ကု 1 တ်းု တ update
လုပ်တ ဖြစ်ပါတယ်။
5 index < N ဆုတတ ော့ 5 < 6 ဆုတတ ော့မန်တယ်။ while condition မန်တတ ော့
while body ကု အလုပ်လုပပ
် ါမယ်။
8 6 index = index + 1
index = 5 + 1 = 6
5 န််းပပီ်း 6 အ န််းရ မ ဖြစ်လုို့ အ န််းနပါတ်ကု 1 တ်းု တ update
လုပ်တ ဖြစ်ပါတယ်။
5 index < N ဆုတတ ော့ 6 < 6 ဆုတတ ော့ condition မ ်းသွေ ်းပပီဖြစ်လုို့ looping
ထက ထွေက်မ ဖြစ်လုို့ end of loop ရျို့တနာက် တစ်တ က င််း line 10 ကု
သွေ ်းအလုပ်လုပမ
် ဖြစ်ပါတယ်။
192 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
LA => {10, 20, 30, 40, 50, 60} ထက X = 10 ကုရ တ ပထမဆု်းအ န််း (0 အ န််း) မ တင် တတွေျို့ရျို့
နို့ တနာက်ဆု်းအ န််းအထ အ ျန်ကုန် ပပီ်းလုက် စစ်တနတ ဖြစ်ပါတယ်။ တကယ်က တတွေျို့ရင် ဆက်ပပီ်း
စစ်စရ မလုအပ်တတ ော့ပါဘူ်း။ ဒီတတ ော့ အ ျန်ကုန်သက်သ ရတအ င် တတွေျို့တ နို့ ဆက်မစစ်ပ တန််းပပီ်း
ပနပါ။ ဖပန်တရ်းဖပရရင်
1. linearSearch(LA, N, x)
2. Set index = 0
3. Repeat while index < N:
4. If LA[index] == x then:
5. Return index
6. Index = index + 1
7. End of Loop
8. Return -1
Line 8 ကုတရ က်လ တယ်ဆုတ အတပေါ်က line 4 မမန် ော့လ၊ုို့ တတွေျို့ ော့တ မရ ော့လုို့ ဖြစ်ပါတယ်။
line 4 မ စစ်ထ ်းသလု ညီတ သ တတွေျို့ ော့ရင် line 5 က return ဖပန်ပပီ်း function ထက
ထွေက်သွေ ်းပပီ ဖြစ်ပါတယ်။ ဒါတ က ငော့် Line 8 ကုတရ က်လ တ ညီတ မတတွေျို့ဘူ်းဆုတော့
193 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ငယ်စဉ်ကကီ်းလုက် သမ
ုို့ ဟုတ် ကကီ်းစဉ်ငယ်လုက် sorting စီပပီ်းသ ်း array မ မ Binary Search
တစခိုနဲ့ တစခို တက
ို စစခရင ြစနင
ို ခ ခ ၃ ခိုပရှပါတယ။ ငယတာရယ၊ ညတာရယ နဲ့ ကက်းတာရယ
ြစပါတယ။
1. ညီ ော့ ရင် Mid အ န််းမ တတွေျို့တ ဖြစ်လုို့ တတွေျို့တော့ အ န််းနပါတ်ဖြစ်တော့ Mid ကု return
ဖပန်ပါ ော့မယ်။
AA BB CC DD JJ KK LL XX YY ZZ
0 1 2 3 4 5 6 7 8 9
LB Mid UB
194 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လုပ်ြမ
ုို့ လုပါဘူ်း။ ဒါတ က ငော့် တရျို့တစ်ဖ မ််းမ မပါတော့ UB ကို Mid ရှေ့ ခရှှေ့တစခန်း Mid-1 ကို
ခရှေ့ပါမယ။
လုပ်ြမ
ုို့ လုပါဘူ်း။ ဒါတ က ငော့် တနာက်တစ်ဖ မ််းမ မပါတော့ LB ကို Mid ရှေ့ ခနောကတစခန်း Mid+1
ကို ခရှေ့ပါမယ။
AA BB CC DD JJ KK LL XX YY ZZ
0 1 2 3 4 5 6 7 8 9
LB =0, UB = 9
195 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒီတတ ော့ LA[Mid], LA[4] နို့ x ကု တုက်စစ်တယ်။ JJ နို့ CC ဆုတတ ော့ user ရ ျင်တော့ CC က
ငယ်တနတယ်။ ဒီတတ ော့ တရျို့တစ်ဖ မ််းကု ရ မယ်။ တရျို့တစ်ဖ မ််းဆုတတ ော့ UB ကု Mid -1 ကု တရျို့မယ်။
UB = Mid – 1 = 3
ခသ်း ်းူ ။
LB = Mid + 1 = 1+ 1 =2
ဒီတတ ော့ LA[Mid], LA[2] နို့ x ကု တုက်စစ်တယ်။ CC နို့ CC ဆုတတ ော့ ညီသွေ ်းပပီ၊ တတွေျို့သွေ ်းပပီ ဖြစ်လုို့
196 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
တရ်းသ ်းတပ်းထ ်းတ ဖြစ်ပါတယ်။ ဒါတ က ငော့် အဒီ algorithm ကု ကကီ်းစဉ်ငယ်လုက်စီထ ်းတော့
4. တဖပ င််းတရ်းထ ်းတော့ algorithm ဖြငော့် LA => {555, 444, 333, 222, 111, 99, 88, 77, 66, 55, 44,
197 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Minimum
တနာက်တစ် န််း( အ န််းနပါတ် ၁) ကတန တနာက်ဆု်းအ န်းထ တစ် န််းပပီ်း တစ် န််း min တန်ြု်းနို့
တက
ု ်စစ်ပါ။ min ဟ ငယ်ရမ ဖြစ်ပါတယ်။ min က ကကီ်းတနရင် ငယ်တော့ တက င်နို့ တဖပ င််းထညော့်ပါ။
1. findMin(LA, UB)
2. Set min = LA [0]
3. Repeat for index = 1 to UB by 1:
4. If min > LA[index] then:
5. min = LA[index]
6. End of Loop
7. Return min
198 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
7 Return min ဆုတတ ော့ return 5 ဆုပပီ်း တ ေါ်တော့ တနရ ဆီကု return
ဖပန်တပ်းသွေ ်းမ ဖြစ်ပါတယ်။
199 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Min ဆုတတ ော့ ငယ်ရမ ၊ ဒါတ က ငော့် min ကကကီ်းတန ော့ရင် တဖပ င််းထညော့်တ ဖြစ်ပါတယ်။ Maximum မ
ကျန်တ အတူတူပါပ။ Maximum ဆုတတ ော့ ကကီ်းရမ ဖြစ်လုို့ ငယ်ရင် တဖပ င််းထညော့်ရမ ဖြစ်ပါတယ်။
ခ ကျင ကကညပါ။
2. LA => {10, 70, 35, 77, 6, 600,4} ထမ Maximum ကု Question 1 ရျို့ findMaximum
200 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
“ပထမနစ်တက်တုန််းက
စ တမ်းပွေနာ်းနီ်းတတ ော့မ
တမ်းလုကတ
် က
‘Trace လုကတ
် ယ် ဆုတ ဘ တုန််းတော့’
အတတွေ်းက အဒီလုလွေန် ကတ ၊
အဒီလု ဖြစ်တနမယ်။”
201 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၃)
Sorting Algorithms
Swap
Sorting algorithm တ အ
ွေ တ ြောင်း မတ ြောခငမြော sorting စီ တ
ဲ့ ေရြောမြော သိ ိိုသိဖလ
ိုို့ိ ိိုအ ဲ့
ဆိိုလိို ြော ဒီဘ ေ်း ေထ အသီ်းတ ွေ ိို ဟိိုဖ ေ်း ေထ ထညဲ့ပ ီ်း၊ ဟိိုဘ ေ်း ေထ
ရ ါ ယ။
202 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ ဒါဆိို အသီ်း ေ်း ေလွေ သွေြော်းပ ီဆိိုတ ြောဲ့ အသီ်း ေ်း ေထ ိို အြောလူ်းတ ြောစ ဲ့အစြော်းအစြောတ ွေ
လြောထညဲ့ ါမယ။
▪ အြောလူ်းတ ြောစ ဲ့အစြော်းအစြော ေ်း ေ အခိုလွေ သွေြော်းပ ီ ဆိိုတ ြောဲ့မ ေ်း ေအလွေ ထ ထညဲ့ထြော်း ဲ့
အသီ်းတ ွေ လြောထညဲ့မြော ဖစ ါ ယ။
ဘယနစခါ လို လိို ရသလဆိိုရင ၃ ခါ လို လိို ရ ါ ယ။ အဒီလိို ါ LA ရဲ့ j အခေ်းေို့ k အခေ်း ထ
Swap ( j, k)
1. Temp = LA [ j ]
2. LA [ j ] = LA [ k ]
3. LA [ k ] = Temp
ထညဲ့လိို တ ြောဲ့ j အခေ်းလွေ သွေြော်း ါ ယ။ ဒီတ ြောဲ့မ j အခေ်းထ ိို k အခေ်းထ ေဖိို်း ထညဲ့ ါ ယ။
တေော ဆို်းမြောတ ြောဲ့ လွေ သွေြော်း ဲ့ k အခေ်းထ ိို Temp ထ ဟြော ေထညဲ့လိို ြောတ ြောငဲ့ Temp ထ
10 20 30 40 50
0 1 2 3 4
Let j = 1, k=3
Line 1: Temp = LA [ j ] = LA [ 1 ] = 20
Line 2: LA [ j ] = LA [ k ]
LA [ 1] = LA [3]
LA[1] = 40
Line 3: LA [ k ] = Temp
LA [3] = 20
10 40 30 20 50
0 1 2 3 4
203 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Bubble Sort
Bubble sort ရဲ့ အလို လို ို လျ နစခေ်း ိို ိို စစမြော ဖစ ါ ယ။ ထမဆို်း 0
သူရဲှေ့တနောကမှာ က
ို စရှာ မရတ ှာဲ့လိုို့ ဖြစပါ ယ။ ဒါတ ကှာငဲ့ ပထမအကကမမှာ စခန်း
တလ ှာဲ့ပပီ်း က
ို စစမှာ ဖြစပါ ယ။
44, 33, 11, 55, 77, 90, 10 ိို ငယစဉက ီ်းလိို စီ ညဲ့လိို ရတအြောင ါ။
No. 1 - တနောကအခန်းနဲို့ က
ို မှာမို့ို စခန်းတလ ှာဲ့ က
ို မယ။
44, 33, 11, 55, 77, 90, 10 0 အခေ်း ေို့ 1 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတ ြောဲ့ swap
လို မယ။
33, 44, 11, 55, 77, 90, 10 1 အခေ်း ေို့ 2 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတ ြောဲ့ swap
လို မယ။
33, 11, 44, 55, 77, 90, 10 2 အခေ်း ေို့ 3 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတေတ ြောဲ့
33, 11, 44, 55, 77, 90, 10 3 အခေ်း ေို့ 4 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတေတ ြောဲ့
204 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
33, 11, 44, 55, 77, 90, 10 4 အခေ်း ေို့ 5 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတေတ ြောဲ့
33, 11, 44, 55, 77, 90, 10 5 အခေ်း ေို့ 6 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတေတ ြောဲ့ swap
လို မြော ဖစ ါ ယ။
အက ီ်းဆို်း ေ
ိ ်း စလို်း ဖစ ဲ့ 90 ဟြော တေော ဆို်း ိို တရြော သွေြော်းပ ီ ဖစ ါ ယ။ ဒါတ ကှာငဲ့ ဒို ယ
အကကမမှာ 90 အခန်းကို ထညဲ့ ိုကစရှာမလိုတ ှာဲ့ပါဘူ်း။ ဒီတ ှာဲ့ ဒို ယ အကကမမှာ နစခန်း တလ ှာဲ့ပပီ်း
က
ို စစမှာဖြစပါ ယ။
33, 11, 44, 55, 77, 10, 90 0 အခေ်း ေို့ 1 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတ ြောဲ့ swap
လို မယ။
11, 33, 44, 55, 77, 10, 90 1 အခေ်း ေို့ 2 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတ ြောဲ့ swap
11, 33, 44, 55, 77, 10, 90 2 အခေ်း ေို့ 3 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတေတ ြောဲ့
205 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
11, 33, 44, 55, 77, 10, 90 3 အခေ်း ေို့ 4 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတေတ ြောဲ့
11, 33, 44, 55, 77, 10, 90 4 အခေ်း ေို့ 5 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတ ြောဲ့ swap
လို မယ။
11, 33, 44, 55, 10, 77, 90 0 အခေ်း ေို့ 1 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတ ြောဲ့ swap
11, 33, 44, 55, 10, 77, 90 1 အခေ်း ေို့ 2 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတ ြောဲ့ swap
11, 33, 44, 55, 10, 77, 90 2 အခေ်း ေို့ 3 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတေတ ြောဲ့
11, 33, 44, 55, 10, 77, 90 3 အခေ်း ေို့ 4 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတ ြောဲ့ swap
လို မယ။
206 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ယအကကမ က
ို လဲပပီ်းသွေှာ်းတရှာ အကကီ်းဆို်းကန်း 3 လို်းတနောကဆ်းို တရှာကသွေှာ်းပပီ ဖြစပါ ယ။
11, 33, 44, 10, 55, 77, 90 0 အခေ်း ေို့ 1 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတ ြောဲ့ swap
11, 33, 44, 10, 55, 77, 90 1 အခေ်း ေို့ 2 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတ ြောဲ့ swap
11, 33, 44, 10, 55, 77, 90 2 အခေ်း ေို့ 3 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတေတ ြောဲ့ swap
လို မယ။
ယအကကမ က
ို လဲပပီ်းသွေှာ်းတရှာ အကကီ်းဆို်းကန်း 4 လို်းတနောကဆ်းို တရှာကသွေှာ်းပပီ ဖြစပါ ယ။
11, 33, 10, 44, 55, 77, 90 0 အခေ်း ေို့ 1 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း ငယတ ြောဲ့ swap
11, 33, 10, 44, 55, 77, 90 1 အခေ်း ေို့ 2 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတ ြောဲ့ swap
လို ါမယ။
207 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
11, 10, 33, 44, 55, 77, 90 0 အခေ်း ေို့ 1 အခေ်း ိို စစ ယ။ တရဲ့အခေ်း က ီ်းတ ြောဲ့ swap
လို ါမယ။
ဆကပပီ်း က
ို စစစရှာမလိုတ ှာဲ့ပါဘူ်း။
အတ ေါ် ွေ ထြော်း ြော ိို ေ ညဲ့ ညဲ့ ါမယ။ 44, 33, 11, 55, 77, 90, 10 ဆိိုပ ီ်း ိေ်း 7
ိို စစမြော ဖစ ါ ယ။
▪ No ထြော်း ြော စက ိမမြော ဇယြော်းထ အမျြော်းက ီ်း ိို စစရ ါတသ်း ယ။ စက ိမမြော
အမျြော်းက ီ်း ဆိိုရင nested loop လိိုို့ တ ြောခဲ့ ါ ယ။ ဒီတ ြောဲ့ စက ိမလို ဲ့ No ထြော်း
ြော outer loop ဖစပ ီ်း၊ ဇယြော်းထမြော ရ နစခေ်း အက ိ မက ိမ ိို ြော inner
208 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ ပ ီ်းတ ြောဲ့ No 1 မြော စခေ်းတလ ြောဲ့ ိို ယ၊ No 2 အက ိမမြော နစခေ်းတလ ြောဲ့ ိို ြော ဖစလိိုို့
bubbleSort(LA, N)
1. Set UB = N - 1
6. End of Loop
7. End of loop
စပပီ်း တလဲ့လှာ အ
ဲဲ့ ချနမှာ algorithm ကို တကှာင်းတကှာင်း နောလညသတဘှာတပါက တစ ဲဲ့
နည်းကတ ှာဲ့ Trace လိုက ှာပဲ ဖြစပါ ယ။ ဒီတ ြောဲ့ trace လိို ညဲ့ ရတအြောင။
209 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
UB = N – 1 = 4 – 1 = 3
Out ဟြော 1 တေ UB အထိ သွေြော်းမြော ဖစလိိုို့ table ရဲ့ out column မြော 1,2,3 ဆိိုပ ီ်း
ဖညဲ့လိို ယ။
out In အလိုပလိုပပို LA
1
2
3
out In အလိုပလိုပပို LA
1 0 44, 11, 22, 33
1
2
out In အလိုပလိုပပို LA
1 0 44, 11, 22, 33
1
210 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
2
2 0
1
3 0
အခိုဆိိုရင outer loop ေို့ inner loop ိို table ထ ဖညဲ့ပ ီ်းပ ီ ဖစလိိုို့ စက ိမချင်းစီမြော
1 0 LA [0] > LA[1] ➔ 44 > 11 ဆိိုတ ြောဲ့မေတ ြောဲ့ 44, 11, 22, 33
1 LA [1] > LA[2] ➔ 44 > 22 ဆိိုတ ြောဲ့မေတ ြောဲ့ 11, 44, 22, 33
2 LA [2] > LA[3] ➔ 44 > 33 ဆိိုတ ြောဲ့မေတ ြောဲ့ 11, 22, 44, 33
2 0 LA [0] > LA[1] ➔ 11 > 22 ဆိိုတ ြောဲ့မြော်း ယ၊ ဒီတ ြောဲ့ 11, 22, 33, 44
1 LA [1] > LA[2] ➔ 22 > 33 ဆိိုတ ြောဲ့မြော်း ယ၊ ဒီတ ြောဲ့ 11, 22, 33, 44
211 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
3 0 LA [0] > LA[1] ➔ 11 > 22 ဆိိုတ ြောဲ့မြော်း ယ၊ ဒီတ ြောဲ့ 11, 22, 33, 44
11, 22, 33, 44 ဆိိုပ ီ်း အတ ဖထွေ သွေြော်း ါ ယ။ ဒီမြော စချ သ ိထြော်းတစချင ြော out
ေဖိို်း 1 ပ ီ်းလိိုို့ ထွေ လြော ဲ့ အတ ဖ ိို စချ ညဲ့ ညဲ့ ါ။ table မြော မငသြောတအြောင
တလ်းတထြောငဲ့တလ်းေို့ ဝိိုင်း ထြော်း ါ ယ။ out ေဖိို်း 1 ပ ီ်းရိုေို့ အြော်းလို်း sorting စီပ ီ်းသြော်း ဖစသွေြော်း
ြော ိို တ ွေဲ့ရမြော ဖစ ါ ယ။ ဒီလိို sorting စီပ ီ်းသြော်း ဖစမ ဖစဆိို ြော တ ြောဲ့ ဝငလြော ဲ့ ိေ်းတ ွေ
တ ေါ်မြောမူ ည ြောတ ါ ဲ့တလ။ sorting စီပ ီ်းသြော်း ဖစ ြော ိို ထ စစတေတ ြောဲ့ အချိေ ိုေ ြော ရိမြော
ဖစ ါ ယ။ ဒါတ ြောငဲ့ စီပ ီ်းသြော်း ဖစသွေြော်းရင၊ ေည်းအြော်း ဖငဲ့ out စက ိမစြောအ ွေ inner loop
ိို စစ ြော မြော စက ိမမ မလရတ ြောဲ့ရင (swap) မလို ရတ ြောဲ့ရင ဆ မ ိို ေို့ ဆိို ြောမျိြို်း
တရ်းလိိုို့ရ ါ ယ။
bubbleSort(LA, N)
1. Set UB = N – 1
2. Set out = 1
5. isSwap = false
212 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
9. isSwap = true
outer loop ရဲ့ while မြော and isSwap ဆိို ြော isSwap လည်း true ဖစတေမ ဆ လို မယလိိုို့
ဆိိုလိို ြော ဖစ ါ ယ။ ဒီမြော out (outer) loop စက ိမစ ြောေို့ isSwap ထ ိို false ထညဲ့ထြော်း
isSwap ထ ိို true တ ြော ထညဲ့လိို ယ။ ဒီတ ြောဲ့ inner ထမြော စက ိမမ မလခဲ့ရဘူ်းဆိိုရင
တ ြောထြော်းလိိုို့ out တေထွေ သွေြော်းမြော ဖစလိိုို့ ဆ ပ ီ်း စရြောမလိိုတ ြောဲ့ဘူ်း။ ဒီတ ြောဲ့ run time
ိို စစတေမြော ဖစ ါ ယ။
1. LA = 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 ိို Bubble sort ထမ algorithm ဖငဲ့
sorting စီ ါ။
2. LA = 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 ိို Bubble sort ဒို ယ
ိ algorithm
213 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
4. ဒို ယ
ိ Bubble sort algorithm ိို က ီ်းစဉငယလိို စီဖိိုို့ ေတရ်းတ ်း ါ။
Selection Sort
Selection Sorting တ ြောင်း ေ၊ 2 ဆိိုရင 1, 1 ဆိိုရင 0 အခေ်း၊ J ဆိိုရင J-1 ဆိိုပ ီ်း
စခဲ့မယဆိိုရင သူူ့မြော ိို စစစရြော အတရဲ့ခေ်းမရိတ ြောဲ့လိိုို့ ဖစ ါ ယ။ ဒါတ ြောငဲ့ 1 ခေ်း စ ါမယ။
အတ ေါ်မြော ေမူေော ွေ ပ ီ်းမ algorithm ရင်း ြော ဆိိုတ ြောဲ့၊ ဒီ စခါ algorithm ပ ီ်းမ trace
လိို ရတအြောင။
selectionSort(LA, N)
1. Set UB = N -1
2. Repeat for out =0 to UB-1 by 1
3. Set min = out
4. Repeat for in = out + 1 to UB by 1
5. If(LA[min] > LA[in]) then:
6. min = in;
7. End of Loop
8. Swap (out, min)
9. End of Loop
214 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
44, 33, 11, 55, 77, 90, 10 ိို ငယစဉက ီ်းလိို SelectionSort ေို့ စီ ညဲ့လိို ရတအြောင ါ။
N=7
10
2 0
3 0 min = out = 0
4 1 in = out + 1 = 0 + 1
4 ိို ေ မယ။
ါ ယ။ ပ ီ်းရင condition စစ
ါ ယ။
By 1 ဆိို အ
ဲ့ ွေ in = in +1
လို
in = 1 + 1 = 2
215 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
to UB လိိုို့ဆိို ဲ့အ ွေ
7 Go to line 4
4 3 Update : in = in +1 = 2+1 = 3
7 Go to Line 4
4 4 Update : in = in +1 = 3+1 = 4
7 Go to Line 4
4 5 Update : in = in +1 = 4+1 = 5
7 Go to Line 4
216 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
4 6 Update : in = in +1 = 5+1 = 6
7 Go to Line 4
4 7 Update : in = in +1 = 6+1 = 7
ဖစ ါ ယ။
ေ ။
3 1 min = out
4 2 Initialization: in = out + 1
217 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Condition : in <= UB
2 <= 6 → true
စခို ိို တ ြောင်းတ ြောင်းေော်းလညဖိိုို့ ဆိိုရင trace မျြော်းမျြော်းလိို ရမြော ဖစ ါ ယ။ ဒါတ ြောငဲ့
ဒါတလ်း ိို အတ ဖထွေ သညအထိ Trace လိို ဖစတအြောငလိို ါ။ Trace ပ ီ်းတအြောင လိို ပ ီ်းမ
အခေ်းေ ါ ိို min ထ ထညဲ့ခဲ့မယ။ စစလိိုို့ အြော်းလို်းပ ီ်းမြော min အခေ်း ေို့ out အခေ်း
တေရြောချင်း လမယ။ ဒါတ ြောငဲ့ out 0 ပ ီ်းသွေြော်းရင အငယဆို်း ေဖိို်း စခို 0 ခေ်းမြော
တရြော သွေြော်းမြော ဖစ ါ ယ။
အခေ်းေ ါ ိို min ထ ထညဲ့ခဲ့မယ။ စစလိိုို့ အြော်းလို်းပ ီ်းမြော min အခေ်း ေို့ out အခေ်း
တေရြောချင်း လမယ။ ဒါတ ြောငဲ့ out 1 ပ ီ်းသွေြော်းရင ဒို ိယ အငယဆို်း ေဖိို်း စခို
218 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
N = 7, UB = 7- 1 =6
Out : 0 to UB - 1 → 0 to 5
Out 0: min = 0
တရဲ့အဝိိုင်း in ဖစပ ီ်း၊ တေော အဝိိုင်း တ ြောဲ့ min ဖစ ါ ယ။ ပ ီ်း ြောေို့ out အခေ်း နငဲ့
Swap(0,6) ဆိိုတ ြောဲ့ 10, 33, 11, 55, 77, 90, 44 ဆိိုပ ီ်းရမယ။ အငယဆို်း ိေ်း ဖစ ဲ့ 10 ဟြော
တေော တရြော ယ။ Selection sort တ ြောဲ့ စက ိမပ ီ်း ိိုင်း အငယဆို်း စခေ်း
တရဲ့တရြော ါ ယ။ ဒါတ ြောငဲ့ စက ိမပ ီ်းရင တရဲ့ စခေ်း တလ ြောဲ့တလ ြောဲ့ပ ီ်း ွေ မြော ဖစ ါ ယ။
219 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Out 1: min = 1
ပ ီ်း ြောေို့ out အခေ်း နငဲ့ min အခေ်း swap လို မယ။
Swap(1,2) ဆိိုတ ြောဲ့ 10, 11, 33, 55, 77, 90, 44 ဆိိုပ ်းီ ရမယ။ ဒို ယ
ိ အငယဆို်း ိေ်း ဖစ ဲ့
11 ဟြော 1 အခေ်း ိို တရြော သွေြော်းပ ီ ဖစ ါ ယ။ ဒါတ ြောငဲ့ တေော စခေ်းထ ပ ီ်း တလ ြောဲ့ ွေ မြော
ဖစ ါ ယ။
Out 2: min = 2
ပ ီ်း ြောေို့ out အခေ်း နငဲ့ min အခေ်း swap လို မယ။ Swap(2,2) ဆိိုတ ြောဲ့ 10, 11, 33, 55, 77, 90,
44 ဆိိုပ ီ်းရမယ။
220 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Out 3: min = 3
ပ ီ်း ြောေို့ out အခေ်း နငဲ့ min အခေ်း swap လို မယ။ Swap(3,6) ဆိိုတ ြောဲ့ 10, 11, 33, 44, 77, 90,
55 ဆိိုပ ီ်းရမယ။
Out 4: min = 4
ပ ီ်း ြောေို့ out အခေ်း နငဲ့ min အခေ်း swap လို မယ။ Swap(4,6) ဆိိုတ ြောဲ့ 10, 11, 33, 44, 55, 90,
77 ဆိိုပ ီ်းရမယ။
Out 5: min = 5
ပ ီ်း ြောေို့ out အခေ်း နငဲ့ min အခေ်း swap လို မယ။ Swap(5,6) ဆိိုတ ြောဲ့ 10, 11, 33, 44, 55, 77,
90 ဆိိုပ ီ်းရမယ။ စစဖိို့ို စလို်း ျေတ ြောဲ့ ြောတ ြောငဲ့ sorting စီလိိုို့ပ ီ်းသွေြော်းပ ီ ဖစ ါ ယ။
ထ ပ ီ်း ိို ေ
ွေ ်း ါရတစ။ တလဲ့ ျငဲ့ခေ်းတ ွေလို ါ။ မျြော်းမျြော်း Trace လိို ါ။
221 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1. LA = 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 ိို Selection sort အလွေယေည်း ဖငဲ့
sorting လို ါ။
လိို ါ။
Insertion Sort
insertionSort(LA, N)
1. Set UB = N – 1
4. in = out
7. in = in -1
8. End of Loop
9. LA [in] = temp
Insertion sort စခိုခို ိို အတသထြော်းပ ီ်း တရဲ့ အခေ်းတ ွေေို့ ိို ြော ဖစ ါ ယ။
မငသြောတအြောင Trace လိို ညဲ့လိို ရတအြောင။ အတ ေါ်မြော တ ြောတ ြောရင်းခဲ့ပ ီ်းပ ီဆိိုတ ြောဲ့ ဒီ စခါ
222 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
N = 7, UB = 7- 1 =6
Out 1:
Temp = LA [1] = 33
LA[0] = temp
Out 2:
temp = LA[2] = 11
223 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
in 0: a[in] = temp
Out 3:
temp = LA[3] = 55
LA[in] = temp
LA[ 3] =temp
Out 4:
temp = LA[4] = 77
224 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
LA[in] = temp
LA[ 4] =temp
Out 5:
temp = LA[5] = 90
LA[in] = temp
LA[ 4] =temp
Out 6:
temp = LA[6] = 10
in -1 အခေ်းနငဲ့ temp ေို့ ိို ါမယ။ temp ထ က ီ်းခဲ့ရင in – 1 အခေ်းထ ေဖိို်း ိုိ in
အခေ်းထ ထညဲ့မြော ဖစ ါ ယ။
225 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
226 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
LA[0] = temp
1. Sorting Algorithm တ ွေ ဖစ ဲ့ Bubble Sort, Selection Sort နငဲ့ Insertion sort ၃ ခိုမြော
ဘယ စခိုဟြော အလို လို ရ ြော အသ သြောဆို်းလိိုို့ ထင ါသလ။ Trace လိို ထြော်း ြော ိို
2. LA = 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 ိို Insertion sort algorithm ဖငဲ့
sorting စီ ါ။
4. LA = 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 ိို Insertion sort algorithm ဖငဲ့
က ီ်းစဉငယလိို စီတ ်း ါ။
Merge Sort
227 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အရငဆို်း နစ ိုင
ိ ်း င
ိို ်း ါမယ။ တရဲ့အ င
ိို ်း 0 to N/2 အထိ ဖစပ ီ်း တေော အ ိိုင်း တ ြောဲ့ N/2+1 to
N-1 အထိ ဖစ ါ ယ။ ထမ င
ိို ်း 0 to N/2 = 3, ဒို ိယ ိိုင်း N/2+1 to N-1 → 4 to 6
44 33 11 55 77 90 10
44 33 11 55 77 90 10
Divide
44 33 11 55 77 90
11 55
Conquer 33 44 77 90
10 77 90
11 33 44 55
10 11 33 44 55 77 90
ထ ခါ ခွေ ါမယ။
▪ Conquer လိိုို့ စြော ေ်းထိို်း ထြော်း ဲ့ တေရြော တေ စပ ီ်း ေတ ါင်း ြော ါ။ ဘြောလိိုို့
မရိတ ြောဲ့လိိုို့ ေတ ါင်း ြော ါ။ တ ါင်း ဲ့ အချိေမြောတ ြောဲ့ ိို စစပ ီ်း တ ါင်း ါ ယ။
ငယစဉက ီ်းလိို ဆိို ငယ ြောာ တရဲ့ ထြော်းပ ီ်း ေတ ါင်း ြော ဖစ ါ ယ။ တေော ဆို်း အြော်းလို်း
228 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ေ
ို ို့ ရင်း ထြော်း ဲ့ စြော ိို ွေ ညဲ့လိို ရင သတဘြောတ ါ မယထင ါ ယ။
1. if N == 1 then:
return a
Line 1 and Line 2: မြောစစထြော်း ြော array size စခို ျေ ြောလြော်း။ ဒါဆိို ခွေစရြောမလိိုတ ြောဲ့လိိုို့
Line 2 and Line 3: မြော ဝငလြော ဲ့ a array တလ်း ိို 0 to N/2 အထိ ိို array စခို၊ N/2+1 to N-1
Line 4 and Line 5: ခွေထြော်း ဲ့ array ၂ ခို ိို 1 ခေ်း ျေတ ြောဲ့ ဲ့ အထိ ထ ခါ ထ ခါ ခွေချငလိိုို့
Line 6 : တ ြောဲ့ ခွေထြော်း ဲ့ array 2 ခို ိို ေတ ါင်းဖိို့ို merge function ိို လမ်းတခေါ် ြော ဖစ ါ ယ။
ထညဲ့တ ်းလိို ြော ဖစ ါ ယ။ upper bound ဆိို ြော array အခေ်းအတရအ ွေ length ထ တေ
/* two arrays a and b, UBA is upper bound of a array, UBB is upper bound of b array*/
1. declare c as array
229 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ Line 2, Line 3, Line 4 : array a ိိုတထြော ဖိိုို့ ia, array b ိို တထြော ဖိိုို့ ib, array c ိို တထြော ဖိိုို့
ic ဆိိုပ ီ်း သ မ ယ။ ပ ီ်းတ ြောဲ့ အစဆို်း 0 ခေ်း တေ စတထြော မြော ဖစလိိုို့ variable 3 ခို
လို်းထ 0 ထညဲ့လိို ယ။
▪ Line 5 to Line 13: Array a တရြော၊ Array b နစခိုလို်း data ရိတေတသ်း ဲ့ အချိေ။
230 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ia =0, ib = 0 , ic =0
ia, ib 0 ဖစတေ ဲ့အ ွေ a[0] ေို့ b[0] ေို့ ိို မယ။ b[0] ငယတ ြောဲ့ b[0] ိို c[ic], c[0]
ထ သွေြော်းထညဲ့မယ။ b ိို ထညဲ့လိို ြော ဖစလိိုို့ ib ိို ၁ ်းိို မယ။ c ထ ထညဲ့ ြော ဖစလိိုို့ ic
ဒီ စခါ a[ia] ေို့ b[ib] ိို ညဲ့မယ။ a[0] ေို့ b[1] ဖစ ယ။ a[0] ငယ ယ။ ဒီတ ြောဲ့
c[ic] ထ ိို a[ia] ိို ထညဲ့မယ။ ပ ီ်းရင ia, ic ိို ၁ ်းိို မယ။ ib ိို ထိရေမလိို။
ia = 1, ib = 1, ic =2, c{10,11}
တ ြောဲ့ ဘြော ဖစ ဖစ အပမအထညဲ့ခတေရ ြော ဖစလိိုို့ a ေို့ b ဘယသူ ငယငယ ic တ ြောဲ့ အပမ
စ ို်းိ ရ ါ ယ။ ဒါတ ြောငဲ့ ic စ ို်းိ ြော ိို if ေို့ else ထမြော မတရ်း line 12 မြော
ဒီ စခါ ိို စစရမြော a[1] ေို့ b[1] ဖစ ယ။ a[1] ငယ ယ။ ဒီတ ြောဲ့ c[ic] ထ ိို
ia = 2, ib = 1, ic =3, c{10,11,22}
231 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ညဲ့ ညဲ့လိို ါ။ a a = { 11,22} ဆိိုပ ီ်း 0,1 နစခေ်း ရိ ြော ိို c ထ အ ိုေထညဲ့ပ ီ်းသွေြော်းလိိုို့
ia ေဖိို်း 2 ဖစပ ီ်း UBA = 1 ထ က ီ်းသွေြော်းပ ီ။ ဆိိုလိို ြော a, b နစခို မရိတ ြောဲ့ဘူ်း။ b
ျေတ ြောဲ့ ယ။ b {10, 55, 77} ဆိိုပ ီ်း 0,1,2 သို်းခေ်းရိ ြော 0 စခေ်းထညဲ့ပ ီ်းလိိုို့ ib 1
ျေ ါတ ြောဲ့ ယ။
ကူ်းထညဲ့မယ။
ူ်းထညဲ့မြော ဖစ ါ ယ။ ဒါဆိို b {10, 55, 77} ရိ ြောဆိိုတ ြောဲ့ UBB 2 ဖစလိိုို့ 1,2
ညဲ့ ါ c ထ ိို b ထညဲ့ ြော ဖစလိိုို့ စက ိမပ ီ်း ိိုင်း ic ေို့ ib ိို 1 ိို်း ါ ယ။ အခိုဟြော b
232 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
တေော ဆို်းတ ြောဲ့ a ေို့ b ိို ငယစဉက ီ်းလိို တ ါင်းပ ီ်းသြော်း array c ိို return ေတ ်းလိို ြော
2. LA = 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 ိို Merge sort algorithm ဖငဲ့
Quick Sort
အခေ်း ိို pivot ယူပ ီ်း ပ ီ်းရင pivot ထ ငယ ြော စ ိိုင်း၊ က ီ်း ြော စ ိိုင်း နစ ိုင
ိ ်းထ ခွေ ါ ယ။
ေမူေောအတေေို့ တ ြောရရင
▪ 44, 33, 11, 55, 77, 90, 22, 66 ဆိိုရင piviot 66 ဖစ ါမယ။
▪ စက ိမ loop ပ ီ်းရင {44, 33, 11, 55, 22} 66 {77, 90} ဆိိုပ ီ်း pivot ထ ငယ ြော
စအို စို၊ pivot၊ pivot ထ က ီ်း ြော စအို စိုဆိိုပ ီ်း ၃ အို စို ရ ါမယ။ {44, 33, 11, 55,
22} ရဲ့ order ဟြောအတရ်းမက ီ်း ါဘူ်း။ အတရ်းက ီ်း ြော တရဲ့အ င
ိို ်းထမြော 66 ထ
233 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒီတ ြောဲ့ Quick Sort ဆိို ြော တေော ဆို်းအခေ်း ိို pivot ယူပ ီ်း pivot ထ ငယ ြော စ ိိုင်း၊ pivot၊
1. if LB < UB then:
2. Loc = partition(LA, LB, UB)
3. quickSort(LA, LB, Loc-1)
4. quickSort(LA, Loc+1, UB)
ေတ ်းလိို ဲ့ location အတ ေါ်မူ ညပ ီ်း ငယ ြော စ ိိုင်း၊ က ီ်း ြော စ ိိုင်း ထ ခွေ ယ။
ခွေထြော်း ဲ့ နစ ိုင
ိ ်းအ ွေ recursion ေတခေါ် ယ။ ဒါ ါ ။
1. Loc = LB
2. pivot = LA[UB]
3. Left = LB
4. Right = UB
5. Repeat while true:
6. Repeat while LA[left ] < pivot:
7. Left = Left + 1
8. End of Loop
9. Repeat while LA[Right] >= pivot and Right>LB:
234 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Right ထ ငယတေသတရဲ့ တအြော အလို တ ွေ ိို ထ ခါထ ခါလို မယ(Line 5,12 and
13)။ Line 5 while true ဆိိုတ ြောဲ့ loop ဘယတ ြောဲ့မ မထွေ ဘူ်း၊ ဒါ ိုိ line 12 and
13 left>=right ဆိိုရင break ဆိိုပ ီ်း ထွေ စ ြော ဖစ ါ ယ။ Break ဆိို ြော loop
ထတရြော တေရင loop ထ တေ ရို ရ ခိုေထွေ ြော ဖစ ါ ယ။ ဒါတ ြောငဲ့ break ိို
▪ ပ ီ်းရင left အခေ်းထ ေဖိို်း pivot ထ ငယတေသမ left ိို ၁ ိို်းမယ။ ဆိိုတ ြောဲ့ left
က ီ်းတေတသ်းသမ Right ိို 1 တလ ြောဲ့မယ။ ဆိိုတ ြောဲ့ right အခေ်း ေဖိို်း pivot ထ ငယရင
ရ မယ။(Line 9 to 11)
▪ ပ ီ်းသွေြော်းလိိုို့
1. Left >=Right ဆိို break လို မယ။ break ဆိို ြော ိို outer loop ထ မြော တရ်းထြော်း ြော
ဖစလိိုို့ outer loop ထ ထွေ ါမယ။ break ဆိို ြောတရြော ဲ့တေရြော တေ ထွေ ြော
ဖစ ါ ယ။
235 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ ဒီတ ြောဲ့ partition function ိို call တခေါ်ထြော်း ဲ့ quicksort ိို ေတရြော ပ ီ်း 0
2. left Right ထ ငယတေ ယဆိိုရင Left ေို့ Right ိို တေရြောချင်းလမယ။ ပ ီ်းရင end
of loop ဆိိုလိိုို့ outer loop (Linef 5) ိို ေ ယ။ outer loop ရဲ့ condition
တေရြောမြော True ထညဲ့ထြော်းလိိုို့ looping ဟြော ဘယတ ြောဲ့မ မထွေ တ ြောဲ့ တေမြော
ဖစ ါ ယ။ ဒါတ ြောငဲ့ တအြော မြော break ိို သို်းပ ီ်း ထွေ ရ ြော ဖစ ါ ယ။ (Line 12
and 13)
Left = LB = 0
Right = UB = 7
Left ိို 1 ်းိို ိို်းပ ီ်း အခေ်းထ ေဖိို်း pivot ထ ငယသတရဲ့ တရဲ့ ယ။
Right ိို 1 တလ ြောဲ့တလ ြောဲ့ပ ီ်း အခေ်းထ ေဖိို်း pivot ထ က ီ်းပ ီ်းညီသတရဲ့ တရဲ့ ယ။
Left (4) >= Right(6) ဆိိုတ ြောဲ့ false ဖစလိိုို့ swap (Left, Right) လို မယ။
236 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Swap (4, 6)
44, 33, 11, 55, 22, 90, 77, 66, Left =4, Right = 6
Left ိို 1 ်းိို ိို်းပ ီ်း အခေ်းထ ေဖိို်း pivot ထ ငယသတရဲ့ တရဲ့ ယ။
Right ိို 1 တလ ြောဲ့တလ ြောဲ့ပ ီ်း အခေ်းထ ေဖိို်း pivot ထ က ီ်းပ ီ်းညီသတရဲ့ တရဲ့ ယ။
Left (5) >= Right(4) ဆိိုတ ြောဲ့ true ဖစလိိုို့ break ဆိိုပ ီ်း outer loop ထ ထွေ မယ။
Recursion for {44, 33, 11, 55, 22} 66 Recursion for {77, 90} ဆိိုပ ီ်းရမယ။
Recursion for {44, 33, 11, 55, 22} : Pivot = 22, LB =0, UB = 4, Left = LB = 0, Right = UB = 4
44, 33, 11, 55, 22 (left 44 မြော pivot ထ က ီ်းတ ြောဲ့ရ , Right 11 မြော pivot ထ ငယလိရ
ိုို့ )
Left(0) >= Right (2) false ထွေ လိိုို့ Swap(left, right) = Swap(0, 2) လို မယ။
237 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
44, 55, 33 (left 44(2) မြော pivot ထ က ီ်းတ ြောဲ့ရ , Right 44(2) မြော UB တရြော လိိုို့ ရ )
Left(2) >=Right(2) ဆိိုတ ြောဲ့ true ဖစလိိုို့ break ဆိိုပ ီ်း outer loop ထ ထွေ မယ။
Return 2 ဖစသွေြော်းမယ။
238 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Recursion {55,44}
55, 44(left 55(2) မြော pivot ထ က ီ်းတ ြောဲ့ရ , Right 55 မြော LB တရြော လိရ
ိုို့ )
44,55
Left ိို retrun ေ။ retrun 3 ဆိိုတ ြောဲ့ {}, 3, 4 to UB(4) ဆိိုပ ီ်း 3 င
ိို ်း င
ိို ်း ါ ယ။
{} 44 {55}
Recursion for {44, 33, 11, 55, 22} : {11} 22 Recursion for {44, 55, 33}: 11, 22, 33, 44, 55
Left(7) >= Right (6) ဆိိုတ ြောဲ့မေလိိုို့ break ဆိိုပ ီ်း outer loop ထ ထွေ ။
Recursion for {44, 33, 11, 55, 22} 66 Recursion for {77, 90}
11, 22, 33, 44, 55, 66, 77, 90 ဆိိုပ ီ်း ရသွေြော်း ါ ယ။
239 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
2. LA = 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 ိို Quick sort algorithm ဖငဲ့
Chapter အနစချျုပ
algorithm ေည်းေည်း ွေ ရ ယဆိိုပ ီ်း ြောချိေ ိို ခေို့မေ်းလိိုို့ရတ မဲ့ time complexity
▪ အစ တ ြောဲ့ Algorithm တ ွေရဲ့ time complexity (Worst Case (Big O notation (O)) ေို့
အရမ်း ရှု တထွေ်း ိုေမြော စိို်းလိိုို့ မထညဲ့လိို ါဘူ်း။ တေော အတ ခအတေတ ်းရင algorithm
စခို ိို ဘယလိို space complexity, time complexity ွေ ြောတလ်း လို တ ်း ါမယ။
240 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ပ ်းီ တ ြောဲ့ ယ
ိို သီချင်းတရ်းဖွေငဲ့ထြော်းရင
ဒီတ ြောဲ့ စြောလို ရ ြော အထူ်းသ ဖငဲ့ ဒီလိုိ ေော်းလညရခ ြောတ ွေ ိို တလဲ့လြော ဲ့အခါမြော
ျင်းစရြောမတ ြောင်းတ ြောဲ့ဘူ်း၊
241 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၄)
Stack
Stack ရဲ့ နာမည်က ြီးတဲ့ ဥပမာတတွေ တတာဲ့ Pancake ထပ်တာ နဲ့ ပန်ြီး န် ထပ်တာ တဲ့ပ
ဖြစ်ပါတယ်။
ဒါတက ာငဲ့် pancake ပ်သူတနာ ်ဆြီး ပ်ထာြီးတဲ့ မနဲ့် အတပေါ်ဆြီးမာ ရတယ်။ စာြီးတဲ့သူ
STACK လု တခေါ်ပါ ယ။
242 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အ ပ် ပ်တာပဖြစ်ပါတယ်။
အခန်ြီးနပါတ် 0 to 4 ဖြစ်ပါမယ်။ ထညဲ့်ရင် 0 ခန်ြီး တနစပပြီး ထညဲ့်ပါ ဲ့မယ်။ ဒါတက ာငဲ့် top = 0 ဆပပြီး
0 1 2 3 4
ဆပပြီးဖြစ်တနပါ ဲ့မယ်။ ဒတတာဲ့ top 0 ဖြစ်တနတယ် ဆတာ တစ်ခန်ြီးမ မရ ဖဲ့ ြစ်ပါတယ်။ ဒါတက ာငဲ့်
Stack ဟာ empty ဖြစ်တနသ ာြီး(တစ်ခန်ြီးမ မရဘူြီး ာြီး) စစ်ခ င်ရင် top 0 နဲ့ ညသ ာြီး
စစ် ်ရင်ရပါတယ်။
isEmpty()
243 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
၁ တြီးပါမယ်။
AA
0 1 2 3 4
Top ၁ တြီး ်တတာဲ့ top =1 ဖြစ်သွောြီးပါ ဲ့မယ်။
စဉ်ြီးစာြီးက ညဲ့် ်တတာဲ့ top 0 ဖြစ်တနတန်ြီး 0 အခန်ြီးထ ထညဲ့်ပပြီး၊ top 1 ဖြစ်တနတတာဲ့ 1 အခန်ြီးထ
တြီးပါမယ်။
AA BB
0 1 2 3 4
top 2 ဖြစ်သွောြီးပါ ဲ့မယ်။
AA BB CC
0 1 2 3 4
ထညဲ့်ပပြီးတာနဲ့ top 1 တြီးတတာဲ့ top 3 ဖြစ်သွောြီးပါ ဲ့မယ်။
244 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
AA BB CC DD
0 1 2 3 4
ထညဲ့်ပပြီးတာနဲ့ top 1 တြီးတတာဲ့ top 4 ဖြစ်သွောြီးပါ ဲ့မယ်။
AA BB CC DD EE
0 1 2 3 4
ထညဲ့်ပပြီးတာနဲ့ top 1 တြီးတတာဲ့ top 5 ဖြစ်သွောြီးပါ ဲ့မယ်။
က ညဲ့်က ညဲ့် ်တတာဲ့ top တန်ြြီး 5 မာ ဆ ်ထညဲ့် ဲ့မရတတာဲ့ဘူြီး။ ဘာတက ာငဲ့် ဆရင် array ရဲ့
isFull()
ဒမာ သြီးထာြီးတဲ့ N ဆတာ array ရဲ့ length တန်ြြီးပ ဖြစ်ပါတယ်။ ဒတတာဲ့ ထညဲ့်ရင် မဖပညဲ့်တသြီးမ
ထညဲ့် ရ
ဲ့ မာ ဖြစ် ဲ့ ဖပညဲ့်မဖပည်ဲ့ စစ်တပြီးရမာ ဖြစ်ပါတယ်။
245 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
push(Item)
if isFull() then:
return false
Else:
Stack[top]= Item
top = top + 1
Return true
ထညဲ့်တဲ့ push function ထမာ isFull ဆတဲ့ function တခေါ်ပပြီး စစ် ်တယ်။ Full ဖြစ်တနရင်
ဆ ်တဖပာရရင် full မဖြစ်တသြီးဘူြီး ဆရင် top အခန်ြီးထ ထညဲ့်တယ်။ top 1 တြီးတယ်။ ပပြီးရင်
AA BB CC DD EE
0 1 2 3 4
top တန်ြြီး 5 ဖြစ်တနတယ်။ ထတ်မယ်ဆ EE အခန်ြီး 4 ထတ်ရမာ ဖြစ်ပါတယ်။ ဒါတက ာငဲ့် top 5
ဆရင် 4 အခန်ြီးထတ်ခ င်တာဖြစ် ဲ့ top အရင် 1 လ ျှော့ပပြီးမ top အခန်ြီး ဆွေထတ်ရမာ ဖြစ်ပါတယ်။
246 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
top အခန်ြီး return ဖပန်တတာဲ့ return Stack[4] ဆုတ ော့ EE ဆုပပီ်း တနောကဆု်းထညော့ခော့ ော့
AA BB CC DD
0 1 2 3 4
ဒါတက ာငဲ့် pop တရြီးက ညဲ့်မယ်ဆရင် array ခန်ြီး empty ဖြစ်တနရင် ဆွေထတ် မ
ဲ့ ရတတာဲ့ဘူြီး။
pop()
If isEmpty() then:
Return NULL
Else:
top = top – 1
Return Stack[top]
AA BB CC DD
0 1 2 3 4
AA BB CC
0 1 2 3 4
247 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Empty မဖြစ်တသြီးတတာဲ့ top 1 လ ျှော့၊ top =2 ဖြစ်, ဒတတာဲ့ return Stack[2] → return CC
AA BB
0 1 2 3 4
Empty မဖြစ်တသြီးတတာဲ့ top 1 လ ျှော့၊ top =1ဖြစ်, ဒတတာဲ့ return Stack[1] → return BB
AA
0 1 2 3 4
Empty မဖြစ်တသြီးတတာဲ့ top 1 လ ျှော့၊ top =0 ဖြစ်, ဒတတာဲ့ return Stack[0] → return AA
0 1 2 3 4
တနာ ်တစ်က မ် စစ်တတာဲ့ top 0 ဆတတာဲ့ empty ဖြစ်သွောြီးပပဆပပြီးရပ်သွောြီးပါမယ်။
Pop ပ်ခဲ့ ဲ့ return ဖပန်တာတ ြီးတတွေ က ညဲ့်က ညဲ့်ပါ။ ဖပန်ရတာ EE, DD, CC, BB, AA
ဖြစ်ပါတယ်။ ဒါတက ာငဲ့် Stack ဟာ တနာ ်ဆြီးဝင်တာ အရင်ထွေ ်တာဖြစ်ပါတယ်။ ဒမာ algorithm
တ ြီးတတွေ က ာြီးညပ
ှ ်ညှပ်ပပြီး တရြီးဖပခဲ့ပါတယ်။ တသခ ာတ ြီးဖပန်က ညဲ့်ပါတယ်။ နာြီး ည်ပပဆမ
248 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
နမူနော စပုဒ က
ွ ဖပပါ ော့မယ။ ကျန တ ွကတ ော့ တလော့ကျငော့ ကညော့ ကပါ။
b) Pop()
c) Pop()
d) Push(66)
e) Push(77)
f) Pop()
g) Push(88)
h) Push(99)
i) Pop()
2. Stack : A, E, I, O, U, -----
a) Push(“BB”)
b) Pop()
c) Push(“CC”)
d) Push(“DD”)
e) Pop()
f) Push(“EE”)
a) start = 5
b) end = 100
249 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
c) Push(start)
d) Push(end)
Answer of No1:
a) top = 5
b) Pop()
top = 4 and Stack: 11, 22, 33, 44, ----, ---, ----, ---- and return 55
c) Pop()
top = 3 and Stack: 11, 22, 33, ----, ----, ---, ----, ---- and return 44
d) Push(66)
Stack: 11, 22, 33, 66, ----, ---, ----, ---- and top = 4
e) Push(77)
Stack: 11, 22, 33, 66, 77, ---, ----, ---- and top = 5
250 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
f) Pop()
top= 4 and Stack: 11, 22, 33, 66, ---, ---, ----, ---- and return 77
g) Push(88)
Stack: 11, 22, 33, 66, 88, ---, ----, ---- and top = 5
h) Push(99)
Stack: 11, 22, 33, 66, 88, 99, ----, ---- and top = 6
i) Pop()
top= 5 and Stack: 11, 22, 33, 66, 88, ---, ----, ---- and return 99
Example of Stack
[a * (2 + b)] / 2
တထာငဲ့် ွေင်ြီးအစ အရင် ာပါတယ်။ ပပြီးတတာဲ့ ( - ်သည်ြီး ွေင်ြီးစ ာပါတယ်။ တဟာ ပတ်တတာဲ့
ဒတတာဲ့ တနာ ်ဆြီး ထညဲ့်ခဲ့တဲ့ ွေင်ြီးစရဲ့ အပတ် အရင် ာရတာဖြစ် ဲ့ stack သြီးပပြီး
စစ် ဲ့ရပါတယ်။ တွေန်ဲ့ ွေင်ြီး {, တထာငဲ့် ွေင်ြီး [, ်သည်ြီး ွေင်ြီး ( ဘယ် ွေင်ြီးစ ာ ာ Stack
တပေါ်တင်ပါ ဲ့မယ်။
251 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
်သည်ြီး ွေင်ြီး ်သည်ြီး ွေင်ြီးခ င်ြီး၊ တထာငဲ့် ွေင်ြီး တထာငဲ့် ွေင်ြီးခ င်ြီး၊ တွေန်ဲ့ ွေင်ြီး
တွေန်ဲ့ ွေင်ြီးခ င်ြီး တူမတူ စစ်ပါ ဲ့မယ်။ မတူရင် ွေင်ြီးစ ွေင်ြီးပတ် ွေတနတာ ဖြစ် ဲ့ မာြီးပါတယ်။
(Unmatch)
ဖြစ်ပါတယ်။ (Correct)
[a * (2 + b) } / 2
Character
* Skip
252 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
2 Skip
+ Skip
b Skip
မန်တသြီးတတာဲ့ ဆ ်တ ်မယ်။
ဆ ်တ ်စရာမ တတာဲ့ဘူြီး။
{e + [a * (2 + b)] / 2
character
e Skip {
+ Skip {
253 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
a Skip {, [
* Skip {, [
2 Skip {, [, (
+ Skip {, [, (
b Skip {, [, (
ဆ ်ပပြီး တ ်မယ်။
/ Skip {
2 Skip {
တလော့ကျငော့ ကညော့ပါ။
254 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
1. Set index = 0
2. Set length= LENGTH(equation)
3. Repeat while index < length:
4. Ch = equation [index]
5. If Ch == ‘(‘ or Ch == ‘[‘ or Ch == ‘{‘ then:
6. stack.push(Ch)
7. Else if Ch == ‘)‘ or Ch == ‘]‘ or Ch == ‘}‘ then:
8. If stack.isEmpty() then:
255 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Queue
Queue ရဲ့ ဥပမာတတွေ ဖြစ်ပါတယ်။ ဝငပပီ်း န်းစီရင တနောကကတန ဝငပပီ်း န်းစီရ ယ။ ပပီ်းသွ ်းလု
ထွကတ ော့ တရှေ့ကလူက အရငထွကသွ ်း ယ။ ဒီတ ော့ ဝငရင တနောကကတနဝင၊ ထွကရင တရှေ့က
ထွက ဖြစလု တနောကကု တထ ကြု Rear နငော့ တရှေ့ကု တထ ကြု Front ဆုပပီ်း တထ ကြု
နစခုလုပါ ယ။ ဒါတက ာငဲ့် ဝင်ရင် Rear ဝင်၊ ထွေ ်ရင် Front တန ထွေ ်ရမာဖြစ်ပါတယ်။
သူက အရငရင်းပပီ်း ထွကသွ ်းရ ဖြစပါ ယ။ အရင တရ ကသူ အရငထွကရ ဖြစလု Queue ကု
and E
E:0
256 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Insert A Front : 0 A, ---, ---, ---, --- ထညဲ့်တတာဲ့ Rear ဘ ် ထညဲ့်၊ Rear
တြီး။
Rear : 2
E: 2
Rear : 3
E:3
Return : A
Return : B
257 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အစ ဖပန်စရမာမဲ့ Rear ၁
တတာဲ့ 1 တြီး။
E: 4 တြီး၊ E ၁တြီး။
E:4 Return : C
258 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
E:3 Return : D
တြီးမညဲ့်အစာြီး 0 ဖပန်စရမယ်။
Return : E
E:1 Return : F
Front : 2 ---, ---, ---, ---, --- Front 2 ဆတတာဲ့ 2 အခန်ြီး G ထတ်။
E:0 Return : G
259 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Front = 0, Rear = 0 ,
isEmpty( )
Return E == 0
isFull()
return E==N
Insert (Item)
If isFull() then:
Return false
If Rear == N – 1 then:
Rear = 0
Else:
Rear = Rear + 1
E=E+1
Delete ()
If isEmpty() then:
Write “Underflow”
Return NULL
If Front == N – 1 then:
Front = 0
260 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Else:
Front = Front + 1
E=E–1
Return Value
If isFull() then:
Return false
ယခ ပ်တနတဲ့ function ထ တန function call တခေါ်တဲ့ဆ ထွေ ်သွောြီးမာ ဖြစ်ပါတယ်။ ဒါတက ာငဲ့်
If မာ စစ်ထာြီးတဲ့ isFull မန်ခဲ့ရင် Queue[ Rear ] = Item တနစပပြီး သူူ့ရဲ့တအာ ် စာတက ာင်ြီးတတွေ
တရာ ် ာစရာ အတက ာင်ြီးမရပါဘူြီး။ တရာ ် ာတယ်ဆရင် If တနာ ် condtion ဖြစ်တဲ့ isFull
ဖပန်တရြီးဖပရရင်
Insert (Item)
If isFull() then:
Return false
Else:
If Rear == N – 1 then:
Rear = 0
Else:
Rear = Rear + 1
E=E+1
261 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Else တ ြီးတြ ာ ်တာနဲ့ မတြ ာ ်တာပ ွောပါတယ်။ Delete မာ ည်ြီး ထနည်ြီးတူ ဖပန်တရြီး ဲ့
ရပါတယ်။
1. Insert 50
2. Insert 60
3. Insert 70
4. Insert 80
2. Queue is Empty. N = 10
262 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Hashing
ပထမဆြီး တွေ ် ရ
ဲ့ ာတဲ့ တန်ြြီး 0 ခန်ြီး၊ ဒတယ တွေ ် ရ
ဲ့ ာတဲ့ တန်ြြီး 1 အခန်ြီး စသည်ဖြငဲ့်
က ြီး ်တတွေ မဟတ်ဘူြီး။ သူထွေ ်ခ င်တာ ထွေ ်တယ်။ ဒတတာဲ့ အဒ array ထ တစ်ခခ
ရာခ င်တယ် ဆရင် တန်ြြီးတတွေဟာ sorting စထာြီးတာမဟတ် ဲ့ Binary search သြီး ဲ့မရဘူြီး၊
မ ပ်တတာဲ့ဘ ထညဲ့ခ
် င်တတ
ဲ့ န်ြြီးတပေါ်မူတည်ပပြီး အခန်ြီးနပါတ် တွေ ်ပါမယ်။ တွေ ် ရ
ဲ့ ာတဲ့
အခန်ြီးထ ထညဲ့်ခ င်တဲ့ တန်ြြီး သွောြီးသမ်ြီးမာ ဖြစ်ပါတယ်။ ဒတတာဲ့ ရာတဲ့ အခါမာ ည်ြီး Linear
search မ ြိုြီး အခန်ြီးတပါ ်တစဲ့ အခန်ြီးနပါတ်အစဉ် ် ရာစရာမ တတာဲ့ဘ ရာခ င်တဲ့ တန်ြြီးတပေါ်
Cat ဆရင်
263 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
t = 20
Cat = 3 + 1 + 20 = 24
N = 60
Insert 360
arrayIndex = 360 % 60 = 0, ဒါတက ာငဲ့် 360 array ရဲ့ 0 ခန်ြီးမာ သွောြီးပပြီး ထညဲ့်မာဖြစ်ပါတယ်။
Insert 70
Search 365
Collisions
သတ
ဲ့ သာ် ည်ြီး Hashing နည်ြီး collisions ဖပဿနာရတနပါတယ်။ Collision
နမူနာဖပရရင်
264 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သွောြီးမာဖြစ်ပါတယ်။
သွောြီးထညဲ့်မာဖြစ်ပါတယ်။
သွောြီးမာ ဖြစ်ပါတယ်။
265 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
N = 30
… Searching 307
7 67
arrayIndex = 307 % 30 = 7
8 128
9 39 7 အခန်ြီးမာ မတတွေဲ့တတာဲ့ 12 တပါင်ြီးမယ်။
10
7 + 12 = 8 မာ ည်ြီး မတတွေဲ့ဘူြီး။
11 41
12 22 တပါင်ြီးမယ်။
13
7 + 22 = 11မာ ည်ြီး မတတွေဲ့ဘူြီး။
14
15 32 တပါင်ြီးမယ်။
16 307 7+ 32 = 16 မာ တတွေဲ့ပါတယ်။
…
3. Double Hashing
arrayIndex = value % N
N = 10
Insert 12
266 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Insert 2
StepSize = 5 – (2 % 5 ) = 5 – 2 = 3
Insert 102
StepSize = 5 – (102 % 5 ) = 5 – 2 = 3
သတ
ဲ့ သာ် Hashing ဆတာ ရာတအ
ဲ့ ခ န်(ဆွေထတ်တဲ့ အခ န်) အခ န် န် သ ်သာတအာင်
267 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
3. N = 30 ဖြစ်တသာ Array ထ 13, 2, 32, 92, 17, 18, 33, 63, 153, 150 တဲ့ Quadratic
5. N = 23၊ constant 5၊ 1, 38, 37, 16, 20, 3, 11, 24, 5, 16, 10, 31, 18, 12, 30, 1, 19, 36, 41,
7. ဒမာ stack တဲ့ Queue တဲ့ အဖပင် sorted array ဆတာ ရပါတသြီးတယ်။ ဥပမာ N=10 ဆ 10
တ ဲ့ ငဲ့်တစခ င်ပါတယ်။
268 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Chapter အနှစခ
် ျုပ်
▪ LIFO - တနာ ်ဆြီးဝင်တဲ့တ ာင် အရင်ဆြီးထွေ ်ခ င်ရင် Stack သြီးပါတယ်။ တနာ ်ဆြီးဝင်၊
top ဲ့ တခေါ်ပါတယ်။
269 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
တ ာ ်သင်ပန်ြီးတပေါ်သင်ရတာ၊
အဒ
ဲ့ ါ ပထမနစ် အဖပန်ဖပန် အ န် န် ပ်ခတ
ဲ့ ဲ့ အ ြိုြီးတတွေပါ။”
270 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၅)
Linked List
Value Link
HEAD 0
1 1 I 3
2
3 --- (space) 4
4 C 6
5
6 A 7
7 N 10
8
9
10 ---(space) 12
11
12 F 13
13 L 14
14 Y NULL
271 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Simple Linked List (Singly Linked List လိုို့လည်ြီး ခ ေါ် ါတယ်) ဆိုခတှာါ့ variable name ကို SLL
ဲ ထှာြီးလိုက်ကကရခ ှာင်။
ရမှာပြစ် ါတယ်။
272 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
ဒမှာ Poem ဆိုတဲါ့ pointer ကခန “Life is a challenge, meet it.” ဆိုတှာကို
ခထှာက် င် ါတယ်။ Agreement pointer ကခန “Try Our Best!” ဆိုတှာကို ခထှာက် င် ါတယ်။
Value Link
Poem 0 Life
1 is
2 Try
3 a
Agreement
4
5 Our
6 challenge
7
8
9 ,
10 meet
11 Best!
12 It.
13
14
Value Next
ဒလို နစ် ို တလ
ဲ ိုက်ခလြီးနဲို့ ပ ခလါ့ရ ါတယ်။ အဲဒီအတလ
ဲ ်းကို Node ိုို့လခေါ်ပါတယ။ Node ထဲမော
273 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
float/double data type, စှာလိုြီးခလြီးတစ်လိုြီး သမ်ြီး င်ရင် char data type၊ စှာသှာြီး
2. Next - ကခတှာါ့ ခနာက် Node တစ် ိုကို ခထှာက်မှာပြစ်လိုို့ Node pointer ပြစ်ရ ါမယ်။
pointer ဆိုတှာ ခထှာက်တှာ ါ။ int ကို ခထှာက်ရင် int pointer, float ကို ခထှာက်ရင် float
pointer။ ဒမှာက Node ကိုခထှာက်ခတှာါ့ Node pointer ပြစ် ါမယ်။ Node pointer ကို
HEAD
AA BB CC DD NULL
HEAD → Value = AA
AA → Next → Value = BB
BB → Next → Value = CC
CC → Next → Value = DD
Traversing ဆိုခတှာါ့ HEAD ကခန စပ ြီး Next Next ဆိုပ ြီး ခနာက်ကို ဆက်ဆက်ပ ြီး NULL
274 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ခလ ှာက်ခရှေ့လမ
ိုို့ ရ ါဘူြီး။ ဒါခကကှာငါ့် line 4 မှာ PTR ထဲကို HEAD ထညါ့်လိုက်ပ ြီး PTR ကို ဲ NULL
မခရှာက်မ င်ြီး ဆက်ပ ြီး ခရှေ့သှာြီးမှာပြစ် ါတယ်။ HEAD ကခတှာါ့ ခရှေ့ဆိုြီးမှာ ဲ က န်ခန ဲါ့ြိုို့
HEAD PTR
AA BB CC DD NULL
Line 6: ဒခတှာါ့ PTR → Value ပြစ်တဲါ့ AA ခ ေါ်မှာ တက်တှာ က်တှာ၊ ထိုတ်ပ တှာ ကက က်တှာလို ် ါ။
Line 7: PTR = PTR → Next ကို ထညါ့်တယ်ဆိုတှာ update လို ်တှာပြစ် ါတယ်။ PTR ဟော AA Node
275 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
PTR
HEAD
AA BB CC DD NULL
Line 6:ဒခတှာါ့ PTR → Value ပြစ်တဲါ့ BB ခ ေါ်မှာ တက်တှာ က်တှာ၊ ထိုတ်ပ တှာ ကက က်တှာလို ် ါ။
Line 7: PTR = PTR → Next ကို ထညါ့်တယ်ဆိုတှာ update လို ်တှာပြစ် ါတယ်။ ခနာက်တစ် Node
ကို ခရှေ့တှာ ပြစ် ါတယ်။ PTR ဟော BB လနရောကလန CC Node လနရောကို လရောက ောမောဖဖစပါတယ။
PTR
HEAD
AA BB CC DD NULL
Line 6:ဒခတှာါ့ PTR → Value ပြစ်တဲါ့ CC ခ ေါ်မှာ တက်တှာ က်တှာ၊ ထိုတ်ပ တှာ ကက က်တှာလို ် ါ။
Line 7: PTR = PTR → Next ကို ထညါ့်တယ်ဆိုတှာ update လို ်တှာပြစ် ါတယ်။ ခနာက်တစ် Node
ကို ခရှေ့တှာ ပြစ် ါတယ်။ PTR ဟော CC လနရောကလန DD Node လနရောကို လရောက ောမောဖဖစပါတယ။
PTR
HEAD
AA BB CC DD NULL
276 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Line 6:ဒခတှာါ့ PTR → Value ပြစ်တဲါ့ DD ခ ေါ်မှာ တက်တှာ က်တှာ၊ ထိုတ်ပ တှာ ကက က်တှာလို ် ါ။
Line 7: PTR = PTR → Next ကို ထညါ့်တယ်ဆိုတှာ update လို ်တှာပြစ် ါတယ်။ ခနာက်တစ် Node
ကို ခရှေ့တှာ ပြစ် ါတယ်။ PTR = PTR → Next = NULL ဆိုပ ြီး PTR ထဲ ကို NULL
ခရှာက်သှာြီးမှာပြစ် ါတယ်။
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
1. Node ရဲှေ့ Value ဟှာ int ပြစ်တယ်ဆို ါခတှာါ့။ Linked List ထဲက Node ှာြီးလိုြီးရဲှေ့ Value
ှာြီးလိုြီးခ ါင်ြီးလဒ်ကို တက်ခ ြီးတဲါ့ algorithm ကို ခရြီးခ ြီး ါ။ ခ ါင်ြီးမှာ ပြစ်တဲါ့ တက် total
277 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဆိုတဲါ့ ခပြထညါ့မ
် ညါ့် variable ထဲကို 0 assign လို ်ထှာြီးခ ြီး ါ။ ပ ြီးမ node တစ် ို င်ြီးရဲှေ့
ဆက်သှာြီးစရှာမရခတှာါ့ ဲ NULL နဲို့ ညသှာြီးပ ဆိုမ total ကို return ပ န်ခ ြီး ါ။
2. Linked list ထဲမ Value 100 ထက်ကကြီးတှာခတကို ထိုတ်ပ မညါ့် algorithm ကို ခရြီး ါ။
Node တစ် ိုကို simple linked list ထဲ ထညါ့်ခတှာါ့မယ် ဆိုရင် ပြစ်နိုင်ခ 2 မ ြီး ရ ါတယ်။
1. Node တစ် ိုမ မရခသြီးလိုို့ HEAD ကခထှာက်စရှာမရ ဲ NULL ပြစ်ခနတယ်။ ဒခတှာါ့ ထညါ့်တါ့ဲ
HEAD
AA BB DD NULL
TT NULL
278 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
TT Node က သစ်ထညါ့်မညါ့် newNode ပြစ် ါတယ်။ ဒခတှာါ့ ခနာက်ဆိုြီး Node ကို ရင်ရှာရမှာ
ပြစ် ါတယ်။ ခနာက်ဆိုြီး Node ဆိုတှာ Next က NULL ပြစ်ခနတဲါ့ Node ပြစ် ါတယ်။ မ
ို ှာ
ကကညါ့်ကကညါ့် ါ။ ဒါခကကှာငါ့် PTR ထဲကို HEAD ထညါ့်ပ ြီး Next က NULL နဲို့ မညမ င်ြီး သှာြီး ါ ါ့မယ်။ Next
က NULL ပြစ်ပ ဆိုခတှာါ့မ ခနာက်ဆိုြီး Node ကို ခရှာက်သှာြီးပ ပြစ်လိုို့ PTR ရဲှေ့ Next ထဲကို
Insert(newNode)
Value ကိုခ ြီးမယ်။ ဲဒခ ြီးလိုက်တါ့ဲ value နဲို့တူတဲါ့ value ရတဲါ့ Node တစ် ိုကို simple linked list
279 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
HEAD
AA BB DD NULL
▪ ခရှေ့ဆိုြီး Node ကို ြ က်မှာပြစ်လိုို့ ဒိုတယ Node ကို HEAD က သှာြီးခထှာက်ရမှာ ပြစ် ါတယ်။
Free (PTR)
2. အ ယ Node ကိုဖျကတော၊
HEAD
AA BB DD NULL
280 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ ြ က်မညါ့် Node ရဲှေ့ ခရှေ့ က Node ( ို ရဆိုရင် AA Node) ကို prev လိုို့ထှာြီး ါ ါ့မယ်။
▪ Arrow ခလြီးနဲို့ ပ ထှာြီးသလို ါ ဲ prev ရဲှေ့ Next ထဲကို ဖျကမညေ့ Node ရဲှေ့ Next ကို ထညါ့်ရမှာ
ပြစ် ါတယ်။ (ပ ထှာြီးတဲါ့ ို ရဆိုရင် Prevရဲှေ့ Next ဆိုတှာက AA ရဲှေ့ Next ပြစ်ပ ြီး ြ က်မညါ့် Node
BB ရဲှေ့ Next ဆိုတှာ DDကို ခပ ှာတှာပြစ် ါတယ်။ BB ြ က်လိုက်လိုို့ AA Node က DD Node ကို
ဒါခကကှာငါ့် Prev ဆိုတဲါ့ pointer တစ် ိုထ ်ပ ြီးလို ါတယ်။ PTR က ခနာက်ကို မခရှေ့ င် PTR ခနရှာကို
Prev ထှာြီးခ ြီး ဲါ့ရမှာပြစ် ါတယ်။ ဒါမှာ PTR က ြ က်မညါ့် Node ကို ခရှာက်သှာြီး န်မှာ ြ က်မညါ့်
Free (PTR)
HEAD
AA BB CC NULL
ခနာက်ဆိုြီး Node ကို ြ က်မယ် ဆိုတှာ ဘယ်လို သမလဲဆိုရင် ြ က်မညါ့် Node ရဲှေ့ Next ဟှာ NULL နဲို့
ညရင် ြ က်မညါ့် Node ဟှာ ခနာက်ဆိုြီး Node ပြစ် ါတယ်။ ခနာက်ဆိုြီး Node CC ကို ြ က်မယ်ဆိုရင်
Prev Node (BB Node) ဟှာခနာက်ဆိုြီး Node ပြစ်သှာြီးမယ်။ ဒခတှာါ့ BB ရဲှေ့ Next ထဲကို NULL
ထညါ့်ခ ြီးရမယ်။
281 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဘှာခကကှာငါ့်လဲဆိုရင် CCNode ရဲှေ့ Next ဟှာလည်ြီး NULL ပြစ်ခနလိုို့ ါ။ ဒါခကကှာငါ့် Prev → Next =
PTR →Next လိုို့ ခရြီးလိုို့ရ ါတယ်။ (PTR ဆိုတှာ ြ က်မညါ့် Node, Prev ဆိုတှာ ြ က်မညါ့် Node ရဲှေ့
Free (PTR)
Prev = PTR
End of Loop
If PTR==NULL then:
Return false
Else:
Free (PTR)
Return true
PTR ဟှာ ခရှေ့ဆိုြီး Node ကခနစပ ြီး ရှာလိုက်တှာ ခတှေ့ရင်ရ ်သှာြီးမှာပြစ် ါတယ်။
မခတှေ့လိုို့ဆက်သှာြီးရင်ခတှာါ့ ခနာက်ဆိုြီး NULL နဲို့ ညသှာြီးမှာ ပြစ် ါတယ်။ ဒါခကကှာငါ့် NULL နဲို့
282 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ထက်လှာတဲါ့ န်မှာလည်ြီး PTR ဟှာ NULL နဲို့ ညလိုို့ ထက်လှာတှာလှာြီး စစ်တယ်။ NULL နဲို့
ခတှေ့လိုို့ထက်လှာတှာပြစ် ါတယ်။
delete(x)
283 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
Linked List ထဲမှာ 40, 30, 20, 10, 55, 33, 77 ဆိုပ ြီး စဉ်လိုက်ရခန ါတယ်။ HEAD ဟှာ 40Node
ကို ခထှာက်ခနမှာပြစ်ပ ြီး၊ 77 ကခတှာါ့ ခနာက်ဆိုြီး Node ပြစ်မှာ ပြစ် ါတယ်။ ခ ှာက် ါတို့က
ို ို trace
လိုက် ါ။
1. delete(40)
2. delete(10)
3. delete(77)
Node တစ် ိုကို simple linked list ထဲ ငယ်စဉ်ကကြီးလိုက် sorted order ပြငါ့်ထညါ့်ခတှာါ့မယ် ဆိုရင်
ပြစ်နင
ို ်ခ ၃ မ ြီး ရ ါတယ်။
2. လယ်မှာသှာြီးထညါ့်တှာ။
284 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
10 NULL 20
New Node
• ထမဆိုြီးခတှာါ့ newNode ရဲှေ့ Next ဟှာ NULL ပြစ်ခနတယ်။ NULL စှာြီး HEAD
HEAD = newNode
2. အ ယမော သော်းထညေ့တော။
HEAD
10 20 30 NULL
2
1
25 NULL
ခနရှာကခနစပ ြီး ကကညါ့် ါ ါ့မယ်။ ထညါ့် င်တါ့ဲ newNode တန်ြိုြီးထက် ငယ်ခန ဲါ့ရင် ခနာက်ကို
ဆက်သှာြီး ါမယ်။ ကကြီးပ ဆိုခတှာါ့မ ဲဒကကြီးတဲါ့ Node ရဲှေ့ ခရှေ့မှာ newNode ကို ထညါ့်ရမှာ
ပြစ် ါတယ်။ simple linked list ဟှာ Next ဲ ါလိုို့ ခနာက်ကို ဲသှာြီးလိုို့ ရ ါတယ်။ ခရှေ့ပ န်သှာြီးလိုို့
285 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
မ
ို ှာပ ထှာြီးတဲါ့ တင
ို ်ြီး 25 ကိုထညါ့် င်တယ်၊ ထမ Node 10ဟှာ ထညါ့် င်တါ့ဲ 25
ဆက်သှာြီးမယ်၊ 30 မငယ်ခတှာါ့ဘူြီး ရ မ
် ယ်။
3. လနောကဆို်းမော သော်းထညေ့တော
HEAD
10 20 30 NULL
45 NULL
286 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ပြစ်သှာြီးတဲါ့ န်မှာ 30 ဟှာ Prev ပြစ်ခန ဲါ့မှာ ါ။ ဒခတှာါ့ Prev → Next = newNode ဆိုရင်
ဆင်ခပ သှာြီး ါပ ။
End of Loop
If PTR == NULL then:
Prev → Next = newNode
InsertSorted(newNode)
287 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
1. ခ ေါ်က insertSorted algorithm ကို ကကြီးစဉ်ငယ်လိုက်ထညါ့်တှာ တက်ဆို
ဘယ်ခနရှာခတ ပ င်ခရြီးရမလဲ။
Doubly Linked List မှာဆိုရင် pointer က နစ် ို ပြစ်သှာြီးတှာ ါ။ တစ် ိုက ခရှေ့ကNode ကို
ခထှာက်မညါ့် Prev ပြစ်ပ ြီး၊ က န်တစ် ိုက ခနာက်ကNode ကိုခထှာက်မညါ့် Next ပြစ် ါတယ်။
HEAD TAIL
NULL 10 20 30 NULL
▪ ခရှေ့ခထှာက်မညါ့် Prev နငါ့် ခနာက်ကိုခထှာက်မည်ါ့ Next ဆိုပ ြီး pointer နစ် ို ါတှာဆိုခတှာါ့
288 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ ခနာက်ကို သှာြီး င်ရင် HEAD ကခနစမယ်။ ပ ြီးရင် Next ဟှာ NULL နဲို့ မညမ င်ြီးသှာြီးလိုို့ရမယ်။
▪ ခရှေ့ကိုသှာြီး င်ရင် TAIL ကခနစမယ်။ ပ ြီးရင် Prev ဟှာ NULL နဲို့ မညမ င်ြီး သှာြီးလိုို့ရမယ်။
လ
ို ှာမှာပြစ် ါတယ်။
Node တစ် ိုကို Doubly linked list ထဲ ထညါ့်ခတှာါ့မယ် ဆိုရင် ပြစ်နိုင်ခ 2 မ ြီး ရ ါတယ်။
289 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Linked list ထဲမှာ Node တစ် ိုမ မရခသြီးရင် HEAD ခရှာ၊ TAIL ါ ခထှာက်စရှာမရလိုို့ NULL
ပြစ်ခနမှာ ပြစ် ါတယ်။ သစ် င်လှာတဲါ့ newNode သည် တစ် ိုတည်ြီးခသှာ Node ပြစ်မှာ
ပြစ်ပ ြီး တစ် ိုတည်ြီးရတှာပြစ်လိုို့ ခရှေ့ဆိုြီး Node ဆိုလည်ြီး newNode ါ ဲ။ ခနာက်ဆိုြီး Node
ပြစ် ါတယ်။
တူတူ ပဲ ြစ် ါတယ်။ ဘှာလိုို့လဲဆိုရင် Node တစ် ိုမ မရခသြီးတဲါ့ ခပ ခနမှာ HEAD ခရှာ
HEAD TAIL
NULL 10 20 30 NULL
40 NULL
မ
ို ှာ ပ ထှာြီးတဲါ့ တင
ို ်ြီး newNode 40 ကို လက်ရ TAIL ရဲှေ့ခနာက်မှာ ထညါ့်လိုက်ရို ါ ဲ။ ပ ြီးရင်
ပြစ် ါတယ်။
290 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
TAIL → Next = newNode ဆိုတှာ 30 Node ရဲှေ့ Next က သစ် င်လှာတဲါ့ 40 Node
TAIL
30 40 NULL
ပြစ် ါတယ်။
TAIL
30 40 NULL
ပြစ် ါတယ်။
TAIL
30 40 NULL
InsertIntoDLL(newNode)
291 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Node တစ် ိုကို doubly linked list ထဲကခန လိုက်ရှာပ ြီး ြ က်ခတှာါ့မယ် ဆိုရင် simple linked list
လို ဲ ပြစ်နင
ို ်ခ ၃ မ ြီး ရ ါတယ်။
HEAD TAIL
NULL 10 20 30 NULL
▪ ခရှေ့ပ ြီးသှာြီး HEAD (20 Node) ခရှေ့ဆိုြီးပြစ်လှာမှာပြစ်လိုို့ သူူ့ခရှေ့မှာ ဘှာမ မရခတှာါ့လိုို့ သူူ့ရဲှေ့ prev
▪ ပ ြီးမ ရင် HEAD ခနရှာ ထှာြီး ဲါ့တါ့ဲ PTR (10 Node) ကို ြ က် စ်ရမှာပြစ် ါတယ်။
Free (PTR)
292 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
2. အ ယ Node ကိုဖျကတော၊
HEAD TAIL
NULL 10 20 30 NULL
▪ ို ရဆိုရင် 10 Node ရဲှေ့ Next ဟှာ 30 Node ကို သှာြီးခထှာက်ပ ြီး၊ 30 Node ရဲှေ့ prev ဟှာ 10
node ကို သှာြီးပ ြီး ခထှာက်ရမှာ ပြစ် ါတယ်။ ပ ြီးမ ြ က်မညါ့် Node ကို Free လို ် ါမယ်။
End of Loop
If PTR==NULL then:
Return false
Free (PTR)
Return true
293 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ခနာက်ဆိုြီး Node ကို ြ က်တယ်ဆိုတှာ TAIL Node ကို ြ က်တှာပြစ်လိုို့ TAIL ရဲှေ့ prev ဟှာ TAIL
deleteFromDLL(x)
294 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
1. Simple Linked List ရဲှေ့ sorted စ
ို နဲို့ထညါ့်တှာကို ကကညါ့်ပ ြီး doubly linked list တက်
sorted စ
ို နဲို့ ထညါ့်တှာကို ပ န်ခရြီးခ ြီး ါ။ကကြီးစဉ်ငယ်လိုက်ထညါ့်တှာရယ်၊ ငယ်စဉ်ကကြီးလိုက်
2. Doubly Linked List မှာ HEAD, TAIL ဆိုပ ြီး pointer ၂ ိုကို သိုြီးပ ပ ြီး လို ်လို ်သှာြီးတှာ
doubly linked list တက် algorithms ခတကို HEAD pointer တစ် ို ဲ သိုြီးပ ပ ြီး
ခရှေ့ဆိုြီး Node ကို ပ န်ခထှာက်တှာ ပြစ် ါတယ်။ ဒါခကကှာငါ့် Circular Linked List မှာ ခနာက်ဆိုြီး Node
လှာြီး စစ် င်ရင် ရင်လို Next က NULL နဲို့ညတှာ စှာြီး Next က HEAD နဲို့ ညတှာခပ ှာင်ြီးစစ်ရမှာ
ပြစ် ါတယ်။
295 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
HEAD ပ န်ထညါ့်ခ ြီး ဲါ့ရမှာ ပြစ် ါတယ်။ လယ်က Node ခတ၊ ခရှေ့ဆိုြီး Node
လို ်လို ် ခ
ို တဟှာ ကှာြီးပ ှာြီး က်မရ ါဘူြီး။
HEAD
10 20 30 40
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
Simple Linked List နဲို့ doubly linked list ကို ခသ ှာခပ ှာပ ြီးသှာြီးပ ၊ ပ ြီးခတှာါ့ circular linked list
ရဲှေ့ ကှာပ ှာြီး က်ကိုလည်ြီး ခပ ှာ ဲါ့ပ ြီးပ ပြစ်တှာခကကှာငါ့် traversing, insert, delete, insertSorted
စတဲါ့ algorithms ခတကို circular linked list တက် ကက ြီးစှာြီးပ ြီး ခရြီးကကညါ့်ခ ြီး ါ။
Chapter အနစချျုပ
စ
ို တစ်မ ြီး ပြစ် ါတယ်။
ကက တင်ပ ြီး implement လို ်ထှာြီးခ ြီးပ ြီး (ကက တင်ခရြီးသှာြီးထှာြီးခ ြီးပ ြီး) ပြစ် ါတယ်။
296 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ သခ
ိုို့ သှာ် က ြီး၊ ပ စ် မသ ဲ ခတှေ့ရှာခလ ှာက်သိုြီးရင် ခသ ှာခ ါက် run time မှာ
Next စတဲါ့ link ခတခပ ှာင်ြီး တ်ခနရတှာနဲို့ န်ယူခနတှာပြစ် ါတယ်။ ရိုြီးရိုြီး array မှာခတှာါ့
297 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၆)
Tree
B C
D E F G
H I
▪ Key: အွဲဒြီ Node ထွဲမာပါတွဲ A,B,C ဆိုတာရ ွားရတကိုရတာ key ိုဲ့ရခေါ်ပါတယ့်။ ဒါရ ကာင့် Node
တစ့်ခိုမာ Key ယ့်၊ ပပြီွားရတာ သူ့ရအာက့်က Node ရတကို ပန့်ရထာက့်မည့် Node pointer
ကို B နွဲဲ့ C ွဲှေ့ parent ိုဲ့ရ ပာပပြီွား၊ အ ပန့်အ န့်အရနနွဲဲ့ B နွဲဲ့ C ကို A ွဲှေ့ child ရတ ိုဲ့ ရ ပာပါတယ့်။
▪ Siblings: Parent တတွဲ node ရတကို siblings ိုဲ့ ရခေါ်ပါတယ့်။ ဥပမာ B နွဲဲ့ C ြစ့်ပါတယ့်။
298 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ Edge: Node တစ့်ခို နွဲဲ့ တစ့် ခာွား node တစ့်ခို အ ကာွားဆက့်ထာွားတွဲ line ရ ွားကို edge ိုဲ့
ရခေါ်ပါတယ့်။
▪ Path: A ကရန H ကိုသာွားမယ့်ဆို င့် ABDH ဆိုပပြီွား သာွား မာ ြစ့်ပါတယ့်။ AB, BD, DH ဆိုတွဲ edge
ရခေါ်ပါတယ့်။
▪ Root: parent မ တွဲ node ကို Root node ိုဲ့ ရခေါ်ပါတယ့်။ ပံိုမာဆို င့်ရတာ A node
ြစ့်ပါတယ့်။
▪ Leaf: ထပ့်ခွဲထာွားတွဲ child တစ့်ရယာက့်မ မ တွဲ node ရတကို ရတာ leaf node ိုဲ့ ရခေါ်ပါတယ့်။
ပထာွားတွဲ ပံမ
ို ာရတာ E, F, G, H, I တိုဲ့ ြစ့်ပါတယ့်။
▪ Subtree: Tree ဆိုတာ subtree ရ ွားရတနွဲဲ့ ြှေ့ွဲ စည့်ွားထာွားတာ ြစ့်ပါတယ့်။ ဒြီမာ တတဂံပံိုစံ
ရ ွားရတ ဝိုင့်ွား ပထာွားတာ ရ ွားကို subtree ိုဲ့ ရခေါ်ပါတယ့်။ subtree တစ့်ခိုချင့်ွားမာ ွဲ Root
▪ Level: Level သတ့်မတ့်တွဲ အခါမာ Root node ကို Level 0 ိုဲ့ သတ့်မတ့်ပပြီွား ရအာက့်ရ ာက့်ရ
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
ရအာက့်ကပံက
ို ို ကည့်ပပြီွား ရအာက့်ပါရမွားခန့်ွားရတကို ရ ြပါ။
299 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
10. A ကရန J ကိုသာွား ိုဲ့ မည့် path ကို ာပါ။ အွဲဒြီ path ထွဲမာ Edge ဘယ့်နစ့်ခို ပါပါသ ွဲ။
B C D
E F G H I
J K
Binary Tree
Binary tree မာ
▪ Node ရတဟာ Left child နွဲဲ့ Right child ဆိုပပြီွား အမျာွားဆံိုွား child ၂ ခိုပွဲ ပါမယ့်။
အမျာွားဆံိုွား ိုဲ့ ရ ပာထာွားတာ ြစ့် ိုဲ့ node ရတဟာ child မ ည့်ွား တယ့်၊ child ၁
ခိုပွဲ ည့်ွား တယ့်၊ child ၂ခို ည့်ွား တယ့်၊ child ၂ ခိုထက့်ရတာ ရကျာ့် ိုဲ့ ပါဘွား။
မ
▪ Left child ွဲှေ့ key ဟာ parent node ွဲှေ့ key ထက့်ငယ့် ပါမယ့်။
▪ Right child ွဲှေ့ key ဟာ parent node ွဲှေ့ key ထက့် တကြီွား ပါမယ့်။
300 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
key ယ့်၊ Left child ယ့်၊ Right child ယ့်ဆိုပပြီွား ပါမာ ြစ့်ပါတယ့်။ ထံိုွားစံအတင
ို ့်ွား key ကရတာ
ကိုယတ့် ကြိုက့်တွဲ data type ထာွား ိုဲ့ ပါတယ့်။ Left child နွဲဲ့ Right child ကရတာ node ရတကို
ရထာက့်မာ ြစ့် ိုဲ့ node pointer ြစ့်မာ ြစ့်ပါတယ့်။ Left child ခွဲ င့် Left child ထွဲကို Left child
ြစ့်တွဲ node ကိုသမ့်ွားပပြီွား၊ မ ခွဲ င့် Left child ထွဲကို NULL ထည့်မာ ြစ့်ပါတယ့်။ Right child
3 7
9
2 4
▪ 5 node ဟာ Root node ြစ့်ပါတယ့်။ Left child ွဲှေ့ key ြစ့်တွဲ 3 ဟာ သူ့ ွဲှေ့ key ထက့်ငယ့်ပပြီွား၊
▪ 3 node မာ ည့်ွား Left child ွဲှေ့ key ြစ့်တွဲ 2 ဟာ သူ့ထက့်ငယ့်ပပြီွား၊ Right child ွဲှေ့ key ြစ့်တွဲ 4
ဟာ သူ့ထက့်တကြီွားပါတယ့်။
▪ 7 node မာရတာ Right child တစ့်ခိုပွဲ ပါတယ့်။ Right child ြစ့်တွဲ အတက့်
သူ့ထက့်တကြီွားပါတယ့်။ Left child မ တွဲ အတက့် Left child တန့်ြိုွားဟာ NULL ြစ့်ရနမာ
ြစ့်ပါတယ့်။
301 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဒြီရတာ Binary tree မာ ာချင့်တာပွဲ ြစ့် ြစ့်၊ ထည့်ချင့်တာပွဲ ြစ့် ြစ့်၊ ြျက့်ချင့်တာပွဲ ြစ့် ြစ့် Left
ထည့်ထာွားြဲ့ို ိုပါတယ့်။
တန့်ြိုွားရ ွား တစ့်ခိုရပွားပပြီွား Binary tree ထွဲကရန အွဲဒြီ တန့်ြွားို နွဲဲ့ ညြီတွဲ key တွဲ node ကို
ိုက့်ပပြီွား ာပါမယ့်။
▪ သာွား င့်ွား NULL နွဲဲ့ညြီသာွား င့် မရတှေ့ရတာတာ ြစ့် ိုဲ့ မရတှေ့ရ ကာင့်ွား NULL return
ပန့်ပါမယ့်။
Find (x)
302 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Root node ကရနစပပြီွား ာမာ ြစ့် ိုဲ့ line 3 မာ Current ထွဲကို Root ထည့် ိုက့်ပါတယ့်။
Current ဆိုတာကရတာ Linked list အခန့်ွားက PTR ိုပွဲ ို ာကိုသာွားြဲ့ို သံိုွားတာ ြစ့်ပါတယ့်။
ညြီသာွားမယ့်။ ဒါရ ကာင့် line 4 ွဲှေ့ while မာ မရတှေ့ရတာ ိုဲ့ NULL နွဲဲ့ ညြီ င့် ည့်ွား ထက့်၊ သိုဲ့မဟိုတ့်
ဝင့် ာတွဲ x နွဲဲ့ Current node ွဲှေ့ key နွဲဲ့ ညြီသာွား ည့်ွား ထက့်ဆိုပပြီွား ရ ွားထာွားပါတယ့်။ ဒြီရတာ
ခိုပွဲ တယ့်။ ငယ့်တာနွဲဲ့ တကြီွားတာ။ ငယ့် င့် Current ကို Current ွဲှေ့ Left child ကိုရ ှေ့၊ တကြီွား င့်
Current ကို Current ွဲှေ့ Right child ကို ရ ှေ့ဆိုပပြီွား looping body ထွဲ ရ ွားထာွားတာ
ြစ့်ပါတယ့်။
ညြီသာွား ိုဲ့ သမ
ိုဲ့ ဟိုတ့် Current ဟာသာွား င့်ွား သာွား င့်ွား ညြီတာမရတှေ့ရတာဘွဲ NULL
ရ ာက့်သာွားတာ။
1. ဒါဆို Current ဟာ NULL နွဲဲ့ ညြီ င့် မရတှေ့ရတာတာ ြစ့် ိုဲ့ NULL return ပန့်မယ့်။
Current က NULL ြစ့်ရနတွဲ အချန့်မာ NULL ကို return ပန့်တာရ ကာင့် return
2. ထိုဲ့အတပွဲ Current→ key ဟာ x နွဲဲ့ ညြီ ိုဲ့ ထက့် ာတာဆို င့် ည့်ွား Current မာ
303 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
ပထာွားတွဲ Tree ထွဲကရန 25 ကို ာတာရ ွားကို arrow ရ ွားရတနွဲဲ့ trace ိုက့် ပထာွားပါတယ့်။
ာချင့်တာက 25၊
▪ 25 ကိုရ ာက့်တယ့်။ ညြီသာွားပပြီ ြစ့် ိုဲ့ အွဲဒြီ node ကို return ပန့် ိုက့်တယ့်။
60
50 70
0 0
20 55 90
NULL
0 0 0
10 25
0 0
304 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ 70 မာ Left child မ ရတာ Left child ရန ာ NULL ြစ့်ရနမယ့်။ ဒြီရတာ 70 ွဲှေ့ Left child
ကိုသာွားတွဲ Current ဟာ NULL နွဲဲ့ ညြီသာွားမယ့်။ မရတှေ့ရ ကာင့်ွား NULL ကို return
ပန့်ရပွား ိုက့်မယ့်။
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
ဆွဲ ကည့်ပါ။ Left child ဟာ parent ထက့်ငယ့် မယ့်၊ Right child ဟာ parent ထက့်တကြီွား မယ့်
ဆိုတာကို မရမပါနွဲ။ဲ့
305 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Root = newNode
60 60
0 0
50 70 50 70
0 0 0 0
20 55 20 55
0 0 0 NULL
0
NULL
ပထမပံိုကရတာ 25 ထည့်ချင့်တာအတက့် ဆွဲ ပထာွားတာ ြစ့်ပါတယ့်။
▪ ထံိုွားစံအတင
ို ့်ွား Current ဟာ Root node ကရနပပြီွား စမယ့်။
▪ ထည့်ချင့်တာက 25 ဆိုရတာ Current ရ ာက့်ရနတွဲ 60 ထက့်ငယ့် ိုဲ့ Current ကို Left child ကို
ရ ှေ့မယ့်။
အွဲဒြီရန ာမာ ထည့် မာ ြစ့်ပါတယ့်။ Right child သာွား င့်ွား NULL နွဲဲ့ ြစ့်သာွားတာ ြစ့် ိုဲ့ NULL
မရ ာက့်ခင့် 20 node ွဲှေ့ Right child အရနနွဲဲ့ newNode(25) ကို ထည့် မာ ြစ့်ပါတယ့်။ သရ
ိုဲ့ သာ့်
ရနောက့်ရ ှေ့မယ့် ိုပ့်တိုင့်ွား Current မရ ှေ့ခင့် Current ရန ာကို Prev ကို ထာွားခွဲပါတယ့်။ ဒြီရတာမ
306 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
▪ ထံိုွားစံအတင
ို ့်ွား Current ဟာ Root node ကရနပပြီွား စမယ့်။
▪ ထည့်ချင့်တာက 67 ဆိုရတာ Current ရ ာက့်ရနတွဲ 60 ထက့်တကြီွား ိုဲ့ Current ကို Right child ကို
ရ ှေ့မယ့်။ Current ကို Right child မရ ှေ့ခင့် Prev ကို Current ရန ာ ထာွားခွဲပါမယ့်။ ဒြီရတာ Prev
child ကို ရ ှေ့မယ့်။ Current မရ ှေ့ခင့် Current ရန ာကို Prev ကို ရပွားခွဲမယ့်။ Prev ဟာ 70 မာ
▪ Current NULL ြစ့်သာွားတာဟာ ရန ာ တ့်တာ ြစ့်ပါတယ့်။ Left child သာွား င့်ွား NULL
အချြိုပ့် ပန့်ရ ပာ င့် Current သည့် Root ကရနစမယ့်။ ရန ာ တ့်၊မ တ့်သ ရအာင့် Current ကို
Left child ကို သာွားတာ ာွား၊ Right child ကိုသာွားတာ ာွားဆိုတာ မတ့်ထာွားြဲ့ို ိုတယ့်။ အွဲဒြီအတက့်
isLeftChild ဆိုတွဲ variable ကို အသံိုွား ပြိုပါမယ့်။ Left child ကိုသာွား င့် isLeftChild ထွဲ true ထည့်ပပြီွား
Insert ( newNode )
Root = newNode
307 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Prev = Current
isLeftChild = true
Else:
isLeftChild =false
End of Loop
If isLeftChild then:
Else:
Set isLeftChild = true ဆိုတွဲ စာရ ကာင့်ွားဟာ ဒြီ variable ကို အသံိုွား ပြိုမည့်အရ ကာင့်ွား သြဲ့သ
ို ာ
ြစ့်ပါတယ့်။ true ထည့်ထည့်၊ false ထည့်ထည့် အရ ွားမပါ ပါဘွား။ ဘာရ ကာင့် ွဲဆို င့်
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
308 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
Traversing ဆိုတာရတာ linked list နွဲဲ့ တင့် သခွဲရ ာက့်ပါပပြီ။ Node အာွား ံိုွားကို
1. Pre-order (Root, L, R)
သာွားမာ ြစ့်ပပြီွား၊ in ဆိုတာ အ ယ့် ဆိုတွဲ အဓပပါယ့် ြစ့်တာရ ကာင့် Left, Right ကာွားမာ Root ဆိုပပြီွား
(Left, Root, Right) သာွားမာ ြစ့်ပါတယ့်။ Post ကရတာ ရနောက့်ဆိုတွဲ အဓပပါယ့် ြစ့် ိုဲ့ Left, Right,
အ င့်သာွားမ ွဲဆိုတာရတာ အွဲဒြီ ွဲှေ့ ဆံိုွား ြတ့်ချက့်ရပေါ်မတည့်ပါတယ့်။ ဒြီ ိုပွဲ ဒြီမာ ွဲ pre-order,
309 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Pre-order Traversing
Pre-order ဆိုရတာ Root ကို အ င့် output ထိုတ့်ပါမယ့်။ ပပြီွားမာ Left child, Right Child
ာမာ ြစ့်ပါတယ့်။
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
ဒြီပိုက
ံ ို pre-order နွဲဲ့ output ထိုတ့် ကည့်ပါမယ့်။
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
310 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Root, Left, Right ဆိုရတာ Root ြစ့်တွဲ 60 ကို အ င့် output ထိုတ့် ိုက့်ပါမယ့်။ Root ပပြီွား င့် Left
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
50 ကို ပန့် ကည့် ိုက့်ရတာ တတဂံပံိုစံ ဝိုင့်ွား ပထာွားတွဲ subtree ရ ွား ွဲှေ့ Root ပန့် ြစ့်ရနတယ့်။ Root
အ င့် ိုပ့်မာဆိုတွဲအတက့် 50 ကို output ထိုတ့် ိုက့်တယ့်။ Root ပပြီွားရတာ Left ဆိုရတာ Left child
20 ကိုသာွားပါမယ့်။
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
မင့်တအတ
ွဲ င
ို ့်ွားပါပွဲ။ 20 ဟာ အခို ပထာွားတွဲ subtree ွဲှေ့ Root ြစ့်ရနတွဲအတက့် Root အ ြစ့် 20 ကို
အ င့်ထိုတ့် ိုက့်ပါမယ့်။ Root အရနနွဲဲ့ ထိုတ့်တာ ြစ့် ိုဲ့ Root ပပြီွား င့် Left 10 ကိုသာွားပါမယ့်။
311 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
10 ကို Root အရနနွဲဲ့ ထိုတ့် ိုက့်ပါတယ့်။ Root, Left, Right ဆိုရတာ Left child နွဲဲ့ Right child
ကို ကည့်ပါတယ့်။ 10 မာ Left child ရ ာ၊ Right child ပါ NULL ြစ့်ရန ိုဲ့ မ ိုဲ့
ဆက့်သာွားစ ာမ ိုပွဲ ပပြီွားသာွားပါတယ့်။ 20 node ကရန ကည့် ိုက့် င့် 20 Root ပပြီွားသာွားပပြီ၊ အခို Left
10 ပပြီွားသာွားပပြီ။ ဒြီရတာ Root, Left, Right မာ Root, Left ပပြီွားသာွားပပြီ ြစ့် ိုဲ့ Right 25
ခယ့်ရပွားထာွားပါတယ့်။ 60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
312 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ဆက့်မသာွား ရတာဘွား။ အခိုဆို င့် ထိုတ့် ပ ပိုဲ့ ပြီွားသာွားတွဲ တတဂံပံို ဝိုင့်ွား ပထာွားတာရ ွားကို ကည့်ပါ။
သတဟ
ိုဲ့ ာ 50 ွဲှေ့ Left child ရတ ြစ့်ပါတယ့်။ 50 ဆိုတွဲ Root ကို ထိုတ့်ပပြီွားသာွားပပြီ၊ ပပြီွားရတာ သ ွဲှေ့ Left
ကို ည့်ွား ထိုတ့်ပပြီွားသာွားပပြီ ြစ့် ိုဲ့ ဒြီတစ့်ခါ ဆက့်သာွား မာကရတာ 50 ွဲှေ့ Right child ြစ့်တွဲ 55
ြစ့်ပါတယ့်။ 60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
55 ကို ည့်ွား Root node အရနနွဲဲ့ ထိုတ့်ပါတယ့်။ ပပြီွားရတာ သူ့ ွဲှေ့ Left child, Right child ရတကို
ကည့်ပါရသွားတယ့်။ ဒါရပမွဲ သမာ Left child ရ ာ၊ Right child ရ ာ မ ိုဲ့ ပ့်သာွားပါတယ့်။ တတဂံနွဲဲ့
ပထာွားတာကို ကည့် ိုက့် င့် 60 ဟာ Root အရနနွဲဲ့ ထိုတ့်ပပြီွားသာွားပပြီွား သူ့ ွဲှေ့ Left တစ့် ခမ့်ွား ံိုွား ည့်ွား
ထိုတ့်ပပြီွားသာွားပါပပြီ။ Root, Left ပပြီွားရတာ ဒြီတစ့်ခါ သာွား မာက Right ြစ့် ိုဲ့ 60 ွဲှေ့ Right child 70 ကို
သာွားပါမယ့်။
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
70 ကို Root node အရနနွဲဲ့ ထိုတ့်ပါတယ့်။ Root ပပြီွားရတာ Left ကိုသာွားမယ့်။ 70 မာ Left ကမ ရတာ
313 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
90 ကို Root node အရနနွဲဲ့ ထိုတ့်ပါတယ့်။ Root ပပြီွား ရတာ Left, Right ကို ကည့်တယ့်။ 90
ြစ့်ပါတယ့်။ ဒြီရန ာမာ node တစ့်ခိုကို ရ ာက့် င့် Root အ ြစ့်ထိုတ့်၊ ပပြီွား င့် Left child သာွား၊
Left child ကို Root အ ြစ့် ိုပ၊့် ပပြီွားရတာ Root အ ြစ့် ိုပတ
့် ာရ ကာင့် Left child ဆက့်သာွား ဆိုပပြီွား
ဘက့်ရ ာက့်သာွားတွဲ node ကို ည့်ွား Root အ ြစ့် ပန့် ိုပ့်။ ဆိုရတာ Left, Right ရတဟာ Root
အ ြစ့် recursion ပန့်ရခေါ်သ ို ြစ့်ရနတယ့်။ ဒါရ ကာင့် pre-order algorithm ကို ရ ွားမယ့် ဆို င့်
preOrder(Node localRoot)
Write localRoot→Key, “ ,”
preOrder(localRoot → LeftChild)
preOrder(localRoot → RightChild)
314 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
In-order Traversing
inOrder(Node localRoot)
inOrder(localRoot → LeftChild)
Write localRoot→Key, “ ,”
inOrder(localRoot → RightChild)
Post-order Traversing
postOrder(Node localRoot)
postOrder(localRoot → LeftChild)
postOrder(localRoot → RightChild)
Write localRoot→Key, “ ,”
315 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
Root node ြစ့်တွဲ 60 ကရနစမယ့်။ Left, Right, Root ဆိုတွဲအတက့် Root ကရနောက့်ဆံိုွားမ
Left က NULL ြစ့်ရနရတာ Left မ ရတာ ိုဲ့ Left ပပြီွားပပြီ ြစ့်တယ့်။ ဒြီရတာ 10 ွဲှေ့ Right ကိုသာွားတယ့်။
Right မ ရတာ Right ည့်ွားပပြီွားသာွားပပြီ။ Left, Right , Root ဆိုရတာ Left, Right ပပြီွားသာွား ိုဲ့ 10 ကို
10 ဆိုတာဟာ 20 ွဲှေ့ Left ြစ့်တယ့်။ 20 ွဲှေ့ Left ပပြီွားရတာ 20 ွဲှေ့ Right ကိုသာွားတယ့်။ 25
ကိုရ ာက့်တယ့်။ 25 ကို Root node အ ြစ့် ကည့်တယ့်။ Root ဆိုရတာ Left, Right ကို
အ င့်သာွား မယ့်။ 25 မာ Left, Right မ ရတာ ပပြီွားပပြီ ိုဲ့ ယဆပပြီွား 25 ကို Root အရနနွဲဲ့
ထိုတ့် ိုက့်တယ့်။
316 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
20 ကရန ကည့် ိုက့် င့် သူ့ ွဲှေ့ Left 10, သူ့ ွဲှေ့ Right 25 အ ိုပ့် ိုပ့် ိုဲ့ ပပြီွားသာွားပပြီ။ Left, Right ပပြီွားရတာ
20 ကို Root အရနနွဲဲ့ ထိုတ့် ိုက့်တယ့်။ တတဂံနွဲဲ့ ပထာွားသ ိုပွဲ 50 ကရန ကည့် ိုက့် င့် သူ့ ွဲှေ့ Left
60
0
50 70
0 0
20 55 90
0 0 0
10 25
0 0
55 ကို Root အ ြစ့် ကည့်တယ့်။ Left, Right, Root ြစ့် ိုဲ့ 55 ွဲှေ့ Left, Right ကိုသာွားတယ့်။
မ ရတာမ Left, Right ပပြီွားပပြီ ြစ့် ိုဲ့ 55 ကို Root အ ြစ့်ထိုတ့် ိုက့်တယ့်။
55 ထိုတ့် ည့်ွားပပြီွားရ ာ 50 ဟာ သူ့ ွဲှေ့ Left, Right ပပြီွားသာွားပပြီ ြစ့် ိုဲ့ 50 ကို Root အ ြစ့်ထိုတ့်တယ့်။ 50
ည့်ွား ထိုတ့်ပပြီွားသာွားရတာ 60 ကရန ကည့် ိုက့် င့် သူ့ ွဲှေ့ Left ဘက့် ခမ့်ွားပပြီွားသာွားပပြီ ြစ့်ပါတယ့်။
317 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
60
50 70
0 0
20 55 90
0 0 0
10 25
0 0
ထံိုွားစံအတင
ို ့်ွား 70 ကို Root အ ြစ့် ကည့်မယ့်။ Left, Right, Root ဆိုရတာ 70 ွဲှေ့ Left ကိုသာွားတယ့်။
Left မ ရတာ Left ပပြီွားပပြီ ြစ့် ိုဲ့ 70 ွဲှေ့ Right ကိုသာွားတယ့်။ 90 ကိုရ ာက့်တယ့်။ 90 ကို Root အ ြစ့်
ကည့်မယ့်။ 90 ွဲှေ့ Left, Right ကို အ င့်သာွားတယ့်။ မ ရတာ ိုဲ့ Left, Right ပပြီွားမ 90 ကို Root
အ ြစ့်ထိုတ့်တယ့်။ 90 ပပြီွားတာ 70 ွဲှေ့ Left, Right ပပြီွားသာွား ိုဲ့ 70 ကို Root အ ြစ့်ထိုတ့် ိုက့်တယ့်။ 70
ည့်ွား ထိုတ့်ပပြီွားသာွားရတာ 60 ဟာ Left ဘက့် ခမ့်ွားရ ာ၊ Right ဘက့် ခမ့်ွားပါ ပပြီွားသာွားပပြီ ြစ့် ိုဲ့ 60 ကို
Root အ ြစ့်ရနောက့်ဆံိုွားထိုတ့် ိုက့်တယ့်။ node အာွား ံိုွားကို သာွားပပြီွားပပြီ ြစ့် ိုဲ့ ပပြီွားသာွားပပြီ ြစ့်ပါတယ့်။
ဆိုပပြီွား အရ ြထက့်ပါတယ့်။
318 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
60
44 88
22 55 99
77
0
11 33
66
Heap
Insert 35 35
319 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Insert 33: 33 ဝင့် ာရတာ 35 ွဲှေ့ ရအာက့်မာ တ့်တွဲ Left child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
35
33
Insert 42: 42 ဝင့် ာရတာ 35 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Right child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
35 42
33 42 33 35
Insert 10: 10 ဝင့် ာရတာ 33 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Left child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
42
33 35
10
320 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Insert 14: 14 ဝင့် ာရတာ 33 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Right child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
42
33 35
10 14
Insert 19: 19 ဝင့် ာရတာ 35 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Left child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
42
33 35
10 14 19
Insert 27: 27 ဝင့် ာရတာ 35 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Right child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
42
33 35
10 14 19 27
321 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Insert 44: 44 ဝင့် ာရတာ 10 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Left child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
42 42
33 35 33 35
10 14 19 27 44 14 19 27
42
44
44 35
42 35
33 14 19 27
33 14 19 27
Insert 26: 26 ဝင့် ာရတာ 33 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Right child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
322 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
44
42 35
33 14 19 27
10 26
Insert 31: 31 ဝင့် ာရတာ 14 ွဲှေ့ ရအာက့်မာ တ့်ရနတွဲ Left child အရနနွဲဲ့ ထည့်မာ ြစ့်ပါတယ့်။
44
42 35
44
33 19 27 42 35
14
33 31 19 27
10 26 31
10 26 14
323 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Heap Sort
50
30 40
10 20
20
50
30 40
30 40
40
30 20
324 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
10 30
30 20 10 20
ိုပ့်စ ာမ ိုဘွား။ 20
10
10
လ ေ့ကျငေ့ကကညေ့ကကရလအောင။
LA = { 35, 33, 42, 10, 14, 19, 27, 44, 26, 31 } ကို Min-Heap ပံစ
ို ံ ြင့်သမ့်ွားပါ။ ပပြီွား ျှင့် heap
325 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အခန်း (၁၇)
Graph
Graph ဆိုတာဘာလဲ။
M Y
N S
ဒမြော အဝိုင််းလေ်းလ ွေနဲ့ ပြထြော်း မမ ဲ့အမည်လ ွေကို vertex ေိုဲ့လခေါ်မြ်း vertex လ ွေအ ကြော်း
မမ ဲ့လ ွေကို သွေြော်းေိုဲ့ရ ယ်။ Yangon ကလန Mandalay, He Hoe, Nyaung U, Sittwe စ မမ ဲ့လ ွေကို
သွေြော်းေိုဲ့ရ ယ်။ He Hoe ကလန Mandalay နင် Yangon၊ Nyanung U ကလနေည််း Mandalay နင်
အဒါကို undirected graph ေိုဲ့ လခေါ်ြါ ယ်။ arrow ြါခရင်လ ြော directed graph ေိုဲ့ လခေါ်ြါ ယ်။
326 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
A D
C
ဒမြောဆိုရင် A ကလန B,C, D ကိုသွေြော်းေိုဲ့ရမယ်။ B, C ကလန ယ်မ သွေြော်းမရ ူ်း။ D ကလန B, C ကို
နည််းရြါ ယ်။
1. Depth-First Search
Adjacency Matrix
Graph ကို ပမင်သြောလအြောင် Adjacency Matrix နဲ့ ပြလြ်းေိုဲ့ရြါ ယ်။ အလြေါ်မြော နမူနာပြခ
လေလ ကြောင််းခရ်းစဉ် Undirected Graph အ ွေက် Adjacency Matrix ဆွေ ကည်ြါမယ်။ ရသမျှ vertex
ကိုယ်မမ ဲ့ကိုယပ် ြန်သွေြော်း ြော မရေိုဲ့ 0 ပြစ်ြါ ယ်။ စ်ချ ဲ့ ရြော်းရြော်းြါ်းြါ်းလနရြောလ ွေမြောလ ြော
မမကိုယက
် ိုပြန်ညန
ွေ ််း edge လ ွေ ြါ ်ြါ ယ်။
H M N S Y
H 0 1 0 0 1
M 1 0 1 0 1
N 0 1 0 0 1
327 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
S 0 0 0 0 1
Y 1 1 1 1 0
လအြောက်က table ကလ ြော အလြေါ်မြောပြခ directed Graph အ ွေက် Adjacency Matrix ပြစ်ြါ
ယ်။
A B C D
A 0 1 1 1
B 0 0 0 0
C 0 0 0 0
D 0 1 1 0
Adjacency List
Graph ကို ပမင်သြောလအြောင် Adjacency List နဲ့ ေည််း ပြလြ်းေိုဲ့ရြါ ယ်။ အလြေါ်မြော ပြခ
Vertex
H M→Y
M H→N→Y
N M→Y
S Y
Y H→M→N→S
နာ်းေည်ေွေယ် ြောလ ကြောင် ရင််းပြစရြော မေိုလေြောက် ူ်း ထင်ြါ ယ်။ အလြေါ်မြော ပြခ directed
Vertex
A B → C →D
328 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
D B→C
B F G
C H
A
E I J
G မြ်းလ ြော ဆက်သွေြော်းစရြော ရေြော်း ကည် ယ်။ မရလ ြော လရဲ့ပြန်ေြော ယ်။ F လရြောက် ယ်။
329 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
သွေြော်းမယ်။
ကလန ဆက်မြ်းသွေြော်းမယ်။
ကလန ဆက်မြ်းသွေြော်းမယ်။
ကလန ဆက်မြ်းသွေြော်းမယ်။
Pop A, B, F သလ
ိုဲ့ သြော် G မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် G ကို
ဆက်မြ်းသွေြော်းြါမယ်။
ကလန ဆက်မြ်းသွေြော်းမယ်။
Pop A, B, F သလ
ိုဲ့ သြော် H မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် H ကို
ဆက်မြ်းသွေြော်းြါမယ်။
Pop A, B သလ
ိုဲ့ သြော် F မြော သွေြော်းစရြော အကိုန်သွေြော်းမြ်း သွေြော်းမြ။ ဒလ ြော
ဆက်မြ်းသွေြော်းြါမယ်။
330 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Pop A သလ
ိုဲ့ သြော် B မြော သွေြော်းစရြော အကိုန်သွေြော်းမြ်း သွေြော်းမြ။ ဒလ ြော
ဆက်မြ်းသွေြော်းြါမယ်။
သွေြော်းမယ်။
Pop A သလ
ိုဲ့ သြော် C မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် pop
ကလန ဆက်မြ်းသွေြော်းြါမယ်။
သွေြော်းမယ်။
Pop A သလ
ိုဲ့ သြော် D မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် pop
ကလန ဆက်မြ်းသွေြော်းြါမယ်။
သွေြော်းမယ်။
သွေြော်းမယ်။
သွေြော်းမယ်။
331 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Pop A, E, I သလ
ိုဲ့ သြော် J မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် pop
ကလန ဆက်မြ်းသွေြော်းြါမယ်။
Pop A, E သလ
ိုဲ့ သြော် I မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် pop
ကလန ဆက်မြ်းသွေြော်းြါမယ်။
Pop A သလ
ိုဲ့ သြော် E မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် pop
ကလန ဆက်မြ်းသွေြော်းြါမယ်။
Pop သလ
ိုဲ့ သြော် A မြော ဆက်သွေြော်းစရြောမရလ ြော ူ်း။ ဒါလ ကြောင် pop
Breadth-First Search (BFS) ကလ ြော Depth-First Search(DFS) နဲ့ လပြြောင််းပြန် ပြစ်ြါ ယ်။ သူက
အရင်လရြောက် ဲ့ vertex နဲ့ ဆက်စြ် ြောလ ွေကို အရင်သွေြော်းမြော ပြစ်ေိုဲ့ Queue ကို အသို်းပြ ရမြော
ပြစ်ြါ ယ်။ DFS မြော နမူနာပြခ graph ကို BFS နဲ့ အေိုြ်ေိုြ် ကည်ြါမယ်။
332 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
ေိုက်ြါမယ်။
Delete ြါမယ်။
333 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Delete ြါမယ်။
Delete ြါမယ်။
Visit G I, G
334 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
Delete ြါမယ်။
Delete ြါမယ်။
လလေ့ကျငေ့ကကညေ့ကကရလအာင။
K
B F
L
C G
A
H
D
E J M
335 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
M Y
N S
Chapter အနှစချျုပ
ဆိုရင် vertex နဲ့ ူြါ ယ်။ စ အခန််းကလန ဆက်စြ်လန အခန််းလ ွေပြ ်မြ်း goal ကို
လရြောက်လအြောင် သွေြော်းမြောပြစ်ြါ ယ်။ အဒေို အလပခအလနမျ ်းမြော DFS, BFS ကို သို်းေရ
ိုဲ့ ြါ ယ်။
336 | P a g e
Data Structure and algorithms for beginners | Dr. Myat Mon Aye
အဆုံးသတ်
အခုစာအုပဟ
် ာ Beginners တ ွေကု ရည်ရွယ် ာ ဖြစ်လုို့ အတဖခခံသတ ာ ရာားတ ွေ နို့
ပါ ယ်။ ြ ် သူ န ားလည်တစြုို့ န
် ုင်သမ ကက ားစာားပပား တရားထာားပါ ယ်။
န ားမလည် ာ စ်စံု စ်ခုရှရင် တေြန် အကကံဖပ နုင်ပါ ယ်။ အကကံဖပ ချက်မျာားကုလည်ား တမ ာ်လင်
တနပါ ယ်။ န
် ုင်သမ ြ ်ရသူ အကျ ားရှတအာင် အဆင်မတဖပ ာတ ွေကု တန က်ကတနလုက်ပပား
ဖြည်ဆည်ားတပားြို့ု ကက ားစာားသွောားပါမယ်။
ဒစာအုပပ် ပားရင်တ ာ အခုတခ ်တဖပာတဖပာတန တနရာ ကာ မှာ မပါမဖြစ် Data Science နို့
တကျာင်ား က် န
ု ်ားက အကက က်ဆံုား ာသာရပ်တ ွေဖြစ်ခပပား၊ Master Thesis တရာ၊ Ph.D. Thesis
တကျားဇူားအထူား င်စွောဖြင်
9-5-2021
337 | P a g e