using System;
unsafe class Program
{
//struct Header
//{
// internal int* data;
//};
//Header* object_header = stackalloc Header[sizeof(Header)];
//object_header->data = stackalloc int[length];
static void SwapMe(int* first, int* second)
{
int tmp = *first;
*first = *second;
*second = tmp;
}
static void SortQuick(int* data, int low, int high)
{
int i = low,
j = high,
x = data[(low + high) / 2];
do
{
while (data[i] < x) i++;
while (data[j] > x) j--;
if (i <= j)
{
SwapMe(&data[i], &data[j]);
i++;
j--;
}
} while (i <= j);
if (low < j) SortQuick(data, low, j);
if (j < high) SortQuick(data, j, high);
}
static void ArrayPrint(int* data, int length)
{
for (int i = 0; i < length; i++)
{
Console.WriteLine(data[i]);
}
}
static void Main()
{
int length = new Random().Next(1000, 99999);
int* data = stackalloc int[length];
int capicity = length * sizeof(int);
for (int i = 0; i < capicity; i++)
{
data[i] = new Random().Next(0, 9999);
}
ArrayPrint(data, capicity);
SortQuick(data, 0, length - 1);
ArrayPrint(data, capicity);
Console.ReadLine();
}
}
dXNpbmcgU3lzdGVtOwoKdW5zYWZlIGNsYXNzIFByb2dyYW0KewogICAgLy9zdHJ1Y3QgSGVhZGVyCiAgICAvL3sKICAgIC8vICAgIGludGVybmFsIGludCogZGF0YTsKICAgIC8vfTsKCiAgICAvL0hlYWRlciogb2JqZWN0X2hlYWRlciA9IHN0YWNrYWxsb2MgSGVhZGVyW3NpemVvZihIZWFkZXIpXTsKICAgIC8vb2JqZWN0X2hlYWRlci0+ZGF0YSA9IHN0YWNrYWxsb2MgaW50W2xlbmd0aF07CgogICAgc3RhdGljIHZvaWQgU3dhcE1lKGludCogZmlyc3QsIGludCogc2Vjb25kKQogICAgewogICAgICAgIGludCB0bXAgPSAqZmlyc3Q7CiAgICAgICAgKmZpcnN0ID0gKnNlY29uZDsKICAgICAgICAqc2Vjb25kID0gdG1wOwogICAgfQoKICAgIHN0YXRpYyB2b2lkIFNvcnRRdWljayhpbnQqIGRhdGEsIGludCBsb3csIGludCBoaWdoKQogICAgewogICAgICAgIGludCBpID0gbG93LAogICAgICAgICAgICBqID0gaGlnaCwKICAgICAgICAgICAgeCA9IGRhdGFbKGxvdyArIGhpZ2gpIC8gMl07CgogICAgICAgIGRvCiAgICAgICAgewogICAgICAgICAgICB3aGlsZSAoZGF0YVtpXSA8IHgpIGkrKzsKICAgICAgICAgICAgd2hpbGUgKGRhdGFbal0gPiB4KSBqLS07CgogICAgICAgICAgICBpZiAoaSA8PSBqKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBTd2FwTWUoJmRhdGFbaV0sICZkYXRhW2pdKTsKICAgICAgICAgICAgICAgIGkrKzsKICAgICAgICAgICAgICAgIGotLTsKICAgICAgICAgICAgfQoKICAgICAgICB9IHdoaWxlIChpIDw9IGopOwoKICAgICAgICBpZiAobG93IDwgaikgU29ydFF1aWNrKGRhdGEsIGxvdywgaik7CiAgICAgICAgaWYgKGogPCBoaWdoKSBTb3J0UXVpY2soZGF0YSwgaiwgaGlnaCk7CiAgICB9CgogICAgc3RhdGljIHZvaWQgQXJyYXlQcmludChpbnQqIGRhdGEsIGludCBsZW5ndGgpCiAgICB7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBsZW5ndGg7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKGRhdGFbaV0pOwogICAgICAgIH0KICAgIH0KCiAgICBzdGF0aWMgdm9pZCBNYWluKCkKICAgIHsKICAgICAgICBpbnQgbGVuZ3RoID0gbmV3IFJhbmRvbSgpLk5leHQoMTAwMCwgOTk5OTkpOwogICAgICAgIGludCogZGF0YSA9IHN0YWNrYWxsb2MgaW50W2xlbmd0aF07CiAgICAgICAgaW50IGNhcGljaXR5ID0gbGVuZ3RoICogc2l6ZW9mKGludCk7CgogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgY2FwaWNpdHk7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGRhdGFbaV0gPSBuZXcgUmFuZG9tKCkuTmV4dCgwLCA5OTk5KTsKICAgICAgICB9CgogICAgICAgIEFycmF5UHJpbnQoZGF0YSwgY2FwaWNpdHkpOwogICAgICAgIFNvcnRRdWljayhkYXRhLCAwLCBsZW5ndGggLSAxKTsKICAgICAgICBBcnJheVByaW50KGRhdGEsIGNhcGljaXR5KTsKCiAgICAgICAgQ29uc29sZS5SZWFkTGluZSgpOwogICAgfQp9