加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

经典算法5:用分治法实现元素选择

发布时间:2020-12-16 07:48:08 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 } 三、快速排序函数 quicksort(int a[],int p,int r) void __fastcall TForm1:: quicksort(int a[],int r) { ? ?if(pr) ? ? ? ? ? ? ? ? ? ? ? ? ? ?

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

}

三、快速排序函数 quicksort(int a[],int p,int r)
void __fastcall TForm1:: quicksort(int a[],int r)
{ ? ?if(p<r) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //把a[p]到a[r]的数进行快速排序
{
int q=partition(a,p,r,a[p]); ? ? ? ? //以a[p]为基准划分数组
? ? ? ? ? ? quicksort( a,q-1); ? ? ? ? ? ? ? ? ? //对左边的数组排序
quicksort( a,q+1,r); ? ? ? ? ? ? ? ? ? ? //对右边的数组排序
} ??
?}
四、选择第k小数的函数int select(int a[],int r,int k)
int __fastcall TForm1::select(int a[],int k)
{
? ?if(r-p<75) ? ? ? ? ? ? ? ? //当需选择的数个数小于75时
? { ? ? ? ? ? ? ? ? ? ? ? ?//对它进行快速排序
? ? ? quicksort(a,r); ??
? ? ? return a[p+k-1];
? ?}
for(int i=0;i<=(r-p-4)/5;i++)
? ?{ ? ? ? ? ? ? ? ? ? ? ? ?//将a[p+5*i]到a[p+5*I+4]的数进行快速排序
? ? ? ? quicksort(a,p+5*i,p+5*i+4);
? ? ? ? swap(a[p+i],a[p+5*i+2]); // 交换a[p+i] 和a[p+5*i+2]
? ? }
? ??int ?x=select(a,p+(r-p-4)/5,(r-p-4)/10); ?//找中位数的中位数
? ? int m=partition(a,x); ? ? ?//以中位数的中位数为基准划分数组
? ??int j=m-p+1;
? ? if(k<=j) ?
return ?select(a,m,k); ? //在中位数的中位数左边选择
? ?else
return ?select(a,m+1,k-j); ? ? ? //在中位数的中位数右边选择
}
五、数组生成函数 void create_array( )
void __fastcall TForm1::create_array()
{ ?
int max=120; ? ? ? ? ? ? ? ? //定义数组大小为120;
? ? AnsiString str;
? for(int i=0;i<max;i++)
? { ? ? //循环调用随机函数random()给array数组赋值
? ? array[i]=random(max); //该函数中的max值可以根据需要随意更改
? ? str="array["+IntToStr(i)+"]="+IntToStr(array[i])+";";
Memo1->Lines->Append(str); ? ? ? ?//在Memo组件中显示数组
? Memo1->Lines->SaveToFile("array_before.txt");
? } ? ? //把排序前的数组写入文件名为array_before.txt的文件中
}
六、开始选择函数 void begin_select( );
void __fastcall TForm1::begin_select( )
{?
? int ?no =StrToInt(Edit2->Text); ? ? ? //定义将要选择的第几小数
? int ?first=StrToInt(Edit4->Text); ? ? //定义选择区域的起始位置
? int ?last=StrToInt(Edit5->Text); ? ? //定义选择区域的终止位置
if( no>120 || no<0 || first>119 ||first<0 || last>119 || last<0 || first>last ? ? ? ?
? ? ? || ? first + no-1 > last)
{ ? ? ? ? ? ? ? ?//当no 、first和 last三个数的输入不对时给出消息框
Application->MessageBoxA("输入错误!","警告",48);
}
? ? ?else
{
? ? ? create_array(); ? ? ? ? ? ?//调用create_array()构造数组array
? ? ? int ?result=select(array,first,last,no); ? //调用select函数对数组 ? ?
Edit3->Text=IntToStr(result); ? ? ? ? ? ? ? ? ? ? ? //进行选择
? ? AnsiString string;
? ? ? for(int i=0;i<120;i++)
? ? ? { ? ? ?string="array["+IntToStr(i)+"]="+IntToStr(array[i])+";";
? ? ? ? ? ? ? ?Memo2->Lines->Append(string); ?//排序显示在Memo上
? ? ? ? ? ? ? Memo2->Lines->SaveToFile("array_after.txt");
? ? ? ? ? } //把排序前的数组写入文件名为array_after.txt的文件中
} ?
}
七、动态产生AboutBox窗体的代码
void __fastcall TForm1::N3Click(TObject *Sender)
{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //new一个AboutBox窗体
? ? ? AboutBox=new TAboutBox(Application);
? ? ? AboutBox->ShowModal(); ? ? ? ? ? ? //显示该窗体
? ? ? delete AboutBox; ? ? ? ? ? ? ? ? ? ? ? ? ? ? //释放该对象
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读