问题描述:查找某组数中第几个大的数
解题思路:1)采用先排序后查找的方式
2)利用快速排序,在排序过程中,进行筛选
代码:
public static int FindNumber(int [] arr,int left,int right,int num)
{
if (null == arr || arr.Length == 0 || left >= arr.Length || right >= arr.Length)
{
return -1;
}
if (left < right)
{
int low = left, high = right, key = arr[left];
while (low < high)
{
while (low < high && arr[high] >= key)
{
high--;
}
arr[low] = arr[high];
while (low < high && arr[low] <= key)
{
low++;
}
arr[high] = arr[low];
}
arr[low] = key;
if (low == (num - 1))
{
return arr[low];
}
if (low < (num - 1))
{
return FindNumber(arr, low + 1, right, num);
}
else
{
return FindNumber(arr, left, low, num);
}
}
return -1;
}