I have two algorithms.
1.
void sort3(int *mass, int size)
{
int i = 0;
int j = size - 1;
int temp;
int mid = mass[size / 2];
do
{
while (mass[i] < mid)
{
i++;
}
while (mass[j] > mid)
{
j--;
}
if (i <= j)
{
temp = mass[i];
mass[i] = mass[j];
mass[j] = temp;
i++;
j--;
}
} while (i <= j);
if (j > 0)
{
sort3(mass, j + 1);
}
if (i < size)
{
sort3(&mass[i], size - i);
}
}
2.
void sort4_prepare(int *mass, int size)
{
int middle, start_left, start_right, last;
int *work_mas = new int[size];
middle = size / 2;
start_left = 0;
start_right = middle;
last = size;
for (int j = 0; j < last; j++)
{
if ((start_left < middle) && ((start_right >= last) || (mass[start_left] < mass[start_right])))
{
work_mas[j] = mass[start_left];
start_left++;
}
else
{
work_mas[j] = mass[start_right];
start_right++;
}
}
for (int j = 0; j < last; j++)
mass[j] = work_mas[j];
delete[] work_mas;
};
void sort4(int *mass, int size)
{
if (size > 1)
{
sort4(mass, size / 2);
sort4(&mass[size / 2], size - size / 2);
sort4_prepare(mass, size);
}
}
I also have an array of 1000 random numbers sorted from maximum to minimum. Which of the algorithms will sort the array from minimum to maximim faster? I have done some tests and I think that it is the first one, but I don't know how to prove it. However, in some cases the second one would be faster than the first, and that makes me a little bit unsure in the tests.