Selection Sort on Objects (object တွေကို selection sort နဲ့ စီခြင်း)
B2.4.3, B3.1: Apply sorting algorithms to arrays of objects using getter methods and attribute comparison.
မြန်မာလို: getter method (private attribute ရဲ့ တန်ဖိုးကို ပြန်ပေးတဲ့ method) တွေကို သုံးပြီး attribute (object ရဲ့ ဒေတာကို သိမ်းတဲ့ variable) တွေကို နှိုင်းယှဉ်ကာ object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance) array (အရွယ်အစား ပုံသေ၊ အမျိုးအစားတူ ဒေတာစု) တွေပေါ်မှာ sorting algorithm (အစဉ်တကျ စီပေးတဲ့ အယ်လ်ဂိုရီသမ်) တွေကို အသုံးချနိုင်ရပါမယ်။
အဓိက အယူအဆများ
Object တွေကို ဘာကြောင့် စီရတာလဲ
Sorting Algorithms (ဒေတာကို အစဉ်လိုက် စီတဲ့ အယ်လ်ဂိုရီသမ်များ) စာမျက်နှာမှာ int တန်ဖိုးတွေရဲ့ array တွေကို စီခဲ့ပါတယ်။ တကယ့် program တွေမှာတော့ object တွေရဲ့ array တွေကို စီရပါတယ်: ကျောင်းသားတွေကို grade (အမှတ်) အလိုက်၊ ကုန်ပစ္စည်းတွေကို ဈေးနှုန်း အလိုက်၊ ဝန်ထမ်းတွေကို နာမည် အလိုက် စီတာမျိုးပါ။ စီတဲ့ logic က အတူတူပါပဲ၊ ဒါပေမယ့် တန်ဖိုးတွေကို တိုက်ရိုက် နှိုင်းယှဉ်မယ့်အစား getter method တွေကနေ ယူတဲ့ attribute တွေကို နှိုင်းယှဉ်ပါတယ်။
ဂဏန်း Attribute အလိုက် စီခြင်း
Student object တွေကို grade အလိုက် (ascending (ငယ်ရာကနေ ကြီးရာ)) စီဖို့ တိုက်ရိုက် < comparison (တန်ဖိုး နှစ်ခုကို နှိုင်းယှဉ်ခြင်း) တွေကို getter ခေါ်တာတွေနဲ့ အစားထိုးပါ:
// Selection sort: sort students by grade (ascending)
public static void sortByGrade(Student[] arr, int count) {
for (int i = 0; i < count - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < count; j++) {
if (arr[j].getGrade() < arr[minIndex].getGrade()) {
minIndex = j;
}
}
// Swap references, not individual fields
Student temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
# Selection sort: sort students by grade (ascending)
def sort_by_grade(arr, count): # count = how many students are in the list
for i in range(count - 1):
min_index = i
for j in range(i + 1, count):
if arr[j].get_grade() < arr[min_index].get_grade():
min_index = j
# Swap references, not individual attributes
temp = arr[min_index]
arr[min_index] = arr[i]
arr[i] = temp
int[] ကို စီတာနဲ့ အဓိက ကွာခြားချက်များ:
- Comparison က
arr[j]အစားarr[j].getGrade()ကို သုံးပါတယ် - Swap (တန်ဖိုး နှစ်ခု နေရာချင်း လဲခြင်း) က primitive (Java ရဲ့ အခြေခံ ဒေတာ အမျိုးအစား) တန်ဖိုးတွေကို မဟုတ်ဘဲ object reference (object ရှိရာ memory နေရာကို ညွှန်းတဲ့ တန်ဖိုး) တွေကို ရွှေ့ပါတယ်
- temp variable (တန်ဖိုး သိမ်းထားတဲ့ နာမည်ပေးထားတဲ့ နေရာ) က
intမဟုတ်ဘဲStudenttype ပါ
String Attribute အလိုက် စီခြင်း
နာမည်ကို အက္ခရာစဉ်အလိုက် စီဖို့ .compareTo() ကို သုံးပါ:
// Selection sort: sort students by name (alphabetical)
public static void sortByName(Student[] arr, int count) {
for (int i = 0; i < count - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < count; j++) {
if (arr[j].getName().compareTo(arr[minIndex].getName()) < 0) {
minIndex = j;
}
}
Student temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
# Selection sort: sort students by name (alphabetical)
def sort_by_name(arr, count):
for i in range(count - 1):
min_index = i
for j in range(i + 1, count):
# Python has no compareTo(): < compares strings alphabetically
if arr[j].get_name() < arr[min_index].get_name():
min_index = j
temp = arr[min_index]
arr[min_index] = arr[i]
arr[i] = temp
.compareTo()က boolean (true ဒါမှမဟုတ် false တန်ဖိုးပဲ ယူတဲ့) တန်ဖိုး မဟုတ်ဘဲ int ကို return (ရလဒ်အဖြစ် ပြန်ပေး) လုပ်ပါတယ်။ ပထမ string (စာသား) က ဒုတိယ string ထက် အက္ခရာစဉ်အရ အရင်လာရင် negative number (အနုတ်ကိန်း)၊ တူရင် သုည၊ နောက်မှလာရင် positive number (အပေါင်းကိန်း) ကို return ပြန်ပေးပါတယ်။ Ascending (A-Z) အတွက်< 0ကို၊ descending (ကြီးရာကနေ ငယ်ရာ၊ Z-A) အတွက်> 0ကို သုံးပါ။
.compareTo() အလုပ်လုပ်ပုံ
| Expression (ရလဒ် တန်ဖိုးတစ်ခု တွက်ထုတ်ပေးတဲ့ code အပိုင်း) | ရလဒ် | အဓိပ္ပာယ် |
|---|---|---|
"Alice".compareTo("Bob") | Negative (ဥပမာ -1) | “Alice” က “Bob” ထက် အရင်လာပါတယ် |
"Bob".compareTo("Alice") | Positive (ဥပမာ 1) | “Bob” က “Alice” ထက် နောက်မှလာပါတယ် |
"Alice".compareTo("Alice") | 0 | နှစ်ခု တူပါတယ် |
Ascending order (A ကနေ Z) အတွက်: arr[j].getName().compareTo(arr[minIndex].getName()) < 0 ဖြစ်တဲ့အခါ minIndex ကို update လုပ်ပါ၊ ဆိုလိုတာက j ခုမြောက် နာမည်က လက်ရှိ အငယ်ဆုံး နာမည်ထက် အက္ခရာစဉ်အရ အရင်လာပါတယ်။ (Python မှာ "Alice" < "Bob" က True ပါ: ပုံမှန် comparison operator (တန်ဖိုး နှစ်ခုကို နှိုင်းယှဉ်တဲ့ operator) တွေက string တွေကို အက္ခရာစဉ်အလိုက် နှိုင်းယှဉ်ပြီးသားမို့ compareTo() မရှိသလို လိုလည်း မလိုပါဘူး။)
Descending Order (ကြီးရာကနေ ငယ်ရာ)
Descending order (အမြင့်ဆုံး အရင်) နဲ့ စီဖို့ comparison ကို ပြောင်းပြန် လှန်ပါ:
// Descending: find MAXIMUM instead of minimum
if (arr[j].getGrade() > arr[maxIndex].getGrade()) {
maxIndex = j;
}
# Descending: find MAXIMUM instead of minimum
if arr[j].get_grade() > arr[max_index].get_grade():
max_index = j
String တွေကို descending (Z ကနေ A) စီဖို့: < 0 အစား > 0 ကို သုံးပါ။ (Python မှာ: နာမည်တွေကို > နဲ့ တိုက်ရိုက် နှိုင်းယှဉ်ပါ။)
Object Reference တွေကို Swap လုပ်ခြင်း
Array ထဲမှာ object တွေကို swap လုပ်တဲ့အခါ object တွေကိုယ်တိုင်ကို မဟုတ်ဘဲ reference တွေ (pointer (reference ကို ခေါ်တဲ့ နောက်နာမည်တစ်ခု) တွေ) ကို swap လုပ်တာပါ။ Object တွေက memory (မှတ်ဉာဏ်) ထဲမှာပဲ ရှိနေပါတယ်: array ရဲ့ အကွက်တွေက ဘယ် object ကို ညွှန်းမလဲ ဆိုတာပဲ ပြောင်းသွားတာပါ။
// Before swap: arr[0] -> Alice, arr[1] -> Bob
Student temp = arr[0]; // temp -> Alice
arr[0] = arr[1]; // arr[0] -> Bob
arr[1] = temp; // arr[1] -> Alice
// After swap: arr[0] -> Bob, arr[1] -> Alice
# Before swap: arr[0] -> Alice, arr[1] -> Bob
temp = arr[0] # temp -> Alice (Python variables hold references too)
arr[0] = arr[1] # arr[0] -> Bob
arr[1] = temp # arr[1] -> Alice
# After swap: arr[0] -> Bob, arr[1] -> Alice
Alice နဲ့ Bob object တွေကို copy မကူးသလို ပြင်လည်း မပြင်ပါဘူး။ Array ထဲက reference တွေပဲ ရွှေ့သွားတာပါ။
အဆင့်ဆင့် ရှင်းပြထားတဲ့ ဥပမာများ
ဥပမာ 1: ကုန်ပစ္စည်းတွေကို ဈေးနှုန်း အလိုက် စီခြင်း
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() { return name; }
public double getPrice() { return price; }
}
class Product():
def __init__(self, name, price):
self._name = name
self._price = price
def get_name(self):
return self._name
def get_price(self):
return self._price
Product[] shop = new Product[4];
shop[0] = new Product("Keyboard", 79.99);
shop[1] = new Product("Mouse", 29.99);
shop[2] = new Product("Monitor", 299.99);
shop[3] = new Product("Webcam", 49.99);
// Selection sort by price (ascending)
for (int i = 0; i < shop.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < shop.length; j++) {
if (shop[j].getPrice() < shop[minIndex].getPrice()) {
minIndex = j;
}
}
Product temp = shop[minIndex];
shop[minIndex] = shop[i];
shop[i] = temp;
}
// Print sorted
for (int i = 0; i < shop.length; i++) {
System.out.println(shop[i].getName() + ": $" + shop[i].getPrice());
}
shop = []
shop.append(Product("Keyboard", 79.99))
shop.append(Product("Mouse", 29.99))
shop.append(Product("Monitor", 299.99))
shop.append(Product("Webcam", 49.99))
# Selection sort by price (ascending)
for i in range(len(shop) - 1):
min_index = i
for j in range(i + 1, len(shop)):
if shop[j].get_price() < shop[min_index].get_price():
min_index = j
temp = shop[min_index]
shop[min_index] = shop[i]
shop[i] = temp
# Print sorted
for i in range(len(shop)):
print(shop[i].get_name() + ": $" + str(shop[i].get_price()))
Output (program က ထုတ်ပြတဲ့ ရလဒ်):
Mouse: $29.99
Webcam: $49.99
Keyboard: $79.99
Monitor: $299.99
ဥပမာ 2: ကျောင်းသားတွေကို နာမည် အလိုက် စီခြင်း
အသုံးများတဲ့ ပုံစံ တစ်ခုပါ: အရင် စီပြီးမှ စီပြီးသား array ကို ဆက်လုပ်ဆောင်ပါတယ်။
Student[] students = new Student[4];
students[0] = new Student("Diana", 88, "Blue");
students[1] = new Student("Alice", 95, "Red");
students[2] = new Student("Charlie", 72, "Blue");
students[3] = new Student("Bob", 91, "Red");
// Sort by name (alphabetical)
for (int i = 0; i < students.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < students.length; j++) {
if (students[j].getName().compareTo(students[minIndex].getName()) < 0) {
minIndex = j;
}
}
Student temp = students[minIndex];
students[minIndex] = students[i];
students[i] = temp;
}
// Print sorted roster
for (int i = 0; i < students.length; i++) {
System.out.println(students[i].getName() + ": " + students[i].getGrade());
}
students = []
students.append(Student("Diana", 88, "Blue"))
students.append(Student("Alice", 95, "Red"))
students.append(Student("Charlie", 72, "Blue"))
students.append(Student("Bob", 91, "Red"))
# Sort by name (alphabetical) - < compares strings directly in Python
for i in range(len(students) - 1):
min_index = i
for j in range(i + 1, len(students)):
if students[j].get_name() < students[min_index].get_name():
min_index = j
temp = students[min_index]
students[min_index] = students[i]
students[i] = temp
# Print sorted roster
for i in range(len(students)):
print(students[i].get_name() + ": " + str(students[i].get_grade()))
Output:
Alice: 95
Bob: 91
Charlie: 72
Diana: 88
Quick Check (အမြန် စစ်ဆေးမေးခွန်း)
Q1. Student object တွေရဲ့ array ကို grade အလိုက် စီတဲ့အခါ ဘာကို နှိုင်းယှဉ်ရမလဲ။
Q2. "Apple".compareTo("Banana") က ဘာကို return ပြန်ပေးသလဲ။
Q3. Array ထဲမှာ Student object နှစ်ခုကို swap လုပ်တဲ့အခါ တကယ်တမ်း ဘာ ရွှေ့သွားတာလဲ။
Q4. ကျောင်းသားတွေကို grade အမြင့်ဆုံးကနေ အနိမ့်ဆုံးအထိ စီဖို့ selection sort (အငယ်ဆုံးကို ရှာပြီး နေရာချ စီခြင်း) ရဲ့ comparison မှာ ဘာ ပြောင်းဖို့ လိုသလဲ။
Trace Exercise (code ကို တစ်ဆင့်ချင်း လိုက်ကြည့်ခြင်း)
Price အလိုက် ascending စီနေတဲ့ ဒီ product တွေပေါ်မှာ selection sort ရဲ့ pass (ဒေတာကို တစ်ခေါက် ဖြတ်ကြည့်ခြင်း) တစ်ခု (i=0) ကို အပြည့်အစုံ trace (တစ်ဆင့်ချင်း လိုက်ပြီး မှတ်) လုပ်ပါ။ အဖြေတွေကို English လို ရိုက်ထည့်ပါ: yes (ဟုတ်တယ်)၊ no (မဟုတ်ဘူး) နဲ့ product နာမည်။
Trace: Selection Sort ရဲ့ ပထမ Pass (i=0)
// Products: [0] Keyboard $79.99, [1] Mouse $29.99, [2] Monitor $299.99, [3] Webcam $49.99
// i=0, minIndex starts at 0| j | shop[j].getPrice() | shop[minIndex].getPrice() | minIndex ကို update လုပ်မလား | ပြီးနောက် minIndex |
|---|---|---|---|---|
| 1 | 29.99 | 79.99 | ||
| 2 | 299.99 | |||
| 3 | 49.99 |
Pass ပြီးနောက်: index 0 နဲ့ index ကို swap လုပ်ပါတယ်။ ပထမ element က ဖြစ်သွားပါတယ်။
Spot the Error (အမှား ရှာခြင်း)
ဒီ sort က ကျောင်းသားတွေကို နာမည် အလိုက် အက္ခရာစဉ် စီရမှာပါ၊ ဒါပေမယ့် compile မဖြစ်ပါဘူး။
အမှား ရှာပါ: ကျောင်းသားတွေကို နာမည် အလိုက် A-Z စီရမယ့် sort ပါ၊ ဒါပေမယ့် comparison operator မှားကို သုံးထားပါတယ်။
Line 5 အတွက် မှန်ကန်တဲ့ ပြင်နည်းကို ရွေးပါ:
Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)
Products array ကို price အလိုက် ascending စီပြီးနောက် loop (ထပ်ခါထပ်ခါ run တဲ့ code အပိုင်း) က print ထုတ်တဲ့ ပထမ line က ဘာလဲ။
// Before sort: Keyboard $79.99, Mouse $29.99, Monitor $299.99, Webcam $49.99
// After selection sort by price ascending:
System.out.println(shop[0].getName() + ": $" + shop[0].getPrice()); Practice Exercises (လေ့ကျင့်ခန်းများ)
Core (အခြေခံ အဆင့်)
-
Grade အလိုက် descending စီခြင်း: ကျောင်းသားတွေကို grade အမြင့်ဆုံးကနေ အနိမ့်ဆုံးအထိ စီအောင်
sortByGrademethod ကို ပြင်ပါ။ ကျောင်းသား အနည်းဆုံး 4 ယောက်နဲ့ စမ်းသပ်ပြီး ရလဒ်ကို print ထုတ်ပါ။ -
ကုန်ပစ္စည်းတွေကို နာမည် အလိုက် စီခြင်း:
Productclass ကို သုံးပြီး.compareTo()နဲ့ ကုန်ပစ္စည်းတွေကို နာမည် အလိုက် အက္ခရာစဉ် စီပေးတဲ့ selection sort ကို ရေးပါ။
Extension (တိုးချဲ့ အဆင့်)
-
စီပြီးမှ ရှာခြင်း: ကျောင်းသား array ကို နာမည် အလိုက် စီပါ၊ ပြီးရင် ကျောင်းသားကို နာမည်နဲ့ ရှာဖို့ binary search (တစ်ဝက်စီ ခွဲပြီး ရှာနည်း) ကို သုံးတဲ့ method တစ်ခု ရေးပါ (linear search (တစ်ခုချင်း ဆက်တိုက် ရှာနည်း) မဟုတ်ပါ)။ Student object ဒါမှမဟုတ် null (ဘာ object ကိုမှ မညွှန်းတဲ့ တန်ဖိုး) ကို return ပြန်ပေးပါ။
-
Multi-key sort (key အများနဲ့ စီခြင်း): ကျောင်းသားတွေကို house (ကျောင်းအသင်း) အလိုက် (အက္ခရာစဉ်) အရင် စီပြီး house တစ်ခုစီထဲမှာ grade အလိုက် (descending) ထပ်စီပါ။ “Blue” house က ကျောင်းသားတွေက “Red” house ထက် အရင် ပေါ်ရပြီး house တစ်ခုစီထဲမှာ grade အမြင့်ဆုံးက အရင် လာရပါမယ်။
Challenge (စိန်ခေါ်မှု အဆင့်)
- Leaderboard (အမှတ်စာရင်း ဇယား):
name,scoreနဲ့levelပါတဲ့Playerclass တစ်ခု ဖန်တီးပါ။ Score အလိုက် (descending) စီထားတဲ့ ကစားသူ 10 ယောက်ရဲ့ leaderboard ကို ထိန်းထားပါ။ ကစားသူ အသစ်ကို မှန်ကန်တဲ့ နေရာမှာ ထည့်ပေးပြီး (တခြားသူတွေကို အောက်ကို ရွှေ့ပြီး) board ပြည့်နေရင် အမှတ် အနည်းဆုံးသူကို ဖယ်ထုတ်ပေးတဲ့addPlayer(Player p)method ကို ရေးပါ။ Array တစ်ခုလုံးကို ပြန်မစီပါနဲ့: in place (မူလ array ထဲမှာပဲ) ထည့်ပါ။
ဆက်စပ် ခေါင်းစဉ်များ
- အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Sorting Algorithms: int array တွေပေါ်မှာ selection sort
- အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Encapsulation (ဒေတာနဲ့ method ကို တစ်စုတည်း ထုပ်ပိုးခြင်း): field တွေကို တိုက်ရိုက် မယူဘဲ getter တွေကို ဘာကြောင့် သုံးလဲ
- အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: OOP Method Patterns (OOP မှာ အသုံးများတဲ့ method ပုံစံများ): sorting မှာ သုံးတဲ့ find-max ပုံစံ
- ဆက်စပ် ခေါင်းစဉ်: Object References (object ကို ညွှန်းတဲ့ reference များ): တန်ဖိုးတွေကို မဟုတ်ဘဲ reference တွေကို swap လုပ်ခြင်း
- ဆက်စပ် ခေါင်းစဉ်: Strings (String နဲ့ သုံးတဲ့ method များ): string တွေကို အစဉ်စီဖို့
.compareTo()