QuickSort
public static void QuickSort(int[] array, int left, int right)
{
do
{
int a = left;
int b = right;
int num3 = a + ((b - a) >> 1);//a+(b-a)/2
SwapIfCreater(array, a, num3);
SwapIfCreater(array, a, b);
SwapIfCreater(array, num3, b);
do
{
while (array[a] < array[num3])
{
a++;
}
while (array[num3] < array[b])
{
b--;
}
if (a > b)
{
break;
}
if (a < b)
{
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
a++;
b--;
}
while (a <= b);
if ((b - left) <= (right - a))
{
if (left < b)
{
QuickSort(array, left, b);
}
left = a;
}
else
{
if (a < right)
{
QuickSort(array, a, right);
}
right = b;
}
} while (left < right);
}
{
do
{
int a = left;
int b = right;
int num3 = a + ((b - a) >> 1);//a+(b-a)/2
SwapIfCreater(array, a, num3);
SwapIfCreater(array, a, b);
SwapIfCreater(array, num3, b);
do
{
while (array[a] < array[num3])
{
a++;
}
while (array[num3] < array[b])
{
b--;
}
if (a > b)
{
break;
}
if (a < b)
{
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
a++;
b--;
}
while (a <= b);
if ((b - left) <= (right - a))
{
if (left < b)
{
QuickSort(array, left, b);
}
left = a;
}
else
{
if (a < right)
{
QuickSort(array, a, right);
}
right = b;
}
} while (left < right);
}
代码
private static void SwapIfCreater(int[] array, int a, int b)
{
if (a != b && array[a] > array[b])
{
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}
{
if (a != b && array[a] > array[b])
{
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}