1 / 13

אלגוריתמי חיפוש

אלגוריתמי חיפוש. 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

tola
Download Presentation

אלגוריתמי חיפוש

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. אלגוריתמיחיפוש

  2. 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

  3. 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

  4. ? 12 חיפוש חיפושבינרי 2 5 8 11 23 24 28 34 38 41 44 45 52 60 70 מבוא למדעי המחשב - מאיר קומר - סמסטר א'- תשס"ט - שיעור מספר 7

  5. חיפושבינארי אבל איך יודעים אם מצאתי? יופי, אבל איפה? חיפוש 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

  6. 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

  7. חיפושבינארי ריכורסיבית 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

  8. 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

  9. Boyer and Moore Algorithm

  10. 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)

  11. Boyer-Moore Example T: P:

  12. חיפוש פשוט לטקסטים 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

  13. 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

More Related