אלגוריתמי חיפוש
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 Sub Swap(ByRef x As Integer, ByRef y As Integer) Dim temp As Integer = x x = y y = temp End Sub 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)) End Sub
מבוא למדעי המחשב - מאיר קומר - סמסטר א '- תשס " ט - שיעור מספר ? 12 חיפוש בינרי
מבוא למדעי המחשב - מאיר קומר - סמסטר א '- תשס " ט - שיעור מספר 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 אבל איך יודעים אם מצאתי? יופי, אבל איפה?
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 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
עץ בינרי מלא שורש עלים
עץ בינרי חלקי
חיפוש בעץ בינרי האם הערך X נמצא בעץ? נתחיל מהשורש של העץ בכל קדקוד נחליט אם לפנות ימינה או שמאלה אם X שווה לקדקוד הנוכחי מצאנו! אחרת אם הקדקוד הנוכחי אינו עלה אם X גדול מערך הקדקוד נפנה ימינה אם X קטן מערך הקדקוד נפנה שמאלה אם הגענו לעלה שאינו שווה לX X לא קיים בעץ
Boyer and Moore Algorithm
התאמת התבנית ימין לשמאל ההתאמה מתבצעת מסוף המילה לכיוון ההתחלה –(–( כמו עברית ) עבור תבנית : abc: ↓ T: bbacdcbaabcddcdaddaaabcbcb P: abc במקרה הגרוע עדיין דורש m*n השוואות
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
חיפוש מחוכם לטקסטים ב 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