Почему небезопасный код ниже по сравнению с безопасным доступом к массиву не намного быстрее? Что тормозит и идеи как это лучше написать? Я попытаюсь отредактировать Heapsort как небезопасный.
public unsafe static void HeapSortU(double[] array, int low, int high)
{
int length = high - low + 1;
fixed (double* arrPtr = array, start = &array[low], stred = &array[low + length / 2 - 1], root = &array[high])
{
for (double* i = stred; i >= start; i--)
HeapifyU(start, root, i);
for (double* i = root; i >= start; i--)
{
double temp = *start;
*start = *i;
*i = temp;
HeapifyU(start, i, start);
}
}
}
private unsafe static void HeapifyU(double* start, double* root, double* bottom)
{
double* l, largest;
while (true)
{
largest = bottom; // initialize largest as root
l = bottom + (bottom - start) + 1; // left = 2*bottom + 1
if (l < root && *l > *largest)
largest = l;
l++; // right = 2*bottom + 2
if (l < root && *l > *largest)
largest = l;
if (largest != bottom)
{
double temp = *bottom;
*bottom = *largest;
*largest = temp;
bottom = largest;
}
else
break;
}
}
(Я не знаю, что еще написать, но автоматическая проверка все равно сообщает: «Похоже, ваш пост — это в основном код; пожалуйста, добавьте еще немного деталей».)