中级农民
- 积分
- 106
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-6-23
- 最后登录
- 1970-1-1
|
本帖最后由 lesliere 于 2019-7-1 23:20 编辑
1. 开始在makeQueueOfQueues()我用了递归的做法,结果就是到10000个的时候就溢出了,不知道是自己的算法的问题还是这就是递归本身会存在的问题(这种会递归很多次的就是该避免用递归的方法?
2. 就在quicksort中,我开始分case的时候用的是if和else if结果在这里找了好久的bug。。。
3.另外我我觉得我写的的mergesort还是stable。。。然后自己按照看到的同学的思路写了一个testcase感觉还是stable(当然很可能是一个testcase不够),总之不太理解那个造成stable的原因,好想知道大家不stable的实现和我是哪里不同。。。贴一下我写的:
- public static LinkedQueue mergeSortedQueues(LinkedQueue q1, LinkedQueue q2) {
- // Replace the following line with your solution.
- LinkedQueue newQueue = new LinkedQueue();
- Comparable front1, front2;
- while(q1.size()!=0||q2.size()!=0){
- try{
- front1 = (Comparable)q1.front();
- }catch (QueueEmptyException e) {
- System.out.println("EMPTY QUEUE!");
- front1 = null;
- }
- try{
- front2 = (Comparable)q2.front();
- }catch (QueueEmptyException e) {
- System.out.println("EMPTY QUEUE!");
- front2 = null;
- }
- if (front2==null) {
- newQueue.enqueue(front1);
- try{
- q1.dequeue();
- }catch (QueueEmptyException e) {
- System.out.println("EMPTY QUEUE!");
- }
- }else if(front1==null){
- newQueue.enqueue(front2);
- try{
- q2.dequeue();
- }catch (QueueEmptyException e) {
- System.out.println("EMPTY QUEUE!");
- }
- }else if (front1.compareTo(front2)<=0) {
- newQueue.enqueue(front1);
- try{
- q1.dequeue();
- }catch (QueueEmptyException e) {
- System.out.println("EMPTY QUEUE!");
- }
- }else{
- newQueue.enqueue(front2);
- try{
- q2.dequeue();
- }catch (QueueEmptyException e) {
- System.out.println("EMPTY QUEUE!");
- }
- }
- }
-
- return newQueue;
- }
复制代码
|
 组图打开中,请稍候......
|