#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100000;
void sort(int* a, int lo, int hi)
{
int i = lo;
if (lo >= hi)
return;
for (int j = hi, mode = 1; i < j; mode > 0 ? j-- : i++)
if (a[i] > a[j])
{
swap(a[i], a[j]);
mode = -mode;
}
sort(a, lo, i - 1);
sort(a, i + 1, hi);
}
bool check(int* a)
{
for (int i = 1; i < N; i++)
if (a[i] < a[i - 1])
return false;
return true;
}
int main()
{
int a[N];
for (int i = 0; i < N; i++)
a[i] = (i * 17 + 107) % 10;
sort(a, 0, N - 1);
cout << (check(a) ? "correct" : "incorrect") << endl;
return 0;
}
I found this sort algorithm but after long time of trying to understand it I couldn't. It looks very simple and short. I think that it can be proved through invariant that any element of a[lo:i] is less than any element of a[j:hi] but I can't prove that statement holds true after every iteration of loop (after j-- or i++).