Question: 3 Sorted Array with Sidekick Storing items in a sorted array has the advantage that binary search can find an item in ( l o
Sorted Array with Sidekick
Storing items in a sorted array has the advantage that binary search can find an item in time. However, insertion into a sorted array is cumbersome and takes time.
A Sorted Array with Sidekick SAwS combines a sorted array with a smaller unsorted array, the sidekick. New items inserted in a SAwS are added to the sidekick array, which takes actual time since the sidekick is not sorted. When the number of items in the sidekick grows beyond the sidekick is sorted and merged into the sorted array. After this merger, the new sidekick is empty. To search in a SAwS simply do a binary search in the main array and a linear search in the sidekick.
Throughout this problem, let be the number of items currently stored in the SAwS. This means changes when items are added to or removed from the SAwS. You should analyze the running times in terms of
Just assume that the arrays have enough space allocated. It is possible to combine this data structure with dynamic tables, but let's keep it simple.
Again, for simplicity, just assume that in our use case we will never insert duplicate keys into this data structure. It is possible to handle duplicates, but it gets rather complicated when we delete a key that has duplicates still in the SAwS.
You know nothing about the type, distribution or range of the keys. So you cannot use counting sort, bucket sort, radix sort or any lineartime sorting algorithm.
You may assume without further comment that in the worst case sorting items takes time, binary search in a sorted array of items takes time, finding the median of an unsorted array of items takes time and merging two sorted arrays of and items takes time.
Do not write pseudocode. If you want to sort an array just say "Sort the array
Questions:
Briefly describe how the sidekick array can be merged into the sorted array in actual time.
Using the accounting method, show that the insert and search operations in a SAwS can be accomplished in amortized time. Ignore the delete operation for now.
Note: State an invariant for your accounting scheme and argue that the invariant is maintained after each insert and search operation, including operations that merge the sidekick into the sorted array.
Describe how to implement the delete operation in a SAwS so that search, insert and delete each takes amortized time.
The delete operation is given a key and must find then remove from the SAwS data structure. For sorted arrays, it is convenient to simply mark the location of as "removed". Assume the programming language supports this. For unsorted arrays, you can either mark as removed or move the last item to the vacated location. However, is still the number of items in the SAwS and does not include the locations marked "removed".
Note that a delete makes either the main array or the sidekick array smaller by and that repeated deletions reduce the values of and significantly. In particular, the size of the sidekick array might exceed not because we added items to the sidekick, but because the value of is reduced.
Note: You should revise the invariant for your accounting scheme and argue that the invariant is maintained after each insert, delete and search operation, including operations that merge the sidekick into the sorted array.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
