120 likes | 395 Views
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
E N D
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 • 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.
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
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.
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
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.
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
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
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
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
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