void ShellSort(register int v[], int n)
{
register int gap, i, j, temp;
gap = 1;
do (gap = 3*gap + 1); while(gap <=n);
for (gap /= 3; gap > 0; gap /=3)
for (i = gap; i < n; i++) {
temp = v[i];
for (j = i - gap; (j>=0)&&(v[j]>temp); j-=gap)
v[j+gap] = v[j];
v[j+gap] = temp;
}
}
void Swap(int *l, int *r)
{
if (l == r) return ;
*l = *l ^ *r;
*r = *l ^ *r;
*l = *r ^ *l;
}
int Partition(int v[], int low, int high)
{
int i;
int pivotpos = low;
int pivot = v[low];
for (i = low + 1; i <= high; i++)
if (v[i] < pivot && ++pivotpos != i)
Swap(&v[pivotpos], &v[i]);
Swap(&v[low], &v[pivotpos]);
return pivotpos;
}
void QuickSort(int v[], int left, int right)
{
if (left < right)
{
int pivotpos = Partition(v, left, right);
QuickSort(v, left, pivotpos-1);
QuickSort(v, pivotpos+1, right);
}
}
{
register int gap, i, j, temp;
gap = 1;
do (gap = 3*gap + 1); while(gap <=n);
for (gap /= 3; gap > 0; gap /=3)
for (i = gap; i < n; i++) {
temp = v[i];
for (j = i - gap; (j>=0)&&(v[j]>temp); j-=gap)
v[j+gap] = v[j];
v[j+gap] = temp;
}
}
void Swap(int *l, int *r)
{
if (l == r) return ;
*l = *l ^ *r;
*r = *l ^ *r;
*l = *r ^ *l;
}
int Partition(int v[], int low, int high)
{
int i;
int pivotpos = low;
int pivot = v[low];
for (i = low + 1; i <= high; i++)
if (v[i] < pivot && ++pivotpos != i)
Swap(&v[pivotpos], &v[i]);
Swap(&v[low], &v[pivotpos]);
return pivotpos;
}
void QuickSort(int v[], int left, int right)
{
if (left < right)
{
int pivotpos = Partition(v, left, right);
QuickSort(v, left, pivotpos-1);
QuickSort(v, pivotpos+1, right);
}
}