![]() ![]() That is, as in the previous two problems, you should simply add m−1 to your running total of comparisons every time you recurse on a subarray with length m. In each step, the algorithm compares the input element x with the value of the middle element in array. SUBTLE POINT: A careful analysis would keep track of the comparisons made in identifying the median of the three candidate elements. As discussed in the first and second parts of this programming assignment, be sure to implement Partition exactly as described in the video lectures (including exchanging the pivot element with the first element just before the main Partition subroutine).ĮXAMPLE: For the input array 8 2 4 5 7 1 you would consider the first (8), middle (4), and last (1) elements since 4 is the median of the set, you would use 4 as your pivot element. So for the array 4 5 6 7, the "middle" element is the second one - 5 and not 6!) Identify which of these three elements is the median (i.e., the one whose value is in between the other two), and use this as your pivot. (If the array has odd length it should be clear what the "middle" element is for an array with even length 2k, use the k th element as the "middle" element. Consider the first, middle, and final elements of the given array. In more detail, you should choose the pivot as follows. Compute the number of comparisons (as in Problem 1), using the "median-of-three" pivot rule.Recall from the lectures that, just before the main Partition subroutine, you should exchange the pivot element (i.e., the last element) with the first element. Again, be sure to implement the Partition subroutine exactly as it is described in the video lectures. For this problem, you should implement the Partition subroutine exactly as it is described in the video lectures (otherwise you might get the wrong answer).įor the first part of the programming assignment, you should always use the first element of the array as the pivot element.Ĭompute the number of comparisons (as in Problem 1), always using the final element of the given array as the pivot element. WARNING: The Partition subroutine can be implemented in several different ways, and different implementations can give you differing numbers of comparisons. (This is because the pivot element is compared to each of the other m-1 elements in the subarray in this recursive call.) Rather, when there is a recursive call on a subarray of length m, you should simply add m-1 to your running total of comparisons. You should not count comparisons one-by-one. ![]() As you know, the number of comparisons depends on which elements are chosen as pivots, so we'll ask you to explore three different pivoting rules. Your task is to compute the total number of comparisons used to sort the given input file by QuickSort. Stanford University Engineering Courses: Divide and Conquer, Sorting and Searching, and Randomized Algorithms - Stanford School of Engineering & Stanford. Theseareheldtogetherandcoordinatedbythealgorithm'scorerecursivestructure. The integer in the i th row of the file gives you the i th entry of an input array. Thereal workisdonepiecemeal, inthreedifferentplaces: inthepartitioningofproblemsintosubproblems attheverytail endof therecursion, whenthesubproblemsaresosmallthattheyaresolvedoutright andinthegluingtogetherof partial answers. The file contains all of the integers between 1 and 10,000 (inclusive, with no repeats) in unsorted order. Time complexity (a worst-case running time): O(n 2).Time complexity (an average-case running time): O(n log 2 n).Var sparePeg = hanoi.getSparePeg(fromPeg, toPeg) īut despite all that, even though it only seems to ask for the solveHanoi() to be filled out with the parameters, it wont progress. About this course: The primary topics in this part of the specialization are: asymptotic ('Big-oh') notation, sorting and searching, divide and conquer (master method, integer and matrix multiplication, closest pair), and randomized algorithms (QuickSort, contraction algorithm for min cuts). You can find the spare peg by using the getSparePeg function.Ī call to hanoi.getSparePeg(peg1,peg2) returns the remaining peg that isn't peg1 or peg2. move (numDisks - 1) disks to the spare peg. Make a recursive function call to move the disks sitting on top of the bottom disk on the fromPeg to the spare peg, i.e. Posting here really about the(just prior to this page) stage 2 Challenge Solve hanoi recursively (no place to put questions on that page). ![]() Θ ( n lg n ) \Theta(n \lg n) Θ ( n l g n ) \Theta, left parenthesis, n, \lg, n, right parenthesis Θ ( n ) \Theta(n) Θ ( n ) \Theta, left parenthesis, n, right parenthesis Θ ( n 2 ) \Theta(n^2) Θ ( n 2 ) \Theta, left parenthesis, n, squared, right parenthesis ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |