One编程组第六次题目

数组分割算法

将无序的数组成的一个集合S,分成两个值相等的集合AB。(A+B=S<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

这个没有好的方法,组合找到一个A==S/2即可。

组合相应的算法结构是:

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image001.gif"></imagedata></shape>//src是源数据集合,currentIndex是在源集合里的当前下标,length为源集合的大小,dest是结果集合,num是结果集合的元素个数,初始化时,结果集合要和源集合的个数相等.
<shape id="_x0000_i1026" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/None.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image001.gif"></imagedata></shape>
voidmatch(int*src,intcurrentIndex,intlength,int*dest,intnum)
<shape id="_162_401_Open_Image" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75" o:spid="_x0000_i1027"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image002.gif"></imagedata></shape><shape id="_162_401_Closed_Image" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75" o:spid="_x0000_i1028"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image003.gif"></imagedata></shape>...{
<shape id="_x0000_i1029" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>
if(currentIndex==length)
<shape id="_x0000_i1030" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>
return;
<shape id="_x0000_i1031" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>
<shape id="_x0000_i1032" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>dest[num++]=src[currentIndex++];//
采用当前元素
<shape id="_x0000_i1033" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>
for(inti=0;i<num;i++)
<shape id="_271_298_Open_Image" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75" o:spid="_x0000_i1034"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image005.gif"></imagedata></shape><shape id="_271_298_Closed_Image" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75" o:spid="_x0000_i1035"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image006.gif"></imagedata></shape>...{
<shape id="_x0000_i1036" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>cout<<dest[i]<<"";
<shape id="_x0000_i1037" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image007.gif"></imagedata></shape>}
<shape id="_x0000_i1038" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>cout<<endl;
<shape id="_x0000_i1039" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>match(src,currentIndex,length,dest,num);//
选用当前元素进行递归
<shape id="_x0000_i1040" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/InBlock.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image004.gif"></imagedata></shape>match(src,currentIndex,length,dest,num-1);//
去除当前元素再进行递归
<shape id="_x0000_i1041" style="WIDTH: 8.25pt; HEIGHT: 12pt" alt="" type="#_x0000_t75"><imagedata o:href="http://images.youkuaiyun.com/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" src="file:///C:/DOCUME~1/lenovo/LOCALS~1/Temp/msohtml1/03/clip_image008.gif"></imagedata></shape>}

无序的数组中找第2大的数

因为只有两个数,所以先设两个变量,然后在数组中找一遍即可

不过效率是找最大数的两倍。

无序的数组中找第K大的数

这个算法讲起来较为复杂。这个从快速排序中得到的结论。

a) 随机在数组中选一个值c,并将此值放到数组末端

b) 从左边遍历数组,找到第一个比c小的值。停下,然后从右边遍历数组,找到第一个比c大的值然后停下。交换两个值所在的位置。直到i==j,交换a[i]c的位置。

c) 此时c所在的位置为j,那么c就为第j大的数。

d) 如果k > j,那么就在j后面的数组里用相同的方法找。

e) 如果k == j,恭喜找到了

f) 如果k<j,那么就在前面找。

g) 所以算法复杂度为O(klogk)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值