hw5_sol - Homework 5 Solution Guide Problem 1 This problem...

This preview shows pages 1–3. Sign up to view the full content.

Homework 5 Solution Guide Problem 1 This problem is to find the kth largest element using the minimum space complexity possible. The elements in the list are presented one at a time in some cell C. To find the kth largest element, the largest k elements in the list need to be stored in order to be able to retrieve that element. Because the list is presented in arbitrary order, any of the current k items given could be the kth largest relative to the full input, and so k items must be stored. The basic idea of this algorithm is to use k extra memory cells for the k largest elements in the list. The input cell C will also be used as the k+1 th cell. If there are empty spots in the buffer, move the input into the empty spot. If the buffer is full, sort the buffer. If the new input is larger than the smallest element in the buffer, override the smallest element with the new input. Once all the input has been processed, sort the buffer. The first element in the buffer (in ascending sort order) will be the kth smallest element. It is important to use an array and allocate a contiguous portion of memory for the buffer. Otherwise, additional space will be needed to determine the order of the list (next node pointers). Complexity (see next page for pseudocode) K+1 memory cells are required, as is discussed above. There must be one additional piece of memory used beyond this. A counter must be used to traverse the buffer. Of course, it is possible to use a pointer to do this as well. Because the counter will never be greater than k, ceil(log 2 k) bits can be used to represent the counter. If this is less than the constant amount of memory required for a pointer, then a counter would be more efficient. One potential for concern is determining whether there are empty spots in the buffer. For simplicity, it can be assumed that the memory can be checked to see if it has been filled or not. If this is not available, it is possible to put a counter in the last element of the buffer size, and overwrite it with the element from the list when the space is needed. This will prevent additional space requirements for this checking, but would require some careful use of flags to make sure that the counter is not an element. It could be attempted to reduce the memory requirements further by using a list of size k, with k-1 extra cells and the input cell C used as the kth cell. However, consider the following case. The list is presented in descending sorted order. After the first k elements have been loaded, the rest are not relevant. However, this is not known. If the cell C is used as the kth cell, the kth largest value is overwritten on each input step, in this case by a smaller value. This destroys the kth largest element and because of this k+1 cells must be used, in order to determine if new input is useable or not without destroying cells that could still be the kth largest element.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Pseudocode Cell buffer[K]; Cell C; int i; while(more input remains)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

hw5_sol - Homework 5 Solution Guide Problem 1 This problem...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online