在一个无序的序列中,要查找第i小的元素最简单的方法就是将所有元素排序,就可以直接找到地i小的元素,然而比较排序算法最快也只能是O(nlogn),比如堆排序、归并排序、快速排序。这里研究了只要时间复杂度为O(n)的算法。
利用快速排序算法中的枢轴元素,即枢轴元素的左边全是小于等于它的元素,枢轴元素的右边全是大于等于它的元素,则枢轴元素的位置k就是它在序列中第k小的元素,然后用k和要查找的i比较判断即可。这里用到了分治算法,每次递归调用都可以排除掉部分元素:枢轴元素的全部左部分元素或枢轴元素的全部右部分元素。
主要思想:
1、假设n=1时,就是只有一个数,
直接返回这个位置的值就是所求的第i个值
<span style="font-size:18px;">int RandomSelect(int *pnArr, int nLeft, int nRight, int i)
{
if (nLeft == nRight)
{
return pnArr[nLeft];
}
//寻找一个nTmpPos下标,nTmpPos左边的值都小于它,右边的值都大于它
int nTmpPos = RandomPartiton(pnArr, nLeft, nRight);
int nLCount = nTmpPos - nLeft + 1;
if (nLCount == i)
{
return pnArr[nTmpPos];
}
else if (i < nLCount)
{
return RandomSelect(pnArr, nLeft, nTmpPos - 1, i);
}
else
{
return RandomSelect(pnArr, nTmpPos + 1, nRight, i - nLCount);
}
}</span>
2、当数组的个数大于2时,运用快速排序的思想,寻找一个主元的地址,如果主元在的位置正好是所求的第i个数,则返回值
<span style="font-size:18px;">//寻找一个nTmpPos下标,nTmpPos左边的值都小于它,右边的值都大于它
int nTmpPos = RandomPartiton(pnArr, nLeft, nRight);</span>
</pre><pre name="code" class="html">
</pre><p><span style="font-size:18px;">如果i小于所求的主元地址,则在数组的前半部分求第i个数,主要运用递归的思想, 第i个数也是在前半部分数组的第i个位置,所以递归时所查找的位置还是i</span></p><pre name="code" class="html" style="line-height: 24px;"><span style="font-size:18px;"> else if (i < nLCount)
{
return RandomSelect(pnArr, nLeft, nTmpPos - 1, i);
}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">如果第i个元素的地址大于主元地址</span>
<pre name="code" class="html"><span style="font-size:18px;"> {
return RandomSelect(pnArr, nTmpPos + 1, nRight, i - nLCount);
}</span>
<span style="font-size:18px;">所查找的地址在数组的后半段,所在的地址是<span style="font-family: Arial, Helvetica, sans-serif;">i - nLCount的位置</span></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;">
</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;font-size:18px;">代码:</span>
<span style="font-family:Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
void PrintArr(int *pnArr, int nLen)
{
for (int i = 0; i < nLen; i++)
{
printf("%d ", pnArr[i]);
}
printf("\n");
}
void Swap(int *p1, int *p2)
{
int nTmp = *p1;
*p1 = *p2;
*p2 = nTmp;
}
int Partition(int *pnArr, int nLeft, int nRight)
{
int nKey = nRight;
int i = nLeft - 1;
for (int j = nLeft; j < nRight; j++)
{
if (pnArr[j] <= pnArr[nKey])
{
i++;
Swap(&pnArr[i], &pnArr[j]);
}
}
Swap(&pnArr[i+1], &pnArr[nKey]);
return i+1;
}
int RandomPartiton(int *pnArr, int nLeft, int nRight)
{
srand(time(NULL));
int i = rand()%(nRight - nLeft + 1) + nLeft;
Swap(&pnArr[i], &pnArr[nRight]);
return Partition(pnArr, nLeft, nRight);
}
//i 第i小元素
int RandomSelect(int *pnArr, int nLeft, int nRight, int i)
{
if (nLeft == nRight)
{
return pnArr[nLeft];
}
//寻找一个nTmpPos下标,nTmpPos左边的值都小于它,右边的值都大于它
int nTmpPos = RandomPartiton(pnArr, nLeft, nRight);
int nLCount = nTmpPos - nLeft + 1;
if (nLCount == i)
{
return pnArr[nTmpPos];
}
else if (i < nLCount)
{
return RandomSelect(pnArr, nLeft, nTmpPos - 1, i);
}
else
{
return RandomSelect(pnArr, nTmpPos + 1, nRight, i - nLCount);
}
}
int main()
{
int nArr[10] = {0,2,1,3,5,6,9,7,4,12};
PrintArr(nArr, 10);
printf("第5最小元素的值为%d\n", RandomSelect(nArr, 0, 9, 5));
system("pause");
return 0;
}</span>