時間複雜度 (Time Complexity)

畫面

公式

原始程式

Module ModuleW4000 ' 類別函數 Class TimeComplex ' 類別私有變數 Private Arr As Integer() = Nothing ' 類別建構子 Sub New(Arr As Integer()) Me.Arr = Arr End Sub ' 類別公共函數 Public Function Search(Num As Integer) As Integer ' 函數私有變數 Dim SearchTimes As Integer = 0 Dim Found As Boolean = False For i = 0 To UBound(Me.Arr) SearchTimes = SearchTimes + 1 If Me.Arr(i) = Num Then Found = True Exit For End If Next Return IIf(Found, SearchTimes, 0) End Function ' 類別公共函數 Public Function Search(Num() As Integer) As Integer ' 函數私有變數 Dim SearchTimes As Integer = 0 Dim Found As Boolean = False For i = 0 To UBound(Me.Arr) SearchTimes = SearchTimes + 1 If Me.Arr(i) > Num(0) And Me.Arr(i) < Num(1) Then Found = True Exit For End If Next Return IIf(Found, SearchTimes, 0) End Function End Class ' 主程式-自有函數 Function Theta(SerahcTimes() As Integer) As Integer Dim TotalSerahcTimes As Integer = 0 Dim SearchItems As Integer = 0 For i = 0 To UBound(SerahcTimes) If SerahcTimes(i) > 0 Then SearchItems = SearchItems + 1 TotalSerahcTimes = TotalSerahcTimes + SerahcTimes(i) End If Next Return Math.Floor(TotalSerahcTimes / SearchItems) End Function ' 主程式-自有函數 Function Omega(SerahcTimes() As Integer) As Integer Dim MinSerahcTimes As Integer = 14 For i = 0 To UBound(SerahcTimes) If SerahcTimes(i) > 0 Then If MinSerahcTimes > SerahcTimes(i) Then MinSerahcTimes = SerahcTimes(i) End If End If Next Return MinSerahcTimes End Function ' 主程式-自有函數 Function BigO(SerahcTimes() As Integer) As Integer Dim MaxSerahcTimes As Integer = 0 For i = 0 To UBound(SerahcTimes) If SerahcTimes(i) > 0 Then If MaxSerahcTimes < SerahcTimes(i) Then MaxSerahcTimes = SerahcTimes(i) End If End If Next Return MaxSerahcTimes End Function ' 主程式-自有副程式 Sub Experiment_1(Arr As Integer(), myTimeComplex As TimeComplex) Dim Num() As Integer = {5, 8, 13, 11, 2, 10, 7, 1, 6, 9} Dim SerahcTimes() As Integer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} For i = 0 To UBound(Num) SerahcTimes(i) = myTimeComplex.Search(Num(i)) If SerahcTimes(i) > 0 Then Console.WriteLine("搜尋 {0:00} 找到位置:{1:00} 搜尋次數:{2:00}", Num(i), i, SerahcTimes(i)) End If Next Console.WriteLine("Omega(n)={0} Theta(n)={1} Big-O(n)={2}", Omega(SerahcTimes), Theta(SerahcTimes), BigO(SerahcTimes)) End Sub ' 主程式-自有副程式 Sub Experiment_2(Arr As Integer(), myTimeComplex As TimeComplex) Dim NumPair As ArrayList = New ArrayList() Dim SerahcTimes() As Integer = {0, 0, 0, 0, 0, 0} NumPair.Add({5, 8}) NumPair.Add({1, 7}) NumPair.Add({11, 13}) NumPair.Add({9, 11}) NumPair.Add({2, 4}) NumPair.Add({3, 10}) For i = 0 To NumPair.Count - 1 SerahcTimes(i) = myTimeComplex.Search(NumPair(i)) If SerahcTimes(i) > 0 Then Console.WriteLine("搜尋 {0:00}~{1:00} 找到位置:{2:00} 搜尋次數:{3:00}", NumPair(i)(0), NumPair(i)(1), i, SerahcTimes(i)) End If Next Console.WriteLine("Omega(n)={0} Theta(n)={1} Big-O(n)={2}", Omega(SerahcTimes), Theta(SerahcTimes), BigO(SerahcTimes)) End Sub ' 主程式 Sub Main() Dim Arr As Integer() = {7, 5, 9, 10, 13, 2, 4, 3, 6, 1} Dim myTimeComplex As TimeComplex = New TimeComplex(Arr) Experiment_1(Arr, myTimeComplex) Experiment_2(Arr, myTimeComplex) Console.ReadKey() End Sub End Module