c++中返回K小元素问题

来源:互联网 发布:网络第一辣妈高云微博 编辑:程序博客网 时间:2024/04/29 23:30
以下程序为的是返回第K小元素,运行时输入1 5 3 4 2时,求第1,3,4小时对的,而第2,5的则刚好没有交换.是哪一步少运行了一次交换? #include using namespace std; void swap(int &a,int &b) { int temp=a; a=b; b=temp; }; int part(int a[],int begin,int end ,int x) { void swap(int &,int &); x=a[begin];//以首字母为定位,找位置 int i=begin; int j=end+1; while(ix);//第一小 swap(a[i],a[j]); } swap(a[begin],a[j]); return j; }; int find(int a[],int begin,int end,int k)//有N个数,end=N-1, { if(begin==end)return(a[begin]); int m=part(a,begin,end,a[begin]); int temp=m-begin+1; if(k==temp)return a[m]; else if(k>a[i]; cout<<"查找队列为:"; for( i=0;i<5;i++)cout<