Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

Operation Counts Method in Algorithm (วิธีนับ Operations): ประมาณค่า Time Complexity

มีหลายวิธีประมาณ Cost ของอัลกอริทึม หนึ่งวิธีคือ Operation Counts เลือก Operation หนึ่ง (Add, Subtract, Compare) นับจำนวนครั้งที่ทำ ความสำเร็จขึ้นอยู่กับการระบุ Operation ที่สร้าง Time Complexity ส่วนใหญ่

ตัวอย่าง: Array ขนาด n หา Index ของค่าสูงสุด นับ Comparison Operations ระหว่างคู่สมาชิก Operations อื่นๆ เช่น i++, Assignment ไม่นับในวิธีนี้

Algorithm

getMax(arr, n):
    index := 0
    max := arr[0]
    for i in range 1 to n-1 do
        if arr[i] > max then
            max := arr[i]
            index := i
        end if
    done
    return index

เลือก Operation ที่ทำมากที่สุดเพื่อประมาณ Cost เช่น Bubble Sort นับ Swap Operation ต้องคิดว่ándo Swap มากที่สุด (Worst Case) เพื่อได้ Upper Bound