cs61b homework8
part1 实现mergesort,很奇怪的是感觉按作业上的步骤并没有用到recursion,因为用到了makeQueueOfQueues这个函数,不过不用这个函数用recursion也是可以直接做的。
代码:
makeQueueOfQueues:
1 public static LinkedQueue makeQueueOfQueues(LinkedQueue q) { 2 if(q.isEmpty()) 3 return null; 4 try{ 5 LinkedQueue q1=new LinkedQueue(); 6 while(!q.isEmpty()){ 7 LinkedQueue template=new LinkedQueue(); 8 template.enqueue(q.dequeue()); 9 q1.enqueue(template); 10 } 11 return q1; 12 }catch(QueueEmptyException e){ 13 e.printStackTrace(); 14 return null; 15 } 16 17 }
mergeSortedQueues:
1 public static LinkedQueue mergeSortedQueues(LinkedQueue q1, LinkedQueue q2) { 2 LinkedQueue q=new LinkedQueue(); 3 try{ 4 while(!q1.isEmpty()&&!q2.isEmpty()){ 5 Comparable comp1=(Comparable)q1.front(); 6 Comparable comp2=(Comparable)q2.front(); 7 if(comp1.compareTo(comp2)<0) 8 q.enqueue(q1.dequeue()); 9 else if(comp1.compareTo(comp2)>0) 10 q.enqueue(q2.dequeue()); 11 else if(comp1.compareTo(comp2)==0){ 12 q.enqueue(q1.dequeue()); 13 q.enqueue(q2.dequeue()); 14 } 15 } 16 if(!q1.isEmpty()) 17 q.append(q1); 18 else if(!q2.isEmpty()) 19 q.append(q2); 20 return q; 21 }catch(QueueEmptyException e){ 22 e.printStackTrace(); 23 return q; 24 } 25 }
mergesort:
public static void mergeSort(LinkedQueue q) { try{ LinkedQueue queue=makeQueueOfQueues(q); while(queue.size()!=1){ queue.enqueue(mergeSortedQueues((LinkedQueue) queue.dequeue(),(LinkedQueue) queue.dequeue())); } LinkedQueue q1=(LinkedQueue) queue.dequeue(); while(!q1.isEmpty()) q.enqueue(q1.dequeue()); } catch(QueueEmptyException e){ e.printStackTrace(); } }
part2:实现quicksort,partition后不断recurse qSmall和qLarge
partition:
1 public static void partition(LinkedQueue qIn, Comparable pivot, 2 LinkedQueue qSmall, LinkedQueue qEquals, 3 LinkedQueue qLarge) { 4 try{ 5 if(qIn.size()<2) 6 return; 7 int n=qIn.size(); 8 Random rand=new Random(); 9 int r=rand.nextInt(n); 10 pivot=(Comparable)qIn.nth(r); 11 while(!qIn.isEmpty()){ 12 Comparable front=(Comparable)qIn.front(); 13 if(front.compareTo(pivot)<0) 14 qSmall.enqueue(qIn.dequeue()); 15 else if(front.compareTo(pivot)>0) 16 qLarge.enqueue(qIn.dequeue()); 17 else if(front.compareTo(pivot)==0) 18 qEquals.enqueue(qIn.dequeue()); 19 } 20 }catch(QueueEmptyException e){ 21 e.printStackTrace(); 22 } 23 }
quickSort:
public static void quickSort(LinkedQueue q) { if(q.size()<2) return; LinkedQueue qSmall=new LinkedQueue(); LinkedQueue qLarge=new LinkedQueue(); LinkedQueue qEquals=new LinkedQueue(); Comparable pivot=null; partition(q,pivot,qSmall,qEquals,qLarge); quickSort(qSmall); quickSort(qLarge); q.append(qSmall); q.append(qEquals); q.append(qLarge); }
运行结果:
Queue: [ 2 0 6 3 1 7 7 9 9 5 ] After merge sort: [ 0 1 2 3 5 6 7 7 9 9 ] Queue [ 8 0 9 3 9 8 3 0 1 9 ] After quick sort[ 0 0 1 3 3 8 8 9 9 9 ] Mergesort time, 1000000 Integers: 2971 msec. Quicksort time, 1000000 Integers: 2049 msec.
part3:
time为1000000如上
time为100000时,
Mergesort time, 100000 Integers: 207 msec.
Quicksort time, 100000 Integers: 133 msec.
time为10000:
Mergesort time, 10000 Integers: 12 msec.
Quicksort time, 10000 Integers: 11 msec.
time为1000:
Mergesort time, 1000 Integers: 4 msec.
Quicksort time, 1000 Integers: 2 msec.
time为100:
Mergesort time, 100 Integers: 1 msec.
Quicksort time, 100 Integers: 1 msec.
感觉Quicksort还是要快一些