Here are some exercises for you to try on your own.
All three versions of linear search start at index 0. Rewrite all to search from the end of the list instead of from the beginning. Make sure you test them.
For the new versions of linear search: if there are duplicate values, which do they find?
Binary search is significantly faster than the built-in search, but requires that the list be sorted. As you know, the running time for the best sorting algorithm is on the order of N log2N, where N is the length of the list. If you search multiple times on the same list of data, it makes sense to sort it once before performing the searches. Roughly how many times do you need to search to make sorting and then searching faster than using the built-in search?
Given the unsorted list [6, 5, 4, 3, 7, 1, 2], show what the contents of the list would be after each iteration of the loop as it is sorted using the following:
Another sorting algorithm is bubble sort. Bubble sort involves keeping a sorted section at the end of the list. The list is traversed, pairs of elements are compared, and larger elements are swapped into the higher position. The operation is repeated until all elements are sorted.
Using the English description of bubble sort, write an outline of the bubble sort algorithm in English.
Continue using top-down design until you have a Python algorithm.
Turn it into a function called bubble_sort.
Try it out on the test cases from selection_sort.
In the description of bubble sort in the previous exercise, the sorted section of the list was at the end of the list. In this exercise, bubble sort will maintain the sorted section at the beginning of the list. Ensure that you are still implementing bubble sort!
Rewrite the English description of bubble sort from the previous exercise with the necessary changes so that the sorted elements are at the beginning of the list instead of at the end.
Using your English description of bubble sort, write an outline of the bubble sort algorithm in English.
Write the function bubble_sort_2.
Try it out on the test cases from selection_sort.
Modify the timing program to compare bubble sort with insertion and selection sort. Explain the results.
The analysis of bin_sort said, “Since N values have to be inserted, the overall running time is N log2N.” Point out a flaw in this reasoning, and explain whether it affects the overall conclusion.
There are at least two ways determine with loop conditions. One of them is to answer the question, “When is the work done?” and then negate it. In function merge in , the answer is, “When you run out of items in one of the two lists,” which is described by this expression: i1 == len(L1) or i2 == len(L2). Negating this leads to the condition i1 != len(L1) and i2 != len(L2).
Another way to come up with a loop condition is to ask, “What are the valid values of the loop index?” In the function merge, the answer to this is 0 <= i1 < len(L1) and 0 <= i2 < len(L2); since i1 and i2 start at zero, you can drop the comparisons with zero, giving you i1 < len(L1) and i2 < len(L2).
Is there another way to do it? Have you tried both approaches? Which do you prefer?
In the function mergesort in , there are two calls to extend. They are there because when the preceding loop ends, one of the two lists still has items in it that haven’t been processed. Rewrite that loop so that these extend calls aren’t needed.
Footnotes