测试结果:
part one
public static LinkedQueue mergeSortedQueues(LinkedQueue q1, LinkedQueue q2) { // Replace the following line with your solution. LinkedQueue mergedQueue = new LinkedQueue(); int flag = 0; Object q1item=0; Object q2item=0; try{ while((!q1.isEmpty())&&(!q2.isEmpty())||((q1item!=null)&&(q2item!=null)&&flag==1)){ if(flag==0){q1item = q1.dequeue();q2item = q2.dequeue();} flag=1; while(((Comparable)(q1item)).compareTo(((Comparable)(q2item)))>=0){ mergedQueue.enqueue(q2item); if(!q2.isEmpty()){q2item=q2.dequeue();}else{q2item=null;break;} } while(q2item!=null&&((Comparable)(q1item)).compareTo(((Comparable)(q2item)))<0){ mergedQueue.enqueue(q1item); if(!q1.isEmpty()){q1item=q1.dequeue();}else{q1item=null;break;} } } if(flag==1){ //万一q1 q2是空的 if(q1item!=null){mergedQueue.enqueue(q1item);} if(q2item!=null){mergedQueue.enqueue(q2item);}} if(!q2.isEmpty()){ mergedQueue.append(q2); }else if(!q1.isEmpty()){ mergedQueue.append(q1); } }catch(QueueEmptyException e){ System.err.println("Error: attempt to dequeue from empty queue.");} return mergedQueue; }
mergeSortedQueues
public static LinkedQueue makeQueueOfQueues(LinkedQueue q) { // Replace the following line with your solution. LinkedQueue QueueOfQueues = new LinkedQueue(); try{ while(!q.isEmpty()){ Object OneQueue = q.dequeue(); LinkedQueue OneLinkedqueue = new LinkedQueue(); OneLinkedqueue.enqueue(OneQueue); QueueOfQueues.enqueue(OneLinkedqueue); } }catch(QueueEmptyException e){ System.err.println("Error: attempt to dequeue from empty queue.");} return QueueOfQueues; }
makeQueueOfQueues
public static void mergeSort(LinkedQueue q) { // Your solution here. LinkedQueue Queues = makeQueueOfQueues(q); try{ while(Queues.size()>1){ Queues.enqueue(mergeSortedQueues((LinkedQueue)Queues.dequeue(),(LinkedQueue)Queues.dequeue())); } }catch(QueueEmptyException e){ System.err.println("Error: attempt to dequeue from empty queue.");} q.append(Queues); }
mergeSortedQueues
part two
public static void quickSort(LinkedQueue q) { // Your solution here. if(!q.isEmpty()){ int pivotnum = 1+(int)(Math.random()*q.size()); Comparable pivot = (Comparable)q.nth(pivotnum); LinkedQueue qSmall = new LinkedQueue(); LinkedQueue qLarge = new LinkedQueue(); LinkedQueue qEquals= new LinkedQueue(); partition(q,pivot,qSmall,qEquals,qLarge); if(!qSmall.isEmpty()){ quickSort(qSmall);} if(!qLarge.isEmpty()){ quickSort(qLarge);} q.append(qSmall); q.append(qEquals); q.append(qLarge); } }
quickSort
public static void partition(LinkedQueue qIn, Comparable pivot, LinkedQueue qSmall, LinkedQueue qEquals, LinkedQueue qLarge) { // Your solution here. try{ while(!qIn.isEmpty()){ Object Item = qIn.dequeue(); if(((Comparable)(Item)).compareTo(pivot)>0){ qLarge.enqueue(Item); }else if(((Comparable)(Item)).compareTo(pivot)<0){ qSmall.enqueue(Item); }else{ qEquals.enqueue(Item); } } }catch(QueueEmptyException e){ System.err.println("Error: attempt to dequeue from empty queue.");} }
partition
part three 跑出来的答案不写了,结论是,quicksort比较快!!
part four 按照我的代码 mergesort unstable/quicksort stable
mergesort中,等于号放在第三个while中,mergesort会变成stable的。
while((!q1.isEmpty())&&(!q2.isEmpty())||((q1item!=null)&&(q2item!=null)&&flag==1)){ if(flag==0){q1item = q1.dequeue();q2item = q2.dequeue();} flag=1; while(((Comparable)(q1item)).compareTo(((Comparable)(q2item)))>=0){ mergedQueue.enqueue(q2item); if(!q2.isEmpty()){q2item=q2.dequeue();}else{q2item=null;break;} } while(q2item!=null&&((Comparable)(q1item)).compareTo(((Comparable)(q2item)))<0){ mergedQueue.enqueue(q1item); if(!q1.isEmpty()){q1item=q1.dequeue();}else{q1item=null;break;} } }
quicksort中,partition中依次dequeue决定了quicksort的stable性质。
最近课上讲了好多新知识,留个坑,整理下笔记!
时间: 2024-11-03 03:27:53