日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

随机查找数组中第i个元素(按顺序排列的)

發(fā)布時(shí)間:2025/4/16 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 随机查找数组中第i个元素(按顺序排列的) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在一個(gè)無(wú)序的序列中,要查找第i小的元素最簡(jiǎn)單的方法就是將所有元素排序,就可以直接找到地i小的元素,然而比較排序算法最快也只能是O(nlogn),比如堆排序、歸并排序、快速排序。這里研究了只要時(shí)間復(fù)雜度為O(n)的算法。

?????? 利用快速排序算法中的樞軸元素,即樞軸元素的左邊全是小于等于它的元素,樞軸元素的右邊全是大于等于它的元素,則樞軸元素的位置k就是它在序列中第k小的元素,然后用k和要查找的i比較判斷即可。這里用到了分治算法,每次遞歸調(diào)用都可以排除掉部分元素:樞軸元素的全部左部分元素或樞軸元素的全部右部分元素

主要思想:

1、假設(shè)n=1時(shí),就是只有一個(gè)數(shù),

直接返回這個(gè)位置的值就是所求的第i個(gè)值

<span style="font-size:18px;">int RandomSelect(int *pnArr, int nLeft, int nRight, int i) {if (nLeft == nRight){return pnArr[nLeft];}//尋找一個(gè)nTmpPos下標(biāo),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、當(dāng)數(shù)組的個(gè)數(shù)大于2時(shí),運(yùn)用快速排序的思想,尋找一個(gè)主元的地址,

如果主元在的位置正好是所求的第i個(gè)數(shù),則返回值

<span style="font-size:18px;">//尋找一個(gè)nTmpPos下標(biāo),nTmpPos左邊的值都小于它,右邊的值都大于它int nTmpPos = RandomPartiton(pnArr, nLeft, nRight);</span> </pre><pre name="code" class="html">
</pre><p><span style="font-size:18px;">如果i小于所求的主元地址,則在數(shù)組的前半部分求第i個(gè)數(shù),主要運(yùn)用遞歸的思想, 第i個(gè)數(shù)也是在前半部分?jǐn)?shù)組的第i個(gè)位置,所以遞歸時(shí)所查找的位置還是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個(gè)元素的地址大于主元地址</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;">所查找的地址在數(shù)組的后半段,所在的地址是<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];}//尋找一個(gè)nTmpPos下標(biāo),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>

總結(jié)

以上是生活随笔為你收集整理的随机查找数组中第i个元素(按顺序排列的)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。