在不import任何东西的情况下如何在有限制的情况下做HeapSort和MergeSort
比如说我的开头是
public static void heapSort(int[] a, int d)
a做为一个随机的一组array只会有int在里面d作为Locality-aware value意味着这这个array a里面每个element不能移动超过d位
如图所示。
而且会发生d的值比arr的长度长的情况。
同理MergeSort又要怎么实现呢?
public static void mergeSort(int[] a, int d)
以下为具体要求(可自行翻译):
Guidelines for in-place Heapsort:
● If you do this in place, the overall algorithm becomes a little trickier. You can use
the sliding window idea, but this time everything in the window should be sorted
before you move on to the next window.
● You need to consider how big the window should be.
● You also need to consider how far to “slide” the window after you’re done sorting
it.
● Since this is done in place, you will not need both sink and swim because you
can do your heap construction bottom up using sink, which is also used in
sortDown. To get all the extra credit, you should do it this way, which is more
efficient than doing top-down heap construction.
● The runtime of this implementation should not be worse than O(N/d log d)
Locality-Aware Mergesort. In regular Mergesort, we divide the array into smaller
arrays (until we only have 1-element arrays, which are sorted), then merge the smaller
arrays together. This is a recursive algorithm. If the array is locality-aware, you don’t
have to sort the entire array all at once. Instead, you can use the “sliding window”
method where you sort part of the array, then slide the window and sort the next set of
elements. The questions you need to consider are: How big should the sliding window
be? and How much do I slide the window by after sorting?
Guidelines for Mergesort:
● The runtime for this implementation should not be worse than O(Nlogd).
● To get full credit here, you should only use one temporary array for the whole
method. Do not create new arrays each time you call Mergesort.