Сортировка вставками - C# (181730)
Формулировка задачи:
Имеется код сортировки вставками:
Есть ли возможность как-то ускорить процесс сортировки? Ибо когда массив около 100.000 и больше, сортировка происходит очень медленно.
int[] result = new int[arraySort.Length];
for (int i = 0; i < arraySort.Length; i++)
{
int j = i;
while (j > 0 && result[j - 1] > arraySort[i])
{
result[j] = result[j - 1];
j--;
}
result[j] = arraySort[i];
}
return arraySort;Решение задачи: «Сортировка вставками»
textual
Листинг программы
private static unsafe int[] HisSort2(int[] arraySort)
{
int[] result = new int[arraySort.Length];
fixed (int* root = &result[0])
{
for (int i = 0; i < arraySort.Length; i++)
{
int j = i;
var current = root + j;
var e = arraySort[i];
while (j > 0 && *(current - 1) > e)
{
*current-- = *current;
j--;
}
result[j] = arraySort[i];
}
}
return result;
}