130 likes | 257 Views
אלגוריתמי חיפוש. Brute Force. Module Module1 Function BruteForce(ByRef x() As Integer, ByRef item As Integer) As Integer Dim i As Integer For i = 0 To x.Length() - 1 If (x(i) = item) Then Return i End If Next
E N D
Brute Force Module Module1 Function BruteForce(ByRef x() As Integer, ByRef item As Integer) As Integer Dim i As Integer For i = 0 To x.Length() - 1 If (x(i) = item) Then Return i End If Next Return -1 'Not found! End Function Sub Main() Dim numbers() As Integer = {1, 2, 3, 4, 10, 10, 71, 101} Dim key As Integer key = Console.ReadLine() Console.WriteLine(BruteForce(numbers, key)) End Sub End Module
BubbleSearch Function BubbleSearch(ByRef x() As Integer, ByRef item As Integer) As Integer Dim i As Integer For i = 0 To x.Length() - 1 If (x(i) = item) Then If i > 0 Then Swap(x(i), x(i - 1)) End If Return i End If Next Return -1 'Not found! End Function Sub Main() Dim numbers() As Integer = {1, 2, 3, 4, 10, 10, 71, 101} Dim key As Integer key = Console.ReadLine() Console.WriteLine(BubbleSearch(numbers, key)) Console.WriteLine(BubbleSearch(numbers, key)) End Sub
? 12 חיפוש חיפושבינרי 2 5 8 11 23 24 28 34 38 41 44 45 52 60 70 מבוא למדעי המחשב - מאיר קומר - סמסטר א'- תשס"ט - שיעור מספר 7
חיפושבינארי אבל איך יודעים אם מצאתי? יופי, אבל איפה? חיפוש Function BinarySearch(ByRef x() As Integer, ByRef item As Integer) As Integer Dim Mid As Integer Dim low As Integer = 0 Dim high As Integer = x.Length() - 1 While (low <= high) Console.WriteLine("high is " & high) Console.WriteLine("low is " & low) Mid = (low + high) \ 2 If (item = x(Mid)) Then Console.WriteLine("I found it!!") Return Mid 'Found here! ElseIf item > x(Mid) Then low = Mid + 1 Else high = Mid - 1 End If End While Return -1 'Not found! End Function מבוא למדעי המחשב - מאיר קומר - סמסטר א'- תשס"ט - שיעור מספר 7
Main דוגמא של Sub Main() Dim numbers() As Integer = {1, 2, 3, 4, 10, 10, 71, 101} Dim key As Integer key = Console.ReadLine() Console.WriteLine(BinarySearch(numbers, key)) End Sub
חיפושבינארי ריכורסיבית Sub RBinarySearch(ByRef x() As Integer, ByRef item As Integer, ByVal low As Integer, ByVal high As Integer) Dim Mid As Integer If (low > high) Then Console.WriteLine("Item not found") Else Mid = (low + high) / 2 If item > x(Mid) Then RBinarySearch(x, item, Mid + 1, high) ElseIf item < x(Mid) Then RBinarySearch(x, item, low, Mid - 1) Else Console.WriteLine("I found it in position " & Mid) End If End If End Sub
Main דוגמא של Sub Main() Dim numbers() As Integer = {1, 2, 3, 4, 10, 10, 71, 101} Dim key As Integer key = Console.ReadLine() RBinarySearch(numbers, key, 0, numbers.Length() - 1) End Sub
Right to Left (like in Hebrew) • Matching the pattern from right to left • For a pattern abc: ↓ T:bbacdcbaabcddcdaddaaabcbcb P:abc • Worst case is still O(n m)
Boyer-Moore Example T: P:
חיפוש פשוט לטקסטים Module Module1 Sub Main() Dim t As String = "my name is avi, not aviya, or aviva, avihu, aviv or david!" Console.WriteLine(t.IndexOf("avi")) Console.WriteLine(t.LastIndexOf("avi")) End Sub End Module
Text Matching in VB Imports System.Text.RegularExpressions Module Module1 Sub Main() Dim myMatches As MatchCollection Dim t As String = "my name is avi, not aviya, or aviva, avihu, aviv or david!" Dim myRegex As New Regex("avi") myMatches = myRegex.Matches(t) Dim successfulMatch As Match For Each successfulMatch In myMatches Console.WriteLine(successfulMatch.Index) Next End Sub End Module