第十次作业
- Bubble Sort the list: 33, 56, 17, 8, 95, 22。Make sure the final result is from small to large.
Write out the list after the 2nd pass. (10 points)
17, 8, 33, 22, 56, 95
- Give a sorted array as list={60,65,75,80,90,95}. Design an algorithm to insert the value of x into the sorted array. Then test the algorithm with value 50,67,99.
思考:为什么选择插入点在list头上、中间、尾巴上的三个数作为算法测试的数据,你能解释吗?
for(int temp = 0; temp < list.length(); temp++) {
if( temp==0 &&x < list[ temp ]) {
list.insert(temp,x);
break;
}
if( temp==list.length()-1 && x > list[ temp]) {
list.insert(temp+1,x);
break;
}
else {
if( temp > list[ temp]&& temp < list[ temp+1] {
list.insert(temp,x);
break;
}
}
}
三个插入点分别代表三种判断方式,且迭代次数依次递增。
- What is the state of the stack after the following sequence of Push and Pop operations?
Push “anne”; Push “get”; Push “your” ; Pop; Push “my” Push “gun”
“anne”, “get”, “my”, “gun”