Data Structures - Lab 04
Tuesday, Sept. 15th
Big-Oh Analysis of Sorting Algorithms
Do not begin this lab until you receive directions from the instructor!
This lab will operate in much the same manner as last week's lab. I
will ask you to do a fair amount of analysis of provided code, answer reflective
questions BEFORE you execute code, and then, finally, execute some code to see
what happens.
As with last week, I STRONGLY encourage you to work with a partner.
Activity Set A - insertionSort()
- Get with a partner.
- Download and save lab4.py
- WITHOUT EXECUTING THE CODE, consider the code for insertionSort()
- [Q1] Summarize what approach this method uses in sorting a list of data
- [Q2] Consider the list [3, 1, 4, 6, 2, 5]. Trace through the changes to this list as insertionSort() executes. Add a line to the table below for every time that the list is modified (notice this can happen in two places - in the while loop and right after the while loop is done but before the next iteration of the for loop). For an example of what I have in mind, look at yesterday's lecture notes. (note, you may not need all the rows in this table or you may need slightly more)
| Situation ... |
aList |
| Start |
[3, 1, 4, 6, 2, 5] |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
- [Q3] What did you observe? Why?
- Consider the code for timer()
- [Q4] What is this code going to do?
- [Q6] Given the answer that you got in [Q5] PREDICT how long you think it would take to sort an unorderedList of half as many items (5000 objects).
- [Q7] Repeat the commands above to create an unorderedList(5000) and submit that to the timer. Record your answer in the appropriate column in Table One.
- [Q8] By how much did your time from Q5 differ from your time from Q7? Did this factor match your prediction in Q6? Why or why not?
- [Q9] Continue to complete the first row of Table One by generating orderedList() of 10000 and 5000, reversedList()s of 10000 and 5000 and randomList()s of 10000 and 5000 recording the time for each variation.
- [Q10] Compare the four times that you observed when sorting lists of 10000 items. Notice that the four times are not roughly the same. Which one is significantly shorter? Why does the algorithm have to do less work in this situation?
- [Q11] Which one is significantly longer? Why does the algorithm have to do so much more work in this situation?
Activity Set B - selectionSort()
- WITHOUT EXECUTING THE CODE, consider the code for selectionSort()
- [Q12] Summarize what approach this method uses in sorting a list of data
- [Q13] Consider the list [3, 1, 4, 6, 2, 5]. Trace through the changes to this list as selectionSort() executes. Add a line to the table below for every time that the list is modified
| Situation ... |
aList |
| Start |
[3, 1, 4, 6, 2, 5] |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
- [Q14] What did you observe? Why?
- [Q16] Given the answer that you got in [Q15] PREDICT how long you think it would take to sort an unorderedList of half as many items (5000 objects).
- [Q17] Repeat the commands above to create an unorderedList(5000) and submit that to the timer. Record your answer in the appropriate column in Table One.
- [Q18] By how much did your time from Q15 differ from your time from Q17? Did this factor match your prediction in Q16? Why or why not?
- [Q19] Continue to complete the second row of Table One by generating orderedList() of 10000 and 5000, reversedList()s of 10000 and 5000 and randomList()s of 10000 and 5000 recording the time for each variation.
- [Q20] Compare the four times that you observed when sorting lists of 10000 items. Notice that this time, the four times are roughly the same. Why does the structure of the original data not make a difference with selectionSort() when it did with insertionSort()?
Activity Set C - bubbleSort()
- WITHOUT EXECUTING THE CODE, consider the code for bubbleSort()
- [Q21] Summarize what approach this method uses in sorting a list of data
- [Q22] Consider the list [3, 1, 4, 6, 2, 5]. Trace through the changes to this list as bubbleSort() executes. Add a line to the table below for every time that the list is modified
| Situation ... |
aList |
| Start |
[3, 1, 4, 6, 2, 5] |
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
- [Q23] What did you observe? Why?
- [Q25] Given the answer that you got in [Q24] PREDICT how long you think it would take to sort an unorderedList of half as many items (5000 objects).
- [Q26] Repeat the commands above to create an unorderedList(5000) and submit that to the timer. Record your answer in the appropriate column in Table One.
- [Q27] By how much did your time from Q24 differ from your time from Q26? Did this factor match your prediction in Q25? Why or why not?
- [Q28] Continue to complete the third row of Table One by generating orderedList() of 10000 and 5000, reversedList()s of 10000 and 5000 and randomList()s of 10000 and 5000 recording the time for each variation.
- [Q29] Compare the four times that you observed when sorting lists of 10000 items. How do they compare when considering the bubbleSort()? Are these times more like insertionSort() or more like selectionSort()?
Activity Set D - shortBubbleSort()
- WITHOUT EXECUTING THE CODE, consider the code for shortBubbleSort()
- [Q30] Summarize what approach this method uses in sorting a list of data. How is this code different from the original bubbleSort? What difference might this make?
- [Q32] How did your time from bubbleSort() compare to the time for shortBubbleSort()?
- [Q33] Repeat the commands above to create an unorderedList(5000) and submit that to the timer. Record your answer in the appropriate column in Table One.
- [Q34] Continue to complete the fourth row of Table One by generating orderedList() of 10000 and 5000, reversedList()s of 10000 and 5000 and randomList()s of 10000 and 5000 recording the time for each variation.
- [Q35] Compare the 8 times recorded for bubbleSort() with the 8 times recorded for shortBubbleSort(). Where do you observe any differences? Why?
Activity Set E - mergeSort()
- WITHOUT EXECUTING THE CODE, consider the code for mergeSort()
- [Q36] Summarize what approach this method uses in sorting a list of data
- [Q38] Given the answer that you got in [Q37 PREDICT how long you think it would take to sort an unorderedList of half as many items (5000 objects).
- [Q39] Repeat the commands above to create an unorderedList(5000) and submit that to the timer. Record your answer in the appropriate column in Table One.
- [Q40] By how much did your time from Q37 differ from your time from Q39? Did this factor match your prediction in Q38? Why or why not?
- [Q41] Continue to complete the third row of Table One by generating orderedList() of 10000 and 5000, reversedList()s of 10000 and 5000 and randomList()s of 10000 and 5000 recording the time for each variation.
- [Q42] Compare the four times that you observed when sorting lists of 10000 items. How do they compare when considering the mergeSort()? What do you observe when comparing them to themselves?
- [Q43] Overall, compare the times you observed with the mergeSort() with the times from the other sorts. How do they compare? Why?
Wrap Up
- [Q44] Consider the methods you worked with. Which was the most efficient (the fastest)?
- [Q45] Which one was the least effected by the doubling from 5000 to 10000?
- [Q46] Which method would you prefer to use?
- [Q47] Under which circumstances might you use one of the others?
- [Q48] Which method would you prefer to write from scratch off the top of your head (no reference materials)?
Ongoing Data Analysis
As you completed this lab you were occasionally instructed to run some tests with different data and record the results here. Do not complete the fields in this table until instructed to do so.
You will also use this completed table to answer wrap up questions.
| | unorderedList | orderedList | reversedList | randomList |
| | 10000 | 5000 | 10000 | 5000 | 10000 | 5000 | 10000 | 5000 |
| insertionSort() (activity a) | | | | | | | | |
| selectionSort() (activity b) | | | | | | | | |
| bubbleSort() (activity c) | | | | | | | | |
| shortBubbleSort() (activity d) | | | | | | | | |
| mergeSort() (activity e) | | | | | | | | |