Read sections 5.1-5.6 in chapter 5 of the textbook Explorations in Computing.
For example, print_triangle(4) will print the following triangle
****
***
**
*
Hint: You may find the following print_stars function useful. You can include this function in your solution.
def print_stars(n) if (n==0) then print "\n" else print("*") print_stars(n-1) end endFor example, print_stars(3) will print: ***
One way to convince yourself that the algorithm works is to show that every possible position in the ordered list, including both the values themselves (when the key is found) and the positions between values (when the key is not found and the binary search should return nil), can be derived by following the algorithm for updating mid, low, and high. To do this, we're going to construct a complete binary search tree. First we introduce some notation for depicting nodes in the search tree:def binsearch(list, key) low = -1 high = list.length while low+1 < high mid = (low+high) / 2 if key == list[mid] then return mid elsif key < list[mid] then high = mid else low = mid end end return nil end
|
|
Non-Destructive Merge Sort Algorithm:
Step 1. Sort the first half of the array using merge sort.
Step 2. Sort the second half of the array using merge sort.
Step 3. Merge the two sorted halves to get a sorted result array.
list[list.length/2]
), which we
will call the pivot.
All of those that are less than the pivot go into one list
and all of those that are greater than or equal to the pivot go
into a second list. Then we sort these two sublists (recursively).
The final sorted list is the first sorted sublist followed by
the pivot followed by the second sorted sublist.
For example, if we want to sort the list
list = [56, 42, 82, 75, 18, 58, 27, 61, 84, 41, 21, 15, 71, 90, 33]we create two lists, separating the elements based on the pivot 61 (the "middle" value of the list):
list1 = [56, 42, 18, 58, 27, 41, 21, 15, 33] list2 = [82, 75, 84, 71, 90]
Each sublist is sorted recursively (final results shown for each sublist):
list1 = [15, 18, 21, 27, 33, 41, 42, 56, 58] list2 = [71, 75, 82, 84, 90]
and list1 + {61} + list2 are combined to show the final result
[15, 18, 21, 27, 33, 41, 42, 56, 58] + [61] + [71, 75, 82, 84, 90] => [15, 18, 21, 27, 33, 41, 42, 56, 58, 61, 71, 75, 82, 84, 90]
Consider this quick sort algorithm sorting a list of n integers.
Into what two sublists would this algorithm split the
list [56, 42, 18, 58, 27, 41, 21, 15, 33]
?
This algorithm is recursive. What is the base case for this recursion? (In other words, when does quick sort stop partitioning a list into two sublists?) BE CAREFUL: This is a little trickier than it seems.
The two sublists list1 and list2 need not be of the same length. One could even be empty. Construct an example list (of size 7 or less) that would assure that picking middle element always lead to two lists, list1 and list2 "almost" of equal size. This would assure the quicksort of an order of complexity of O(n log n). Give a simple example of a list and justify your answer.
Explain how an unlucky sequence of pivots could give this algorithm a worst case complexity of O(n2). Give a simple example of a list(size 7 or less) to justify your answer.