/**********************************************************
*DESCRIPTION *
* Quick sort an array a. *
*PARAMETERS *
* a is the array will be sorted; low is left index; hight is right *
* index. *
*RETURN VALUE *
* Success, true is returned; Otherwise, false is returned. *
**********************************************************/
bool quick_sort(int a[], int low, int high)
{
int middle;
if (NULL == a || low < 0 || low >= high)
{
return false;
}
middle = split(a, low, high);
if (!quick_sort(a, low, middle - 1))
{
return false;
}
if (!quick_sort(a, middle + 1, high))
{
return false;
}
return true;
}
static int split(int a[],int low,int high)
{
int key_value = a[low];
for (;;)
{
/* Find a number bigger than key_value from high index to low index. */
while(low < high && key_value <= a[high])
{
high--;
}
if (low >= high)
{
break;
}
a[low++] = a[high];
/* Find a number smaller than key_value from low index to high index. */
while(low < high && a[low] <= key_value)
{
low++;
}
if (low >= high)
{
break;
}
a[high--] = a[low];
}
a[high] = key_value;
return high;
}
*DESCRIPTION *
* Quick sort an array a. *
*PARAMETERS *
* a is the array will be sorted; low is left index; hight is right *
* index. *
*RETURN VALUE *
* Success, true is returned; Otherwise, false is returned. *
**********************************************************/
bool quick_sort(int a[], int low, int high)
{
int middle;
if (NULL == a || low < 0 || low >= high)
{
return false;
}
middle = split(a, low, high);
if (!quick_sort(a, low, middle - 1))
{
return false;
}
if (!quick_sort(a, middle + 1, high))
{
return false;
}
return true;
}
static int split(int a[],int low,int high)
{
int key_value = a[low];
for (;;)
{
/* Find a number bigger than key_value from high index to low index. */
while(low < high && key_value <= a[high])
{
high--;
}
if (low >= high)
{
break;
}
a[low++] = a[high];
/* Find a number smaller than key_value from low index to high index. */
while(low < high && a[low] <= key_value)
{
low++;
}
if (low >= high)
{
break;
}
a[high--] = a[low];
}
a[high] = key_value;
return high;
}