随笔 - 20  文章 - 1  评论 - 159  阅读 - 71121

分治法求第k小元素(vc++)


算法:

求一列数中的第k小元素,利用分治的策略进行递归求解。

首先随便指定一个数,这里我指定的是第一个数为第k小元素记为randK,将数组中其他的数与findK进行比较,比他小的放在左边,大的放在右边,如果randK左边的元素个数为k-1个,说明findK就是你所要找的元素,如果左边的元素个数>k-1,说明你要找的元素在左边的数中,继续使用相同的方法在左边的数中进行查找,如果左边的元素的个数<k-1,说明你要找的元素在右边的数中,则继续使用相同的办法在右边的数中进行查找。。。

代码:

#include<iostream.h>

#include <time.h>

#include<stdlib.h>

#define Array_max 200

 

         void swap(int &a,int &b){

                   int temp;

                   temp = a;

                   a = b;

                   b = temp;

         }

         void out(int a[],int n){

                   for(int i = 0;i<n;i++){

                            cout<<a[i]<<" ";

                            if(i % 8 == 0 && i != 0)

                                     cout<<endl;

                   }

                   cout<<endl;

         }

         int findP(int a[],int oP,int R){//oP是随机元素的位置开始为0oP左边,R右边

                   int randK = a[oP];//随机找一个数

                   int iL = oP + 1;

                   int iR = R;

                   while(true){

                            while(a[iL]<randK){iL++;}

                            while(a[iR]>=randK){iR--;}

                            if(iL>=iR) break;

                            swap(a[iL],a[iR]);

                            }

                   swap(a[oP],a[iR]);

                   return iR;

         }

         int findK(int a[],int oP,int n,int k){//n为要排序的元素的个数

                   int find_k;

                   int copy_k;

                   find_k = findP(a,oP,n);//指向获取元素的最小值,oP指向获取元素的最大值

                   copy_k = find_k - oP;

                   if(k == copy_k)

                            return a[find_k];

                   if(k > copy_k)

                            return findK(a,find_k + 1,n,k - copy_k-1);

                   if(k < copy_k)

                   return findK(a,oP,find_k - 1,k);

                   else return 0;

         }

          void main(){

                   int n;

                   int k;

Begin:

                   cout<<"Please input the length of your Array:";

                   cin>>n;

                   cout<<endl;

                   int *a = new int[n];

                   srand((unsigned)time(NULL)); //生成随机数

                   for(int i = 0; i<n;i++){

                            a[i] = rand()%100;

                   }

                   cout<<"The Random_Array_element is:"<<endl;

                   out(a,n);

                   cout<<"Pleae input the K:";

                   cin>>k;

                   if(k>n) goto Begin;//输入错误,重新输入

                   else

                   cout<<"The element you find now is:"<<findK(a,0,n-1,k-1)<<endl;

         }

posted on   铁拐李  阅读(6008)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
< 2009年12月 >
29 30 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示