打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
FindKthelement

#include <iostream>

#include<stdlib.h>

#include<fstream>

using namespace std;

int temp;int i,j;

void print(ofstream &write,int *a){

     write<<endl;

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

write<<a[i]<<' ';

    }

    write<<endl;

}

int FindKthelement(ofstream &write,int *a,int left,int right,int k){

int judge=a[left];i=left;left++;j=right;

print(write,a);

write<<"Moss: u have just made a recursion.This time left="<<left<<" right="<<right<<" k="<<k<<" judge="<<judge<<" i="<<i<<" j="<<j<<endl;

while(1){

while(a[left]<=judge){left++;}

while(a[right]>judge){right--;}

if(right>left){write<<"switch "<<a[left]<<" and "<<a[right]<<endl;temp=a[left];a[left]=a[right];a[right]=temp;}

else {break;}

}

write<<"break: left is "<<left<<" while right is "<<right<<endl;

if(k==left){return judge;}

else if(k<=left){temp=a[left-1];a[left-1]=a[i];a[i]=temp;right=left-2;write<<"choose the left.";FindKthelement(write,a,i,right,k);}

else{temp=a[left-1];a[left-1]=a[i];a[i]=temp;write<<"choose the right.";FindKthelement(write,a,right+1,j,k);}

}

int main()

{

    int a[10]={4,9,0,1,2,6,5,8,3,7};

//               4 3 0 1 2 6 5 8 9 7

ofstream write;

write.open("Findkthelement.txt");

write<<FindKthelement(write,a,0,9,10);

//int a[3]={5,9,1};

//write<<FindKthelement(a,0,2,2,3);

 //                0 1 2 3 4 5 6 7 8 9

//                {4,9,0,1,2,6,5,8,3,7};

//                 4 3 0 1 2 6 5 8 9 7

   return 0;

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
随机选择算法
消除文法的左递归
几种C++排序算法的实现(冒泡,归并,快速,插入,选择)
排序算法总结
102 LeetCode Online Judge 题目C# 练习Binary Tree Level Order Traversal
图像处理-05-浮雕效果处理
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服