Algorithm design. We have inherited a data structure for storing non-negative in
ID: 668018 • Letter: A
Question
Algorithm design. We have inherited a data structure for storing non-negative integers in an array. The array has been pre-allocated and is of size M. However, we don’t always actually use all M slots – the number of elements stored is some N < M. The original author of the code decided to not explicitly keep track of N, but instead used a “sentinel” approach: all values being stored are in indices [0..N-1] and after that the array is filled with negative 1 (-1 indicating “nothing here”). (Note that N is strictly less than M). This way if we are searching for something, we know when to stop.Your job: devise an algorithm which takes such an array and determines N in O(log N) time.
Write your algorithm as a C function. Assume that M is a given constant.
Argue the correctness of your algorithm and why it achieves the specified runtime.
NOTE: Partial credit for an O(log M) time algorithm, but in general, a solution that is
O(log M) is not necessarily O(log N)
Explanation / Answer
Quicksort:
Quicksort sometimes called partition-exchange sort is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Developed by Tony Hoare in 1959,with his work published in 1961, it is still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort.
Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting.
Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.
Quicksort is a divide and conquer algorithm. Quicksort first divides a large array into two smaller sub-arrays.the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
The steps are:
Parallelization
Quicksort's divide-and-conquer formulation makes it amenable to parallelization using task parallelism. The partitioning step is accomplished through the use of a parallel prefix sum algorithm to compute an index for each array element in its section of the partitioned array. Given an array of size n, the partitioning step performs O(n) work in O(log n) time and requires O(n) additional scratch space.
After the array has been partitioned, the two partitions can be sorted recursively in parallel. Assuming an ideal choice of pivots, parallel quicksort sorts an array of size n in O(n log n) work in O(log² n) time using O(n) additional space.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.