Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of.

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of."— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of notes. Think about all of the homework / projects / quizzes we’ve had. I’ll be doing something similar. There will be a little programming. I have graded homework! I’m slowly getting projects graded – watch your email.

3 Call Trees A “call tree” shows the different calls to methods and their arguments. For example, in sorting {2,4,3,7,1,8}, if the pivot is 4, this will generate calls to qs {2,3,1} and qs {7, 8}. I’m not worried about the ordering of elements coming out of partition – I could have said {2,1,3} as well. Let’s draw a full tree on the board.

4 Complexity What’s the time complexity? partition takes O(s) time, where n = size of subarray (this is bounded by O(n), why?) So how many recursive calls are there and how long does each take? That depends on the shape of the call tree.

5 A Good Call Tree Suppose the partition always divides the array in half. What does the call tree look like in terms of size? What is the complexity in this case?

6 A Bad Call Tree Suppose the pivot happens to divide the array into unequal parts – the pivot is always the highest thing in the subarray. What does the call tree look like? What is the complexity? How might we get a “bad” tree?

7 Preventing Bad Trees a)Choose the pivot in the middle. This is the right choice for sorted data and it’s unlikely that this would be bad for any common data ordering b)Choose the pivot randomly – this defeats any pattern in the incoming data. Middle pivots are usually “good enough”. Note that a bad strategy has to be bad most of the time – a few bad picks don’t mess up the complexity.


Download ppt "CS 280 Data Structures Professor John Peterson. Test #1 We’ll do a test next week on Wednesday. It will take the entire period. You can have 1 page of."

Similar presentations


Ads by Google