1 / 12

Implementing Sets/Maps with a Sorted Array

Implementing Sets/Maps with a Sorted Array. Computer Science 4 Mr. Gerb. Reference: Objective: Understand how to implement Maps and Sets using a sorted array. Implementing Search Using a Sorted Array. Keep the items in the array in sorted order

trory
Download Presentation

Implementing Sets/Maps with a Sorted Array

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. Implementing Sets/Maps with a Sorted Array Computer Science 4 Mr. Gerb Reference: Objective: Understand how to implement Maps and Sets using a sorted array.

  2. Implementing Search Using a Sorted Array • Keep the items in the array in sorted order • This implies that there is some order on the items. • Java implements this with the compareTo() method. • E.g. myObject.compareTo(yourObject); • Returns a negative number if myObject<yourObject • Returns 0 if myObject=yourObject • Returns a positive number if myobject>yourObject • No changes to interface, except: • Iterator always returns items in order • In Java, these are actually different ADTs called SortedMap and SortedSet.

  3. Adding to a Sorted Array • Different from unsorted array. To remain sorted: • If item to add is not found, must find where it fits • Must move all subsequent items up one • E.g. Want to insert 66: • Must move 79 and 99 to the right

  4. Deleting from a Sorted Array • Same as for unsorted lists • Move everything beyond the deleted item down one space • Array is still sorted • E.g. Want to remove 43: • Move 79 and 99 to the left. Array remains sorted.

  5. Retrieving from Sorted Arrays • Can speed up retrieval • Search sequentially until the item is found. • If not found don’t need to search entire array. Stop when you find an element that’s larger than it. • Are sorted arrays an improvement on unsorted arrays? • Adding is still O(N) • Removing is still O(N) • Unsuccessful search is faster (N/2, not N) • But successful search is the same • Both are still O(N) • We haven’t yet improved upon unsorted list. • But if array is sorted, can use binary search

  6. Binary Search in a Sorted Array • Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array. • Repeat the process in the half of the array that should be examined next. • Stop when item is found, or when there is nowhere else to look and item has not been found.

  7. 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] LESS last = midPoint - 1 GREATER first = midPoint + 1 Trace of Binary Search item = 45 first midPoint last 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first midPoint last

  8. 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first, last midPoint GREATER first = midPoint + 1 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] LESS last = midPoint - 1 Trace continued item = 45 first, midPoint, last

  9. 15 26 38 57 62 78 84 91 108 119 info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] first > last found = false Trace concludes item = 45 last first

  10. Efficiency of Binary Search • How many probes does binary search take? • I.e. how many times can you split a list of N items in half before you have only 1? • If N=2, once, N=4, twice, N=2k, k times • I.e. Log(N) times • Binary search allows retrieval to run in O(LogN) time for the worst case

  11. Comparison of Sorted vs. Unsorted Arrays

  12. Summary • Implementing search using a sorted array • Add algorithm changes, remove does not • Both still O(N) • Can use sequential search but it is still O(N) • Can use binary search • Can retrieve in O(LogN) • An improvement over unsorted arrays

More Related