C++程序设计YDaniel Liang 第十八十九二十章课后习题答案Word下载.docx
《C++程序设计YDaniel Liang 第十八十九二十章课后习题答案Word下载.docx》由会员分享,可在线阅读,更多相关《C++程序设计YDaniel Liang 第十八十九二十章课后习题答案Word下载.docx(15页珍藏版)》请在冰豆网上搜索。
if(arraySize>
1)
{
//Mergesortthefirsthalf
T*firstHalf=newT[arraySize/2];
arraycopy(list,0,firstHalf,0,arraySize/2);
mergeSort(firstHalf,arraySize/2);
//Mergesortthesecondhalf
intsecondHalfLength=arraySize-arraySize/2;
T*secondHalf=newT[secondHalfLength];
arraycopy(list,arraySize/2,secondHalf,0,secondHalfLength);
mergeSort(secondHalf,secondHalfLength);
//MergefirstHalfwithsecondHalf
T*temp=newT[arraySize];
merge(firstHalf,arraySize/2,secondHalf,secondHalfLength,
temp);
arraycopy(temp,0,list,0,arraySize);
delete[]temp;
delete[]firstHalf;
delete[]secondHalf;
}
}
Tlist2[],intlist2Size,Ttemp[])
intcurrent1=0;
//Indexinlist1
intcurrent2=0;
//Indexinlist2
intcurrent3=0;
//Indexintemp
while(current1<
list1Size&
&
current2<
list2Size)
if(list1[current1]<
list2[current2])
temp[current3++]=list1[current1++];
else
temp[current3++]=list2[current2++];
list1Size)
while(current2<
Ttarget[],inttargetStartIndex,intlength)
for(inti=0;
i<
length;
i++)
target[i+targetStartIndex]=source[i+sourceStartIndex];
voidprintArray(Tlist[],intarraySize)
arraySize;
cout<
<
list[i]<
"
;
endl;
intmain()
intlist1[]={3,5,1,0,2,8,7};
mergeSort(list1,7);
printArray(list1,7);
doublelist2[]={3.5,0.5,1.4,0.4,2.5,1.8,4.7};
mergeSort(list2,7);
printArray(list2,7);
stringlist3[]={"
Atlanta"
"
Denver"
Chicago"
Dallas"
};
mergeSort(list3,4);
printArray(list3,4);
return0;
Exercise18_6
//Tisagenerictype
boolordered(Tlist[],intsize);
boolordered(Tlist[],intsize,boolascending);
boolordered(Tlist[],intsize)
boolordered=true;
size-1;
if(list[i]>
list[i+1]){
ordered=false;
break;
returnordered;
boolordered(Tlist[],intsize,boolascending)
if(ascending)
returnordered(list,size);
if(list[i]<
ordered(list1,7)<
ordered(list1,7,true)<
intlist2[]={1,2,3,5,7,8};
ordered(list2,6)<
ordered(list2,6,true)<
ordered(list2,6,false)<
Exercise19_2
vector>
string>
typenameElementType,typenameContainerType>
ElementTypemaxElement(ContainerType&
container,constElementType&
value)
container.size();
if(container[i]==value)
returni;
return-1;
intlist1[]={3,5,1,0,2,8,7,6};
vector<
int>
intVector(list1,list1+8);
Themaximumvalueinlist1:
<
(maxElement<
int,vector<
>
(intVector,8));
Exercise19_4
typenameContainerType>
voidreverse(ContainerType&
container)
intcount=0;
//returncount;
intlist1[]={3,5,1,8,2,8,7,6};
reverse<
vector<
(intVector);
Exercise19_6
intv1=10;
unsignedshortx=0XAEAE>
>
4;
x<
Exercise20_2
algorithm>
deque>
iterator>
intnextNum()
srand(time(0));
returnrand();
doublevalues[]={1.3,2.4,4.5,6.7,9.0};
deque<
double>
doubleDeque(values,values+5);
doubleDeque1(values,values+5);
ostream_iterator<