question
stringlengths
34
5.67k
answer
stringlengths
20
20.1k
support_files
listlengths
0
4
metadata
dict
Write a program to determine the number pairs of values in an input file that are equal. If your first try is quadratic, think again and use Arrays.sort() to develop a linearithmic solution.
1.1.8 a) b -> converts the char "b" to String and prints it b) 197 -> sums the char codes of "b" and "c", converts to String and prints it c) e -> sums the char code of "a" with 4, converts it to char and prints it
[]
{ "number": "1.4.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Exercise" }
Modify binary search so that it always returns the element with the smallest index that matches the search element (and still guarantees logarithmic running time).
1.1.10 The array was not initialized and will generate a compile-time error.
[]
{ "number": "1.4.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Exercise" }
Write a program that, given two sorted arrays of N int values, prints all elements that appear in both arrays, in sorted order. The running time of your program should be proportional to N in the worst case.
1.1.12 0 1 2 3 4 4 3 2 1 0
[]
{ "number": "1.4.12", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Exercise" }
Closest pair (in one dimension). Write a program that, given an array a[] of N double values, finds a closest pair: two values whose difference is no greater than the difference of any other pair (in absolute value). The running time of your program should be linearithmic in the worst case.
1.1.16 311361142246
[]
{ "number": "1.4.16", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Farthest pair (in one dimension). Write a program that, given an array a[] of N double values, finds a farthest pair: two values whose difference is no smaller than the difference of any other pair (in absolute value). The running time of your program should be linear in the worst case.
1.1.17 The function never stops because it keeps calling itself on the first line, until a StackOverflowError occurs.
[]
{ "number": "1.4.17", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Local minimum of an array. Write a program that, given an array a[] of N distinct integers, finds a local minimum: an index i such that a[i-1] < a[i] < a[i+1]. Your program should use ~2lg N compares in the worst case.
1.1.18 mystery(2,25) is equal to 50 mystery(3,11) is equal to 33 mystery(a,b) computes a * b mystery2(2,25) is equal to 33554432 mystery2(3,11) is equal to 177147 mystery2(a,b) computes a^b
[]
{ "number": "1.4.18", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Local minimum of a matrix. Given an N-by-N array a[] of N^2 distinct integers, design an algorithm that runs in time proportional to N to find a local minimum: a pair of indices i and j such that a[i][j] < a[i+1][j], a[i][j] < a[i][j+1], a[i][j] < a[i-1][j], and a[i][j] < a[i][j-1]. The running time of your program should be proportional to N in the worst case.
1.1.19 The largest value of N that takes less than 1 hour to compute is 51.
[]
{ "number": "1.4.19", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Throwing two eggs from a building. Consider the previous question, but now suppose you only have two eggs, and your cost model is the number of throws. Devise a strategy to determine F such that the number of throws is at most 2√N, then find a way to reduce the cost to ~c√F. This is analogous to a situation where search hits (egg intact) are much cheaper than misses (egg broken).
1.1.25 For all N E NaturalNumbers and for all nonnegative integers a <= N and b <= N, the Euclidean algorithm computes the greatest common divisor of a and b. Proving that gcd(a, b) = gcd(b, a % b) gcd(20,5) = gcd(5,0) gcd(20,5) is 5 gcd(5,0) is also 5 http://math.stackexchange.com/questions/1274515/prove-that-euclids-algorithm-computes-the-gcd-of-any-pair-of-nonnegative-intege https://www.whitman.edu/mathematics/higher_math_online/section03.03.html
[]
{ "number": "1.4.25", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Queue with two stacks. Implement a queue with two stacks so that each queue operation takes a constant amortized number of stack operations. Hint: If you push elements onto a stack and then pop them all, they appear in reverse order. If you repeat this process, they’re now back in order.
1.1.27 - Binomial distribution For binomial(100, 50, 0.25) the estimate is around 5 billion calls.
[]
{ "number": "1.4.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Hot or cold. Your goal is to guess a secret integer between 1 and N. You repeatedly guess integers between 1 and N. After each guess you learn if your guess equals the secret integer (and the game stops). Otherwise, you learn if the guess is hotter (closer to) or colder (farther from) the secret number than your previous guess. Design an algorithm that finds the secret number in at most ~2 lg N guesses. Then design an algorithm that finds the secret number in at most ~ 1 lg N guesses.
1.1.34 - Filtering Print the maximum and minimum numbers -> Could be implemented as a filter Print the median of the numbers -> Requires saving all values Print the Kth smallest value, for K less than 100 -> Could be implemented as a filter with an array of size K Print the sum of the squares of the numbers -> Could be implemented as a filter Print the average of the N numbers -> Could be implemented as a filter Print the percentage of numbers greater than the average -> Requires saving all values Print the N numbers in increasing order -> Requires saving all values Print the N numbers in random order -> Requires saving all values Thanks to imyuewu (https://github.com/imyuewu) for suggesting a correction for the Kth smallest value case. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/175
[]
{ "number": "1.4.34", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Time costs for pushdown stacks. Justify the entries in the table below, which shows typical time costs for various pushdown stack implementations, using a cost model that counts both data references (references to data pushed onto the stack, either an array reference or a reference to an object’s instance variable) and objects created. | data structure | item type | cost to push N int values | | |------------------|-----------|---------------------------|----------------| | | | data references | objects created | | linked list | int | 2N | N | | | Integer | 3N | 2N | | resizing array | int | ~5N | lg N | | | Integer | ~5N | ~N |
1.1.35 - Dice simulation N has to be 6.000.000 before my empirical results match the exact results to three decimal places.
[]
{ "number": "1.4.35", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Creative Problem" }
Naive 3-sum implementation. Run experiments to evaluate the following implementation of the inner loop of ThreeSum: for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) for (int k = 0; k < N; k++) if (i < j && j < k) if (a[i] + a[j] + a[k] == 0) cnt++; Do so by developing a version of DoublingTest that computes the ratio of the running times of this program and ThreeSum.
1.1.38 - Binary search versus brute-force search largeW.txt results: Bruteforce: 760274 Duration: 9705800 nanoseconds. BinarySearch: 760274 Duration: 112000 nanoseconds. largeT.txt results: Bruteforce: 7328283 Duration: 1325191800 nanoseconds. BinarySearch: 7328279 Duration: 158600 nanoseconds. For testing: https://algs4.cs.princeton.edu/11model/tinyW.txt https://algs4.cs.princeton.edu/11model/tinyT.txt https://algs4.cs.princeton.edu/11model/largeW.txt https://algs4.cs.princeton.edu/11model/largeT.txt
[]
{ "number": "1.4.38", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.4, "section_title": "Analysis of Algorithms", "type": "Experiment" }
Show the contents of the id[] array and the number of times the array is accessed for each input pair when you use quick-find for the sequence 9-0 3-4 5-8 7-2 2-1 5-7 0-3 4-2.
1.1.1 a) 7 b) 200.0000002 c) true
[]
{ "number": "1.5.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Exercise" }
Do Exercise 1.5.1, but use quick-union (page 224). In addition, draw the forest of trees represented by the id[] array after each input pair is processed.
1.1.2 a) 1.618 -> double b) 10.0 -> double c) true -> boolean d) 33 -> String
[]
{ "number": "1.5.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Exercise" }
Show the contents of the sz[] and id[] arrays and the number of array accesses for each input pair corresponding to the weighted quick-union examples in the text (both the reference input and the worst-case input).
1.1.4 a) No such keyword as "then" in Java language b) Missing Parentheses on if conditional c) Nothing wrong d) Missing semicolon after the "then" clause
[]
{ "number": "1.5.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Exercise" }
Repeat Exercise 1.5.5 for weighted quick-union.
1.1.6 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
[]
{ "number": "1.5.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Exercise" }
Develop classes QuickUnionUF and QuickFindUF that implement quick-union and quick-find, respectively.
1.1.7 a) 3.00009 b) 499500 c) 10000
[]
{ "number": "1.5.7", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Exercise" }
Give a counterexample that shows why this intuitive implementation of union() for quick-find is not correct: public void union(int p, int q) { if (connected(p, q)) return; // Rename p’s component to q’s name. for (int i = 0; i < id.length; i++) if (id[i] == id[p]) id[i] = id[q]; count--; }
1.1.8 a) b -> converts the char "b" to String and prints it b) 197 -> sums the char codes of "b" and "c", converts to String and prints it c) e -> sums the char code of "a" with 4, converts it to char and prints it
[]
{ "number": "1.5.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Exercise" }
In the weighted quick-union algorithm, suppose that we set id[find(p)] to q instead of to id[find(q)]. Would the resulting algorithm be correct?
1.1.10 The array was not initialized and will generate a compile-time error.
[]
{ "number": "1.5.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Exercise" }
Quick-union with path compression. Modify quick-union (page 224) to include path compression, by adding a loop to union() that links every site on the paths from p and q to the roots of their trees to the root of the new tree. Give a sequence of input pairs that causes this method to produce a path of length 4. Note: The amortized cost per operation for this algorithm is known to be logarithmic.
1.1.12 0 1 2 3 4 4 3 2 1 0
[]
{ "number": "1.5.12", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Creative Problem" }
Amortized costs plots. Instrument your implementations from Exercise 1.5.7 to make amortized costs plots like those in the text.
1.1.16 311361142246
[]
{ "number": "1.5.16", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Creative Problem" }
Random connections. Develop a UF client ErdosRenyi that takes an integer value N from the command line, generates random pairs of integers between 0 and N-1, calling connected() to determine if they are connected and then union() if not (as in our development client), looping until all sites are connected, and printing the number of connections generated. Package your program as a static method count() that takes N as argument and returns the number of connections and a main() that takes N from the command line, calls count(), and prints the returned value.
1.1.17 The function never stops because it keeps calling itself on the first line, until a StackOverflowError occurs.
[]
{ "number": "1.5.17", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Creative Problem" }
Random grid generator. Write a program RandomGrid that takes an int value N from the command line, generates all the connections in an N-by-N grid, puts them in random order, randomly orients them (so that p q and q p are equally likely to occur), and prints the result to standard output. To randomly order the connections, use a RandomBag (see Exercise 1.3.34 on page 167). To encapsulate p and q in a single object, use the Connection nested class shown below. Package your program as two static methods: generate(), which takes N as argument and returns an array of connections, and main(), which takes N from the command line, calls generate(), and iterates through the returned array to print the connections. private class Connection { int p; int q; public Connection(int p, int q) { this.p = p; this.q = q; } }
1.1.18 mystery(2,25) is equal to 50 mystery(3,11) is equal to 33 mystery(a,b) computes a * b mystery2(2,25) is equal to 33554432 mystery2(3,11) is equal to 177147 mystery2(a,b) computes a^b
[]
{ "number": "1.5.18", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Creative Problem" }
Animation. Write a RandomGrid client (see Exercise 1.5.18) that uses UnionFind as in our development client to check connectivity and uses StdDraw to draw the connections as they are processed.
1.1.19 The largest value of N that takes less than 1 hour to compute is 51.
[]
{ "number": "1.5.19", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Creative Problem" }
Doubling test for random grids. Develop a performance-testing client that takes an int value T from the command line and performs T trials of the following experiment: Use your client from Exercise 1.5.18 to generate the connections in an N-by-N square grid, randomly oriented and in random order, then use UnionFind to determine connectivity as in our development client, looping until all sites are connected. For each N, print the value of N, the average number of connections processed, and the ratio of the running time to the previous. Use your program to validate the hypotheses in the text that the running times for quick-find and quick-union are quadratic and weighted quick-union is near-linear. Note: As N doubles, the number of sites in the grid increases by a factor of 4, so expect a doubling factor of 16 for quadratic and 4 for linear.
1.1.25 For all N E NaturalNumbers and for all nonnegative integers a <= N and b <= N, the Euclidean algorithm computes the greatest common divisor of a and b. Proving that gcd(a, b) = gcd(b, a % b) gcd(20,5) = gcd(5,0) gcd(20,5) is 5 gcd(5,0) is also 5 http://math.stackexchange.com/questions/1274515/prove-that-euclids-algorithm-computes-the-gcd-of-any-pair-of-nonnegative-intege https://www.whitman.edu/mathematics/higher_math_online/section03.03.html
[]
{ "number": "1.5.25", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 1, "chapter_title": "Fundamentals", "section": 1.5, "section_title": "Case Study: Union-Find", "type": "Experiment" }
Show, in the style of the example trace with Algorithm 2.1, how selection sort sorts the array E A S Y Q U E S T I O N.
2.1.1 a[] i min 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 1 E A S Y Q U E S T I O N 1 1 A E S Y Q U E S T I O N 2 6 A E S Y Q U E S T I O N 3 9 A E E Y Q U S S T I O N 4 11 A E E I Q U S S T Y O N 5 10 A E E I N U S S T Y O Q 6 11 A E E I N O S S T Y U Q 7 7 A E E I N O Q S T Y U S 8 11 A E E I N O Q S T Y U S 9 11 A E E I N O Q S S Y U T 10 10 A E E I N O Q S S T U Y 11 11 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.1.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
What is the maximum number of exchanges involving any particular element during selection sort? What is the average number of exchanges involving an element?
2.1.2 The maximum number of exchanges involving any particular item during selection sort is N - 1. This happens when the first item has the highest value in the unsorted array and the other values are sorted. For example, in the array 4 1 2 3, the element 4 will be swapped N - 1 times. The average number of exchanges involving an item is exactly 2, because there are exactly N exchanges and N items (and each exchange involves two items). Reference: https://algs4.cs.princeton.edu/21elementary/
[]
{ "number": "2.1.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Give an example of an array of N items that maximizes the number of times the test a[j] < a[min] fails (and, therefore, min gets updated) during the operation of selection sort (Algorithm 2.1).
2.1.3 Array: H G F E D C B A Thanks to QiotoF (https://github.com/QiotoF) for mentioning a better array solution for this exercise https://github.com/reneargento/algorithms-sedgewick-wayne/issues/76
[]
{ "number": "2.1.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Show, in the style of the example trace with Algorithm 2.2, how insertion sort sorts the array E A S Y Q U E S T I O N.
2.1.4 a[] i j 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 0 E A S Y Q U E S T I O N 1 0 A E S Y Q U E S T I O N 2 2 A E S Y Q U E S T I O N 3 3 A E S Y Q U E S T I O N 4 2 A E Q S Y U E S T I O N 5 4 A E Q S U Y E S T I O N 6 2 A E E Q S U Y S T I O N 7 5 A E E Q S S U Y T I O N 8 6 A E E Q S S T U Y I O N 9 3 A E E I Q S S T U Y O N 10 4 A E E I O Q S S T U Y N 11 4 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.1.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
For each of the two conditions in the inner for loop in insertion sort (Algorithm 2.2), describe an array of N items where that condition is always false when the loop terminates.
2.1.5 Condition 1: j > 0 -> When the array is reverse ordered Z Q K D C B A Condition 2: less(a[j], a[j - 1]) -> When the array is ordered A B C D E F G
[]
{ "number": "2.1.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Which method runs faster for an array with all keys identical, selection sort or insertion sort?
2.1.6 Insertion sort because it will only make one comparison with the previous element (per element) and won't exchange any elements, running in linear time. Selection sort will exchange each element with itself and will run in quadratic time. Thanks to glucu (https://github.com/glucu) for correcting the number of exchanges done in selection sort.
[]
{ "number": "2.1.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Which method runs faster for an array in reverse order, selection sort or insertion sort?
2.1.7 Selection sort because even though both selection sort and insertion sort will run in quadratic time, selection sort will only make N exchanges, while insertion sort will make N * N / 2 exchanges. Thanks to LudekCizinsky (https://github.com/LudekCizinsky), rg9a27 (https://github.com/rg9a27) and BOTbkcd (https://github.com/BOTbkcd) for correcting the number of exchanges in selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/194 and https://github.com/reneargento/algorithms-sedgewick-wayne/issues/211
[]
{ "number": "2.1.7", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Suppose that we use insertion sort on a randomly ordered array where elements have only one of three values. Is the running time linear, quadratic, or something in between?
2.1.8 Quadratic. Insertion sort's running time is linear when the array is already sorted or all elements are equal. With three possible values the running time quadratic.
[]
{ "number": "2.1.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Show, in the style of the example trace with Algorithm 2.3, how shellsort sorts the array E A S Y S H E L L S O R T Q U E S T I O N.
2.1.9 a[] h i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 E A S Y S H E L L S O R T Q U E S T I O N 13 13 13 E A S Y S H E L L S O R T Q U E S T I O N 13 14 14 E A S Y S H E L L S O R T Q U E S T I O N 13 15 2 E A E Y S H E L L S O R T Q U S S T I O N 13 16 3 E A E S S H E L L S O R T Q U S Y T I O N 13 17 17 E A E S S H E L L S O R T Q U S Y T I O N 13 18 18 E A E S S H E L L S O R T Q U S Y T I O N 13 19 19 E A E S S H E L L S O R T Q U S Y T I O N 13 20 20 E A E S S H E L L S O R T Q U S Y T I O N E A E S S H E L L S O R T Q U S Y T I O N 4 4 4 E A E S S H E L L S O R T Q U S Y T I O N 4 5 5 E A E S S H E L L S O R T Q U S Y T I O N 4 6 6 E A E S S H E L L S O R T Q U S Y T I O N 4 7 3 E A E L S H E S L S O R T Q U S Y T I O N 4 8 4 E A E L L H E S S S O R T Q U S Y T I O N 4 9 9 E A E L L H E S S S O R T Q U S Y T I O N 4 10 10 E A E L L H E S S S O R T Q U S Y T I O N 4 11 7 E A E L L H E R S S O S T Q U S Y T I O N 4 12 12 E A E L L H E R S S O S T Q U S Y T I O N 4 13 9 E A E L L H E R S Q O S T S U S Y T I O N 4 14 14 E A E L L H E R S Q O S T S U S Y T I O N 4 15 15 E A E L L H E R S Q O S T S U S Y T I O N 4 16 16 E A E L L H E R S Q O S T S U S Y T I O N 4 17 17 E A E L L H E R S Q O S T S U S Y T I O N 4 18 10 E A E L L H E R S Q I S T S O S Y T U O N 4 19 7 E A E L L H E O S Q I R T S O S Y T U S N 4 20 8 E A E L L H E O N Q I R S S O S T T U S Y E A E L L H E O N Q I R S S O S T T U S Y 1 1 0 A E E L L H E O N Q I R S S O S T T U S Y 1 2 2 A E E L L H E O N Q I R S S O S T T U S Y 1 3 3 A E E L L H E O N Q I R S S O S T T U S Y 1 4 4 A E E L L H E O N Q I R S S O S T T U S Y 1 5 3 A E E H L L E O N Q I R S S O S T T U S Y 1 6 3 A E E E H L L O N Q I R S S O S T T U S Y 1 7 7 A E E E H L L O N Q I R S S O S T T U S Y 1 8 7 A E E E H L L N O Q I R S S O S T T U S Y 1 9 9 A E E E H L L N O Q I R S S O S T T U S Y 1 10 5 A E E E H I L L N O Q R S S O S T T U S Y 1 11 11 A E E E H I L L N O Q R S S O S T T U S Y 1 12 12 A E E E H I L L N O Q R S S O S T T U S Y 1 13 13 A E E E H I L L N O Q R S S O S T T U S Y 1 14 10 A E E E H I L L N O O Q R S S S T T U S Y 1 15 15 A E E E H I L L N O O Q R S S S T T U S Y 1 16 16 A E E E H I L L N O O Q R S S S T T U S Y 1 17 17 A E E E H I L L N O O Q R S S S T T U S Y 1 18 18 A E E E H I L L N O O Q R S S S T T U S Y 1 19 16 A E E E H I L L N O O Q R S S S S T T U Y 1 20 20 A E E E H I L L N O O Q R S S S S T T U Y A E E E H I L L N O O Q R S S S S T T U Y
[]
{ "number": "2.1.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Why not use selection sort for h-sorting in shellsort?
2.1.10 Insertion sort is faster than selection sort for h-sorting because as "h" decreases, the array becomes partially sorted. Insertion sort makes less comparisons in partially sorted arrays than selection sort. Also, when h-sorting, eventually h will have an increment value of 1. Using selection sort with an increment value of 1 would be the same as using the standard selection sort algorithm from the beginning. This would make the steps with the previous increments to be unnecessary work. Thanks to jaeheonshim (https://github.com/jaeheonshim) for adding an extra reason not to use selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/254
[]
{ "number": "2.1.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Exercise" }
Deck sort. Explain how you would put a deck of cards in order by suit (in the order spades, hearts, clubs, diamonds) and by rank within each suit, with the restriction that the cards must be laid out face down in a row, and the only allowed operations are to check the values of two cards and to exchange two cards (keeping them face down).
2.1.13 - Deck sort I would use selection sort, comparing the cards first by suit, and if they have the same suit, by rank. As we are dealing with physical objects it makes sense to minimize the number of swaps. With selection sort it may be needed to look at more cards than insertion sort (twice as many in the average case), but swaps will be required at most 52 times versus at most 676 times. Thanks to zefrawg (https://github.com/zefrawg) and nedas-dev (https://github.com/nedas-dev) for improving this exercise: https://github.com/reneargento/algorithms-sedgewick-wayne/issues/198
[]
{ "number": "2.1.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Creative Problem" }
Dequeue sort. Explain how you would sort a deck of cards, with the restriction that the only allowed operations are to look at the values of the top two cards, to exchange the top two cards, and to move the top card to the bottom of the deck.
2.1.14 - Dequeue sort I would use a variation of bubble sort. 1- I would compare both top cards and, if the top card were bigger than the second card, I would swap them. 2- I would mark the top card, so I could know it was the first card (in this iteration) sent to the bottom of the deck. 3- I would send the top card to the bottom of the deck. 4- I would repeat steps 1 and 3 until the marked card becomes the second card in the deck 5- I would send the top card to the bottom of the deck (and the marked card is now at the top, signaling that a iteration is over) 6- If there were no swaps in this iteration, the deck is sorted. Otherwise, repeat steps 1 to 6. Nice explanation here: http://stackoverflow.com/questions/38061140/sort-a-deck-of-cards
[]
{ "number": "2.1.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Creative Problem" }
Expensive exchange. A clerk at a shipping company is charged with the task of rearranging a number of large crates in order of the time they are to be shipped out. Thus, the cost of compares is very low (just look at the labels) relative to the cost of exchanges (move the crates). The warehouse is nearly full—there is extra space sufficient to hold any one of the crates, but not two. What sorting method should the clerk use?
2.1.15 - Expensive exchange The clerk should use selection sort. Since the cost of compares is low, the N^2 complexity won't be a problem. And it guarantees a cost of at most N exchanges.
[]
{ "number": "2.1.15", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Creative Problem" }
Shellsort best case. What is the best case for shellsort? Justify your answer.
2.1.20 - Shellsort best case Just like insertion sort, the best case for shellsort is when the array is already ordered. This causes every element to be compared only once in each iteration. The time complexity in this case is O(n log n). Good explanation: https://www.toptal.com/developers/sorting-algorithms/shell-sort http://www.codingeek.com/algorithms/shell-sort-algorithm-explanation-implementation-and-complexity/
[]
{ "number": "2.1.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.1, "section_title": "Elementary Sorts", "type": "Creative Problem" }
Give a trace, in the style of the trace given at the beginning of this section, showing how the keys A E Q S U Y E I N O S T are merged with the abstract in-place merge() method.
2.1.1 a[] i min 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 1 E A S Y Q U E S T I O N 1 1 A E S Y Q U E S T I O N 2 6 A E S Y Q U E S T I O N 3 9 A E E Y Q U S S T I O N 4 11 A E E I Q U S S T Y O N 5 10 A E E I N U S S T Y O Q 6 11 A E E I N O S S T Y U Q 7 7 A E E I N O Q S T Y U S 8 11 A E E I N O Q S T Y U S 9 11 A E E I N O Q S S Y U T 10 10 A E E I N O Q S S T U Y 11 11 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.2.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Give traces, in the style of the trace given with Algorithm 2.4, showing how the keys E A S Y Q U E S T I O N are sorted with top-down mergesort.
2.1.2 The maximum number of exchanges involving any particular item during selection sort is N - 1. This happens when the first item has the highest value in the unsorted array and the other values are sorted. For example, in the array 4 1 2 3, the element 4 will be swapped N - 1 times. The average number of exchanges involving an item is exactly 2, because there are exactly N exchanges and N items (and each exchange involves two items). Reference: https://algs4.cs.princeton.edu/21elementary/
[]
{ "number": "2.2.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Answer Exercise 2.2.2 for bottom-up mergesort.
2.1.3 Array: H G F E D C B A Thanks to QiotoF (https://github.com/QiotoF) for mentioning a better array solution for this exercise https://github.com/reneargento/algorithms-sedgewick-wayne/issues/76
[]
{ "number": "2.2.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Does the abstract in-place merge produce proper output if and only if the two input subarrays are in sorted order? Prove your answer, or provide a counterexample.
2.1.4 a[] i j 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 0 E A S Y Q U E S T I O N 1 0 A E S Y Q U E S T I O N 2 2 A E S Y Q U E S T I O N 3 3 A E S Y Q U E S T I O N 4 2 A E Q S Y U E S T I O N 5 4 A E Q S U Y E S T I O N 6 2 A E E Q S U Y S T I O N 7 5 A E E Q S S U Y T I O N 8 6 A E E Q S S T U Y I O N 9 3 A E E I Q S S T U Y O N 10 4 A E E I O Q S S T U Y N 11 4 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.2.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Give the sequence of subarray sizes in the merges performed by both the top-down and the bottom-up mergesort algorithms, for N = 39.
2.1.5 Condition 1: j > 0 -> When the array is reverse ordered Z Q K D C B A Condition 2: less(a[j], a[j - 1]) -> When the array is ordered A B C D E F G
[]
{ "number": "2.2.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Write a program to compute the exact value of the number of array accesses used by top-down mergesort and by bottom-up mergesort. Use your program to plot the values for N from 1 to 512, and to compare the exact values with the upper bound 6N lg N.
2.1.6 Insertion sort because it will only make one comparison with the previous element (per element) and won't exchange any elements, running in linear time. Selection sort will exchange each element with itself and will run in quadratic time. Thanks to glucu (https://github.com/glucu) for correcting the number of exchanges done in selection sort.
[]
{ "number": "2.2.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Show that the number of compares used by mergesort is monotonically increasing (C(N+1) > C(N) for all N > 0).
2.1.7 Selection sort because even though both selection sort and insertion sort will run in quadratic time, selection sort will only make N exchanges, while insertion sort will make N * N / 2 exchanges. Thanks to LudekCizinsky (https://github.com/LudekCizinsky), rg9a27 (https://github.com/rg9a27) and BOTbkcd (https://github.com/BOTbkcd) for correcting the number of exchanges in selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/194 and https://github.com/reneargento/algorithms-sedgewick-wayne/issues/211
[]
{ "number": "2.2.7", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Suppose that Algorithm 2.4 is modified to skip the call on merge() whenever a[mid] <= a[mid+1]. Prove that the number of compares used to mergesort a sorted array is linear.
2.1.8 Quadratic. Insertion sort's running time is linear when the array is already sorted or all elements are equal. With three possible values the running time quadratic.
[]
{ "number": "2.2.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Use of a static array like aux[] is inadvisable in library software because multiple clients might use the class concurrently. Give an implementation of Merge that does not use a static array. Do not make aux[] local to merge() (see the Q&A for this section). Hint: Pass the auxiliary array as an argument to the recursive sort().
2.1.9 a[] h i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 E A S Y S H E L L S O R T Q U E S T I O N 13 13 13 E A S Y S H E L L S O R T Q U E S T I O N 13 14 14 E A S Y S H E L L S O R T Q U E S T I O N 13 15 2 E A E Y S H E L L S O R T Q U S S T I O N 13 16 3 E A E S S H E L L S O R T Q U S Y T I O N 13 17 17 E A E S S H E L L S O R T Q U S Y T I O N 13 18 18 E A E S S H E L L S O R T Q U S Y T I O N 13 19 19 E A E S S H E L L S O R T Q U S Y T I O N 13 20 20 E A E S S H E L L S O R T Q U S Y T I O N E A E S S H E L L S O R T Q U S Y T I O N 4 4 4 E A E S S H E L L S O R T Q U S Y T I O N 4 5 5 E A E S S H E L L S O R T Q U S Y T I O N 4 6 6 E A E S S H E L L S O R T Q U S Y T I O N 4 7 3 E A E L S H E S L S O R T Q U S Y T I O N 4 8 4 E A E L L H E S S S O R T Q U S Y T I O N 4 9 9 E A E L L H E S S S O R T Q U S Y T I O N 4 10 10 E A E L L H E S S S O R T Q U S Y T I O N 4 11 7 E A E L L H E R S S O S T Q U S Y T I O N 4 12 12 E A E L L H E R S S O S T Q U S Y T I O N 4 13 9 E A E L L H E R S Q O S T S U S Y T I O N 4 14 14 E A E L L H E R S Q O S T S U S Y T I O N 4 15 15 E A E L L H E R S Q O S T S U S Y T I O N 4 16 16 E A E L L H E R S Q O S T S U S Y T I O N 4 17 17 E A E L L H E R S Q O S T S U S Y T I O N 4 18 10 E A E L L H E R S Q I S T S O S Y T U O N 4 19 7 E A E L L H E O S Q I R T S O S Y T U S N 4 20 8 E A E L L H E O N Q I R S S O S T T U S Y E A E L L H E O N Q I R S S O S T T U S Y 1 1 0 A E E L L H E O N Q I R S S O S T T U S Y 1 2 2 A E E L L H E O N Q I R S S O S T T U S Y 1 3 3 A E E L L H E O N Q I R S S O S T T U S Y 1 4 4 A E E L L H E O N Q I R S S O S T T U S Y 1 5 3 A E E H L L E O N Q I R S S O S T T U S Y 1 6 3 A E E E H L L O N Q I R S S O S T T U S Y 1 7 7 A E E E H L L O N Q I R S S O S T T U S Y 1 8 7 A E E E H L L N O Q I R S S O S T T U S Y 1 9 9 A E E E H L L N O Q I R S S O S T T U S Y 1 10 5 A E E E H I L L N O Q R S S O S T T U S Y 1 11 11 A E E E H I L L N O Q R S S O S T T U S Y 1 12 12 A E E E H I L L N O Q R S S O S T T U S Y 1 13 13 A E E E H I L L N O Q R S S O S T T U S Y 1 14 10 A E E E H I L L N O O Q R S S S T T U S Y 1 15 15 A E E E H I L L N O O Q R S S S T T U S Y 1 16 16 A E E E H I L L N O O Q R S S S T T U S Y 1 17 17 A E E E H I L L N O O Q R S S S T T U S Y 1 18 18 A E E E H I L L N O O Q R S S S T T U S Y 1 19 16 A E E E H I L L N O O Q R S S S S T T U Y 1 20 20 A E E E H I L L N O O Q R S S S S T T U Y A E E E H I L L N O O Q R S S S S T T U Y
[]
{ "number": "2.2.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Exercise" }
Faster merge. Implement a version of merge() that copies the second half of a[] to aux[] in decreasing order and then does the merge back to a[]. This change allows you to remove the code to test that each of the halves has been exhausted from the inner loop. Note: The resulting sort is not stable (see page 341).
2.1.10 Insertion sort is faster than selection sort for h-sorting because as "h" decreases, the array becomes partially sorted. Insertion sort makes less comparisons in partially sorted arrays than selection sort. Also, when h-sorting, eventually h will have an increment value of 1. Using selection sort with an increment value of 1 would be the same as using the standard selection sort algorithm from the beginning. This would make the steps with the previous increments to be unnecessary work. Thanks to jaeheonshim (https://github.com/jaeheonshim) for adding an extra reason not to use selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/254
[]
{ "number": "2.2.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Creative Problem" }
Lower bound for average case. Prove that the expected number of compares used by any compare-based sorting algorithm must be at least ~N lg N (assuming that all possible orderings of the input are equally likely). Hint: The expected number of compares is at least the external path length of the compare tree (the sum of the lengths of the paths from the root to all leaves), which is minimized when it is balanced.
2.1.13 - Deck sort I would use selection sort, comparing the cards first by suit, and if they have the same suit, by rank. As we are dealing with physical objects it makes sense to minimize the number of swaps. With selection sort it may be needed to look at more cards than insertion sort (twice as many in the average case), but swaps will be required at most 52 times versus at most 676 times. Thanks to zefrawg (https://github.com/zefrawg) and nedas-dev (https://github.com/nedas-dev) for improving this exercise: https://github.com/reneargento/algorithms-sedgewick-wayne/issues/198
[]
{ "number": "2.2.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Creative Problem" }
Merging sorted queues. Develop a static method that takes two queues of sorted items as arguments and returns a queue that results from merging the queues into sorted order.
2.1.14 - Dequeue sort I would use a variation of bubble sort. 1- I would compare both top cards and, if the top card were bigger than the second card, I would swap them. 2- I would mark the top card, so I could know it was the first card (in this iteration) sent to the bottom of the deck. 3- I would send the top card to the bottom of the deck. 4- I would repeat steps 1 and 3 until the marked card becomes the second card in the deck 5- I would send the top card to the bottom of the deck (and the marked card is now at the top, signaling that a iteration is over) 6- If there were no swaps in this iteration, the deck is sorted. Otherwise, repeat steps 1 to 6. Nice explanation here: http://stackoverflow.com/questions/38061140/sort-a-deck-of-cards
[]
{ "number": "2.2.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Creative Problem" }
Bottom-up queue mergesort. Develop a bottom-up mergesort implementation based on the following approach: Given N items, create N queues, each containing one of the items. Create a queue of the N queues. Then repeatedly apply the merging operation of Exercise 2.2.14 to the first two queues and reinsert the merged queue at the end. Repeat until the queue of queues contains only one queue.
2.1.15 - Expensive exchange The clerk should use selection sort. Since the cost of compares is low, the N^2 complexity won't be a problem. And it guarantees a cost of at most N exchanges.
[]
{ "number": "2.2.15", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Creative Problem" }
Indirect sort. Develop and implement a version of mergesort that does not rearrange the array, but returns an int[] array perm such that perm[i] is the index of the ith smallest entry in the array.
2.1.20 - Shellsort best case Just like insertion sort, the best case for shellsort is when the array is already ordered. This causes every element to be compared only once in each iteration. The time complexity in this case is O(n log n). Good explanation: https://www.toptal.com/developers/sorting-algorithms/shell-sort http://www.codingeek.com/algorithms/shell-sort-algorithm-explanation-implementation-and-complexity/
[]
{ "number": "2.2.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.2, "section_title": "Mergesort", "type": "Creative Problem" }
Show, in the style of the trace given with partition(), how that method partitions the array E A S Y Q U E S T I O N.
2.1.1 a[] i min 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 1 E A S Y Q U E S T I O N 1 1 A E S Y Q U E S T I O N 2 6 A E S Y Q U E S T I O N 3 9 A E E Y Q U S S T I O N 4 11 A E E I Q U S S T Y O N 5 10 A E E I N U S S T Y O Q 6 11 A E E I N O S S T Y U Q 7 7 A E E I N O Q S T Y U S 8 11 A E E I N O Q S T Y U S 9 11 A E E I N O Q S S Y U T 10 10 A E E I N O Q S S T U Y 11 11 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.3.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Show, in the style of the quicksort trace given in this section, how quicksort sorts the array E A S Y Q U E S T I O N (for the purposes of this exercise, ignore the initial shuffle).
2.1.2 The maximum number of exchanges involving any particular item during selection sort is N - 1. This happens when the first item has the highest value in the unsorted array and the other values are sorted. For example, in the array 4 1 2 3, the element 4 will be swapped N - 1 times. The average number of exchanges involving an item is exactly 2, because there are exactly N exchanges and N items (and each exchange involves two items). Reference: https://algs4.cs.princeton.edu/21elementary/
[]
{ "number": "2.3.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
What is the maximum number of times during the execution of Quick.sort() that the largest item can be exchanged, for an array of length N?
2.1.3 Array: H G F E D C B A Thanks to QiotoF (https://github.com/QiotoF) for mentioning a better array solution for this exercise https://github.com/reneargento/algorithms-sedgewick-wayne/issues/76
[]
{ "number": "2.3.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Suppose that the initial random shuffle is omitted. Give six arrays of ten elements for which Quick.sort() uses the worst-case number of compares.
2.1.4 a[] i j 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 0 E A S Y Q U E S T I O N 1 0 A E S Y Q U E S T I O N 2 2 A E S Y Q U E S T I O N 3 3 A E S Y Q U E S T I O N 4 2 A E Q S Y U E S T I O N 5 4 A E Q S U Y E S T I O N 6 2 A E E Q S U Y S T I O N 7 5 A E E Q S S U Y T I O N 8 6 A E E Q S S T U Y I O N 9 3 A E E I Q S S T U Y O N 10 4 A E E I O Q S S T U Y N 11 4 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.3.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Give a code fragment that sorts an array that is known to consist of items having just two distinct keys.
2.1.5 Condition 1: j > 0 -> When the array is reverse ordered Z Q K D C B A Condition 2: less(a[j], a[j - 1]) -> When the array is ordered A B C D E F G
[]
{ "number": "2.3.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Write a program to compute the exact value of CN, and compare the exact value with the approximation 2N ln N, for N = 100, 1,000, and 10,000.
2.1.6 Insertion sort because it will only make one comparison with the previous element (per element) and won't exchange any elements, running in linear time. Selection sort will exchange each element with itself and will run in quadratic time. Thanks to glucu (https://github.com/glucu) for correcting the number of exchanges done in selection sort.
[]
{ "number": "2.3.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Find the expected number of subarrays of size 0, 1, and 2 when quicksort is used to sort an array of N items with distinct keys. If you are mathematically inclined, do the math; if not, run some experiments to develop hypotheses.
2.1.7 Selection sort because even though both selection sort and insertion sort will run in quadratic time, selection sort will only make N exchanges, while insertion sort will make N * N / 2 exchanges. Thanks to LudekCizinsky (https://github.com/LudekCizinsky), rg9a27 (https://github.com/rg9a27) and BOTbkcd (https://github.com/BOTbkcd) for correcting the number of exchanges in selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/194 and https://github.com/reneargento/algorithms-sedgewick-wayne/issues/211
[]
{ "number": "2.3.7", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
About how many compares will Quick.sort() make when sorting an array of N items that are all equal?
2.1.8 Quadratic. Insertion sort's running time is linear when the array is already sorted or all elements are equal. With three possible values the running time quadratic.
[]
{ "number": "2.3.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Explain what happens when Quick.sort() is run on an array having items with just two distinct keys, and then explain what happens when it is run on an array having just three distinct keys.
2.1.9 a[] h i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 E A S Y S H E L L S O R T Q U E S T I O N 13 13 13 E A S Y S H E L L S O R T Q U E S T I O N 13 14 14 E A S Y S H E L L S O R T Q U E S T I O N 13 15 2 E A E Y S H E L L S O R T Q U S S T I O N 13 16 3 E A E S S H E L L S O R T Q U S Y T I O N 13 17 17 E A E S S H E L L S O R T Q U S Y T I O N 13 18 18 E A E S S H E L L S O R T Q U S Y T I O N 13 19 19 E A E S S H E L L S O R T Q U S Y T I O N 13 20 20 E A E S S H E L L S O R T Q U S Y T I O N E A E S S H E L L S O R T Q U S Y T I O N 4 4 4 E A E S S H E L L S O R T Q U S Y T I O N 4 5 5 E A E S S H E L L S O R T Q U S Y T I O N 4 6 6 E A E S S H E L L S O R T Q U S Y T I O N 4 7 3 E A E L S H E S L S O R T Q U S Y T I O N 4 8 4 E A E L L H E S S S O R T Q U S Y T I O N 4 9 9 E A E L L H E S S S O R T Q U S Y T I O N 4 10 10 E A E L L H E S S S O R T Q U S Y T I O N 4 11 7 E A E L L H E R S S O S T Q U S Y T I O N 4 12 12 E A E L L H E R S S O S T Q U S Y T I O N 4 13 9 E A E L L H E R S Q O S T S U S Y T I O N 4 14 14 E A E L L H E R S Q O S T S U S Y T I O N 4 15 15 E A E L L H E R S Q O S T S U S Y T I O N 4 16 16 E A E L L H E R S Q O S T S U S Y T I O N 4 17 17 E A E L L H E R S Q O S T S U S Y T I O N 4 18 10 E A E L L H E R S Q I S T S O S Y T U O N 4 19 7 E A E L L H E O S Q I R T S O S Y T U S N 4 20 8 E A E L L H E O N Q I R S S O S T T U S Y E A E L L H E O N Q I R S S O S T T U S Y 1 1 0 A E E L L H E O N Q I R S S O S T T U S Y 1 2 2 A E E L L H E O N Q I R S S O S T T U S Y 1 3 3 A E E L L H E O N Q I R S S O S T T U S Y 1 4 4 A E E L L H E O N Q I R S S O S T T U S Y 1 5 3 A E E H L L E O N Q I R S S O S T T U S Y 1 6 3 A E E E H L L O N Q I R S S O S T T U S Y 1 7 7 A E E E H L L O N Q I R S S O S T T U S Y 1 8 7 A E E E H L L N O Q I R S S O S T T U S Y 1 9 9 A E E E H L L N O Q I R S S O S T T U S Y 1 10 5 A E E E H I L L N O Q R S S O S T T U S Y 1 11 11 A E E E H I L L N O Q R S S O S T T U S Y 1 12 12 A E E E H I L L N O Q R S S O S T T U S Y 1 13 13 A E E E H I L L N O Q R S S O S T T U S Y 1 14 10 A E E E H I L L N O O Q R S S S T T U S Y 1 15 15 A E E E H I L L N O O Q R S S S T T U S Y 1 16 16 A E E E H I L L N O O Q R S S S T T U S Y 1 17 17 A E E E H I L L N O O Q R S S S T T U S Y 1 18 18 A E E E H I L L N O O Q R S S S T T U S Y 1 19 16 A E E E H I L L N O O Q R S S S S T T U Y 1 20 20 A E E E H I L L N O O Q R S S S S T T U Y A E E E H I L L N O O Q R S S S S T T U Y
[]
{ "number": "2.3.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Chebyshev’s inequality says that the probability that a random variable is more than k standard deviations away from the mean is less than 1/k^2. For N = 1 million, use Chebyshev’s inequality to bound the probability that the number of compares used by quicksort is more than 100 billion (.1 N^2).
2.1.10 Insertion sort is faster than selection sort for h-sorting because as "h" decreases, the array becomes partially sorted. Insertion sort makes less comparisons in partially sorted arrays than selection sort. Also, when h-sorting, eventually h will have an increment value of 1. Using selection sort with an increment value of 1 would be the same as using the standard selection sort algorithm from the beginning. This would make the steps with the previous increments to be unnecessary work. Thanks to jaeheonshim (https://github.com/jaeheonshim) for adding an extra reason not to use selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/254
[]
{ "number": "2.3.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
What is the recursive depth of quicksort, in the best, worst, and average cases? This is the size of the stack that the system needs to keep track of the recursive calls. See Exercise 2.3.20 for a way to guarantee that the recursive depth is logarithmic in the worst case.
2.1.13 - Deck sort I would use selection sort, comparing the cards first by suit, and if they have the same suit, by rank. As we are dealing with physical objects it makes sense to minimize the number of swaps. With selection sort it may be needed to look at more cards than insertion sort (twice as many in the average case), but swaps will be required at most 52 times versus at most 676 times. Thanks to zefrawg (https://github.com/zefrawg) and nedas-dev (https://github.com/nedas-dev) for improving this exercise: https://github.com/reneargento/algorithms-sedgewick-wayne/issues/198
[]
{ "number": "2.3.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Prove that when running quicksort on an array with N distinct items, the probability of comparing the ith and jth largest items is 2/(j - i). Then use this result to prove Proposition K.
2.1.14 - Dequeue sort I would use a variation of bubble sort. 1- I would compare both top cards and, if the top card were bigger than the second card, I would swap them. 2- I would mark the top card, so I could know it was the first card (in this iteration) sent to the bottom of the deck. 3- I would send the top card to the bottom of the deck. 4- I would repeat steps 1 and 3 until the marked card becomes the second card in the deck 5- I would send the top card to the bottom of the deck (and the marked card is now at the top, signaling that a iteration is over) 6- If there were no swaps in this iteration, the deck is sorted. Otherwise, repeat steps 1 to 6. Nice explanation here: http://stackoverflow.com/questions/38061140/sort-a-deck-of-cards
[]
{ "number": "2.3.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Exercise" }
Nuts and bolts. (G. J. E. Rawlins) You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is bigger, but it is not possible to directly compare two nuts or two bolts. Give an efficient method for solving the problem.
2.1.15 - Expensive exchange The clerk should use selection sort. Since the cost of compares is low, the N^2 complexity won't be a problem. And it guarantees a cost of at most N exchanges.
[]
{ "number": "2.3.15", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Creative Problem" }
Nonrecursive quicksort. Implement a nonrecursive version of quicksort based on a main loop where a subarray is popped from a stack to be partitioned, and the resulting subarrays are pushed onto the stack. Note: Push the larger of the subarrays onto the stack first, which guarantees that the stack will have at most lg N entries.
2.1.20 - Shellsort best case Just like insertion sort, the best case for shellsort is when the array is already ordered. This causes every element to be compared only once in each iteration. The time complexity in this case is O(n log n). Good explanation: https://www.toptal.com/developers/sorting-algorithms/shell-sort http://www.codingeek.com/algorithms/shell-sort-algorithm-explanation-implementation-and-complexity/
[]
{ "number": "2.3.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Creative Problem" }
Java system sort. Add to your implementation from Exercise 2.3.22 code to use the Tukey ninther to compute the partitioning item—choose three sets of three items, take the median of each, then use the median of the three medians as the partitioning item. Also, add a cutoff to insertion sort for small subarrays.
2.1.23 - Deck sort They separate all cards by suit, making the swaps in order to keep cards in the same suit next to each other (dividing the row in 4 areas). Then, in each suit, they sort the cards by rank using selection sort. In the second step (once suits are sorted), some of them move the unsorted smaller ranks near to the front while searching for the next smallest rank.
[]
{ "number": "2.3.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Creative Problem" }
Samplesort. (W. Frazer and A. McKellar) Implement a quicksort based on using a sample of size 2k - 1. First, sort the sample, then arrange to have the recursive routine partition on the median of the sample and to move the two halves of the rest of the sample to each subarray, such that they can be used in the subarrays, without having to be sorted again. This algorithm is called samplesort.
2.1.24 - Insertion sort with sentinel For 100000 random doubles Insertion Sort default is 1.1 times faster than Insertion Sort with a sentinel Insertion Sort default is slightly faster than Insertion Sort with a sentinel.
[]
{ "number": "2.3.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.3, "section_title": "Quicksort", "type": "Creative Problem" }
Suppose that the sequence P R I O * R * * I * T * Y * * * Q U E * * * U * E (where a letter means insert and an asterisk means remove the maximum) is applied to an initially empty priority queue. Give the sequence of letters returned by the remove the maximum operations.
2.1.1 a[] i min 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 1 E A S Y Q U E S T I O N 1 1 A E S Y Q U E S T I O N 2 6 A E S Y Q U E S T I O N 3 9 A E E Y Q U S S T I O N 4 11 A E E I Q U S S T Y O N 5 10 A E E I N U S S T Y O Q 6 11 A E E I N O S S T Y U Q 7 7 A E E I N O Q S T Y U S 8 11 A E E I N O Q S T Y U S 9 11 A E E I N O Q S S Y U T 10 10 A E E I N O Q S S T U Y 11 11 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.4.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Criticize the following idea: To implement find the maximum in constant time, why not use a stack or a queue, but keep track of the maximum value inserted so far, then return that value for find the maximum?
2.1.2 The maximum number of exchanges involving any particular item during selection sort is N - 1. This happens when the first item has the highest value in the unsorted array and the other values are sorted. For example, in the array 4 1 2 3, the element 4 will be swapped N - 1 times. The average number of exchanges involving an item is exactly 2, because there are exactly N exchanges and N items (and each exchange involves two items). Reference: https://algs4.cs.princeton.edu/21elementary/
[]
{ "number": "2.4.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Provide priority-queue implementations that support insert and remove the maximum, one for each of the following underlying data structures: unordered array, ordered array, unordered linked list, and linked list. Give a table of the worst-case bounds for each operation for each of your four implementations.
2.1.3 Array: H G F E D C B A Thanks to QiotoF (https://github.com/QiotoF) for mentioning a better array solution for this exercise https://github.com/reneargento/algorithms-sedgewick-wayne/issues/76
[]
{ "number": "2.4.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Is an array that is sorted in decreasing order a max-oriented heap?
2.1.4 a[] i j 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 0 E A S Y Q U E S T I O N 1 0 A E S Y Q U E S T I O N 2 2 A E S Y Q U E S T I O N 3 3 A E S Y Q U E S T I O N 4 2 A E Q S Y U E S T I O N 5 4 A E Q S U Y E S T I O N 6 2 A E E Q S U Y S T I O N 7 5 A E E Q S S U Y T I O N 8 6 A E E Q S S T U Y I O N 9 3 A E E I Q S S T U Y O N 10 4 A E E I O Q S S T U Y N 11 4 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.4.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Give the heap that results when the keys E A S Y Q U E S T I O N are inserted in that order into an initially empty max-oriented heap.
2.1.5 Condition 1: j > 0 -> When the array is reverse ordered Z Q K D C B A Condition 2: less(a[j], a[j - 1]) -> When the array is ordered A B C D E F G
[]
{ "number": "2.4.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Using the conventions of Exercise 2.4.1, give the sequence of heaps produced when the operations P R I O * R * * I * T * Y * * * Q U E * * * U * E are performed on an initially empty max-oriented heap.
2.1.6 Insertion sort because it will only make one comparison with the previous element (per element) and won't exchange any elements, running in linear time. Selection sort will exchange each element with itself and will run in quadratic time. Thanks to glucu (https://github.com/glucu) for correcting the number of exchanges done in selection sort.
[]
{ "number": "2.4.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
The largest item in a heap must appear in position 1, and the second largest must be in position 2 or position 3. Give the list of positions in a heap of size 31 where the kth largest (i) can appear, and (ii) cannot appear, for k=2, 3, 4 (assuming the values to be distinct).
2.1.7 Selection sort because even though both selection sort and insertion sort will run in quadratic time, selection sort will only make N exchanges, while insertion sort will make N * N / 2 exchanges. Thanks to LudekCizinsky (https://github.com/LudekCizinsky), rg9a27 (https://github.com/rg9a27) and BOTbkcd (https://github.com/BOTbkcd) for correcting the number of exchanges in selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/194 and https://github.com/reneargento/algorithms-sedgewick-wayne/issues/211
[]
{ "number": "2.4.7", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Answer the previous exercise for the kth smallest item.
2.1.8 Quadratic. Insertion sort's running time is linear when the array is already sorted or all elements are equal. With three possible values the running time quadratic.
[]
{ "number": "2.4.8", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Draw all of the different heaps that can be made from the five keys A B C D E, then draw all of the different heaps that can be made from the five keys A A A B B.
2.1.9 a[] h i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 E A S Y S H E L L S O R T Q U E S T I O N 13 13 13 E A S Y S H E L L S O R T Q U E S T I O N 13 14 14 E A S Y S H E L L S O R T Q U E S T I O N 13 15 2 E A E Y S H E L L S O R T Q U S S T I O N 13 16 3 E A E S S H E L L S O R T Q U S Y T I O N 13 17 17 E A E S S H E L L S O R T Q U S Y T I O N 13 18 18 E A E S S H E L L S O R T Q U S Y T I O N 13 19 19 E A E S S H E L L S O R T Q U S Y T I O N 13 20 20 E A E S S H E L L S O R T Q U S Y T I O N E A E S S H E L L S O R T Q U S Y T I O N 4 4 4 E A E S S H E L L S O R T Q U S Y T I O N 4 5 5 E A E S S H E L L S O R T Q U S Y T I O N 4 6 6 E A E S S H E L L S O R T Q U S Y T I O N 4 7 3 E A E L S H E S L S O R T Q U S Y T I O N 4 8 4 E A E L L H E S S S O R T Q U S Y T I O N 4 9 9 E A E L L H E S S S O R T Q U S Y T I O N 4 10 10 E A E L L H E S S S O R T Q U S Y T I O N 4 11 7 E A E L L H E R S S O S T Q U S Y T I O N 4 12 12 E A E L L H E R S S O S T Q U S Y T I O N 4 13 9 E A E L L H E R S Q O S T S U S Y T I O N 4 14 14 E A E L L H E R S Q O S T S U S Y T I O N 4 15 15 E A E L L H E R S Q O S T S U S Y T I O N 4 16 16 E A E L L H E R S Q O S T S U S Y T I O N 4 17 17 E A E L L H E R S Q O S T S U S Y T I O N 4 18 10 E A E L L H E R S Q I S T S O S Y T U O N 4 19 7 E A E L L H E O S Q I R T S O S Y T U S N 4 20 8 E A E L L H E O N Q I R S S O S T T U S Y E A E L L H E O N Q I R S S O S T T U S Y 1 1 0 A E E L L H E O N Q I R S S O S T T U S Y 1 2 2 A E E L L H E O N Q I R S S O S T T U S Y 1 3 3 A E E L L H E O N Q I R S S O S T T U S Y 1 4 4 A E E L L H E O N Q I R S S O S T T U S Y 1 5 3 A E E H L L E O N Q I R S S O S T T U S Y 1 6 3 A E E E H L L O N Q I R S S O S T T U S Y 1 7 7 A E E E H L L O N Q I R S S O S T T U S Y 1 8 7 A E E E H L L N O Q I R S S O S T T U S Y 1 9 9 A E E E H L L N O Q I R S S O S T T U S Y 1 10 5 A E E E H I L L N O Q R S S O S T T U S Y 1 11 11 A E E E H I L L N O Q R S S O S T T U S Y 1 12 12 A E E E H I L L N O Q R S S O S T T U S Y 1 13 13 A E E E H I L L N O Q R S S O S T T U S Y 1 14 10 A E E E H I L L N O O Q R S S S T T U S Y 1 15 15 A E E E H I L L N O O Q R S S S T T U S Y 1 16 16 A E E E H I L L N O O Q R S S S T T U S Y 1 17 17 A E E E H I L L N O O Q R S S S T T U S Y 1 18 18 A E E E H I L L N O O Q R S S S T T U S Y 1 19 16 A E E E H I L L N O O Q R S S S S T T U Y 1 20 20 A E E E H I L L N O O Q R S S S S T T U Y A E E E H I L L N O O Q R S S S S T T U Y
[]
{ "number": "2.4.9", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Suppose that we wish to avoid wasting one position in a heap-ordered array pq[], putting the largest value in pq[0], its children in pq[1] and pq[2], and so forth, proceeding in level order. Where are the parents and children of pq[k]?
2.1.10 Insertion sort is faster than selection sort for h-sorting because as "h" decreases, the array becomes partially sorted. Insertion sort makes less comparisons in partially sorted arrays than selection sort. Also, when h-sorting, eventually h will have an increment value of 1. Using selection sort with an increment value of 1 would be the same as using the standard selection sort algorithm from the beginning. This would make the steps with the previous increments to be unnecessary work. Thanks to jaeheonshim (https://github.com/jaeheonshim) for adding an extra reason not to use selection sort. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/254
[]
{ "number": "2.4.10", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Describe a way to avoid the j < N test in sink().
2.1.13 - Deck sort I would use selection sort, comparing the cards first by suit, and if they have the same suit, by rank. As we are dealing with physical objects it makes sense to minimize the number of swaps. With selection sort it may be needed to look at more cards than insertion sort (twice as many in the average case), but swaps will be required at most 52 times versus at most 676 times. Thanks to zefrawg (https://github.com/zefrawg) and nedas-dev (https://github.com/nedas-dev) for improving this exercise: https://github.com/reneargento/algorithms-sedgewick-wayne/issues/198
[]
{ "number": "2.4.13", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
What is the minimum number of items that must be exchanged during a remove the maximum operation in a heap of size N with no duplicate keys? Give a heap of size 15 for which the minimum is achieved. Answer the same questions for two and three successive remove the maximum operations.
2.1.14 - Dequeue sort I would use a variation of bubble sort. 1- I would compare both top cards and, if the top card were bigger than the second card, I would swap them. 2- I would mark the top card, so I could know it was the first card (in this iteration) sent to the bottom of the deck. 3- I would send the top card to the bottom of the deck. 4- I would repeat steps 1 and 3 until the marked card becomes the second card in the deck 5- I would send the top card to the bottom of the deck (and the marked card is now at the top, signaling that a iteration is over) 6- If there were no swaps in this iteration, the deck is sorted. Otherwise, repeat steps 1 to 6. Nice explanation here: http://stackoverflow.com/questions/38061140/sort-a-deck-of-cards
[]
{ "number": "2.4.14", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Design a linear-time certification algorithm to check whether an array pq[] is a min-oriented heap.
2.1.15 - Expensive exchange The clerk should use selection sort. Since the cost of compares is low, the N^2 complexity won't be a problem. And it guarantees a cost of at most N exchanges.
[]
{ "number": "2.4.15", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Prove that sink-based heap construction uses fewer than 2N compares and fewer than N exchanges.
2.1.20 - Shellsort best case Just like insertion sort, the best case for shellsort is when the array is already ordered. This causes every element to be compared only once in each iteration. The time complexity in this case is O(n log n). Good explanation: https://www.toptal.com/developers/sorting-algorithms/shell-sort http://www.codingeek.com/algorithms/shell-sort-algorithm-explanation-implementation-and-complexity/
[]
{ "number": "2.4.20", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Exercise" }
Multiway heaps. Considering the cost of compares only, and assuming that it takes t compares to find the largest of t items, find the value of t that minimizes the coefficient of N lg N in the compare count when a t-ary heap is used in heapsort. First, assume a straightforward generalization of sink(); then, assume that Floyd’s method can save one compare in the inner loop.
2.1.23 - Deck sort They separate all cards by suit, making the swaps in order to keep cards in the same suit next to each other (dividing the row in 4 areas). Then, in each suit, they sort the cards by rank using selection sort. In the second step (once suits are sorted), some of them move the unsorted smaller ranks near to the front while searching for the next smallest rank.
[]
{ "number": "2.4.23", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Priority queue with explicit links. Implement a priority queue using a heap-ordered binary tree, but use a triply linked structure instead of an array. You will need three links per node: two to traverse down the tree and one to traverse up the tree. Your implementation should guarantee logarithmic running time per operation, even if no maximum priority-queue size is known ahead of time.
2.1.24 - Insertion sort with sentinel For 100000 random doubles Insertion Sort default is 1.1 times faster than Insertion Sort with a sentinel Insertion Sort default is slightly faster than Insertion Sort with a sentinel.
[]
{ "number": "2.4.24", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Computational number theory. Write a program CubeSum.java that prints out all integers of the form a^3 + b^3 where a and b are integers between 0 and N in sorted order, without using excessive space. That is, instead of computing an array of the N^2 sums and sorting them, build a minimum-oriented priority queue, initially containing (0^3, 0, 0), (1^3, 1, 0), (2^3, 2, 0), . . . , (N^3, N, 0). Then, while the priority queue is nonempty, remove the smallest item(i^3 + j^3, i, j), print it, and then, if j < N, insert the item (i^3 + (j+1)^3, i, j+1). Use this program to find all distinct integers a, b, c, and d between 0 and 10^6 such that a^3 + b^3 = c^3 + d^3.
2.1.25 - Insertion sort without exchanges For 100000 random doubles Insertion Sort default is 0.7 times faster than Insertion Sort without exchanges Insertion Sort without exchanges is faster than Insertion Sort default.
[]
{ "number": "2.4.25", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Heap without exchanges. Because the exch() primitive is used in the sink() and swim() operations, the items are loaded and stored twice as often as necessary. Give more efficient implementations that avoid this inefficiency, a la insertion sort (see Exercise 2.1.25).
2.1.26 - Primitive types For 100000 random doubles Insertion Sort with primitives is 2.0 times faster than Insertion Sort with objects Insertion Sort with primitives is faster than Insertion Sor with objects.
[]
{ "number": "2.4.26", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Find the minimum. Add a min() method to MaxPQ. Your implementation should use constant time and constant extra space.
2.1.27 - Shellsort is subquadratic For a total of 11 experiments with 128, 256, ..., 262,144 values Shell Sort is 343.0 times faster than Selection Sort Shell Sort is 338.8 times faster than Insertion Sort
[]
{ "number": "2.4.27", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Selection filter. Write a TopM client that reads points (x, y, z) from standard input, takes a value M from the command line, and prints the M points that are closest to the origin in Euclidean distance. Estimate the running time of your client for N = 10^8 and M = 10^4.
2.1.28 - Equal keys From the book we know that: Proposition A. Selection sort uses ~(N^2 / 2) compares and N exchanges to sort an array of length N. Proposition B. Insertion sort uses ~(N^2 / 4) compares and ~(N^2 / 4) exchanges to sort a randomly ordered array of length N with distinct keys, on the average. The worst case is ~(N^2 / 2) compares and ~(N^2 / 2) exchanges and the best case is N - 1 compares and 0 exchanges. Thus the running time of selection sort is bound by O(N^2 / 2) and insertion sort averages on O(N^2 / 2) (this is because ~(N^2 / 4) compares + ~(N^2 / 4) exchanges = ~(N^2 / 4)). For arrays with 2 key values (assuming value 1 and value 2) of equal probability: * Selection sort: it still needs to scan N - K elements to sort the Kth element. So its asymptotic running time remains the same O(N^2 / 2). * Insertion sort: when sorting the (K + 1)th element, there are already K / 2 ones and K / 2 twos sorted. Thus the (random variable) expected value of the running time for sorting the (K + 1)th element is: 0.5 * 1 + 0.5 * K Thus the total running time for K = 0, 1, 2, ..., N is: 0.5 * N + ((N + 1) * N / 2) * 0.5 = 3N/4 + (N ^ 2) / 4 ~= O(N^2 / 4) Comparing O(N^2 / 4) against O(N^2 / 2) we can make the hypothesis that the running time of insertion sort with 2 key values is half of that for random values. Hypothesis: 1- Selection sort will take the same time for both arrays with different values and arrays with only 2 possible values. 2- Insertion sort will take half the same for arrays with only 2 possible values than for arrays with different values. Validation: Selection Sort For an array of size 128: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 256: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 512: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 1024: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 2048: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 4096: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 8192: Time for an array with any values: 0.1 Time for an array with 2 values: 0.1 For an array of size 16384: Time for an array with any values: 0.2 Time for an array with 2 values: 0.2 For an array of size 32768: Time for an array with any values: 1.0 Time for an array with 2 values: 0.7 For an array of size 65536: Time for an array with any values: 4.4 Time for an array with 2 values: 3.0 For an array of size 131072: Time for an array with any values: 20.9 Time for an array with 2 values: 11.7 Insertion Sort For an array of size 128: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 256: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 512: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 1024: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 2048: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 4096: Time for an array with any values: 0.0 Time for an array with 2 values: 0.0 For an array of size 8192: Time for an array with any values: 0.1 Time for an array with 2 values: 0.0 For an array of size 16384: Time for an array with any values: 0.2 Time for an array with 2 values: 0.1 For an array of size 32768: Time for an array with any values: 1.0 Time for an array with 2 values: 0.4 For an array of size 65536: Time for an array with any values: 4.3 Time for an array with 2 values: 1.5 For an array of size 131072: Time for an array with any values: 18.9 Time for an array with 2 values: 6.3 -------- Result: 1- Selection sort is faster for arrays with only 2 possible values than for arrays with different values. 2- Insertion sort is a lot faster (about a third of the time) for arrays with only 2 possible values than for arrays with different values. Also, insertion sort is faster than selection sort for sorting arrays with only 2 possible values. Thanks to faame (https://github.com/faame) for improving the analysis. https://github.com/reneargento/algorithms-sedgewick-wayne/issues/216
[]
{ "number": "2.4.28", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Min/max priority queue. Design a data type that supports the following operations: insert, delete the maximum, and delete the minimum (all in logarithmic time); and find the maximum and find the minimum (both in constant time). Hint: Use two heaps.
2.1.29 - Shellsort increments Sedgewicks's sequence is faster than the 3N + 1 sequence. Experiments: For an array of size 32768: 3N + 1 sequence: 0.0 Sedgewicks's sequence: 0.0 For an array of size 65536: 3N + 1 sequence: 0.0 Sedgewicks's sequence: 0.0 For an array of size 131072: 3N + 1 sequence: 0.1 Sedgewicks's sequence: 0.0 For an array of size 262144: 3N + 1 sequence: 0.1 Sedgewicks's sequence: 0.1 For an array of size 524288: 3N + 1 sequence: 0.3 Sedgewicks's sequence: 0.3 For an array of size 1048576: 3N + 1 sequence: 1.0 Sedgewicks's sequence: 0.7 For an array of size 2097152: 3N + 1 sequence: 2.5 Sedgewicks's sequence: 1.8 For an array of size 4194304: 3N + 1 sequence: 7.2 Sedgewicks's sequence: 4.5 For an array of size 8388608: 3N + 1 sequence: 18.4 Sedgewicks's sequence: 11.2 For an array of size 16777216: 3N + 1 sequence: 46.0 Sedgewicks's sequence: 26.6
[]
{ "number": "2.4.29", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Dynamic median-finding. Design a data type that supports insert in logarithmic time, find the median in constant time, and delete the median in logarithmic time. Hint: Use a min-heap and a max-heap.
2.1.30 - Geometric increments Best 1 tValue: 2.10 Best 1 sequence: 1 2 4 9 19 40 85 180 378 794 1667 3502 7355 15447 32439 68122 143056 300419 630880 Best 2 tValue: 2.40 Best 2 sequence: 1 2 5 13 33 79 191 458 1100 2641 6340 15216 36520 87648 210357 504857 Best 3 tValue: 2.20 Best 3 sequence: 1 2 4 10 23 51 113 249 548 1207 2655 5843 12855 28281 62218 136880 301136 662499
[]
{ "number": "2.4.30", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Fast insert. Develop a compare-based implementation of the MinPQ API such that insert uses ~log log N compares and delete the minimum uses ~2 log N compares. Hint: Use binary search on parent pointers to find the ancestor in swim().
2.1.31 - Doubling test Selection sort and insertion sort are quadratic for random inputs. N Predicted Time Time Ratio Selection sort 2000 0.0 0.0 0.6 4000 0.0 0.0 2.3 8000 0.1 0.1 3.8 16000 0.2 0.3 4.7 32000 1.0 1.2 4.9 64000 4.9 5.1 4.1 128000 20.3 20.6 4.1 256000 82.2 124.6 6.1 Insertion sort 2000 0.0 0.0 2.4 4000 0.1 0.0 0.9 8000 0.1 0.1 4.1 16000 0.2 0.3 4.3 32000 1.1 1.1 4.3 64000 4.6 4.9 4.3 128000 19.6 22.8 4.7 256000 91.3 125.7 5.5 Hypothesis: Shell sort is sub quadratic for random inputs. It's worse case (with the 3N + 1 increment sequence) is N ^ 3/2. Shell sort 2000 0.0 0.0 0.5 4000 0.0 0.0 1.0 8000 0.0 0.0 2.0 16000 0.0 0.0 1.0 32000 0.0 0.0 2.0 64000 0.0 0.0 2.5 128000 0.1 0.1 2.5 256000 0.1 0.1 2.4 512000 0.3 0.3 2.8 1024000 1.0 1.0 2.8 2048000 2.7 2.4 2.5 4096000 6.8 6.4 2.7 8192000 18.1 18.7 2.9 16384000 52.7 44.4 2.4 32768000 125.3 123.6 2.8 65536000 348.7 304.6 2.5 Hypothesis validated.
[]
{ "number": "2.4.31", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Index priority-queue implementation (additional operations). Add minIndex(), change(), and delete() to your implementation of Exercise 2.4.33.
2.1.34 - Corner cases As expected: For ordered arrays insertion sort runs in linear time, and is faster than selection sort. For reverse ordered arrays insertion sort runs much slower because it has to swap all values until the beginning of the array. For same keys, just like with ordered arrays, insertion sort runs in linear time. For 2 distinct values, insertion sort has to do less swaps, running faster than selection sort. For size 1 and size 0 arrays all sorts run fast. Shell sort is faster than selection sort and insertion sort in all cases. Sort Type Array Length Time ORDERED ARRAY SELECTION 10000 0.05 INSERTION 10000 0.00 SHELL 10000 0.00 SELECTION 20000 0.18 INSERTION 20000 0.00 SHELL 20000 0.00 SELECTION 40000 0.81 INSERTION 40000 0.00 SHELL 40000 0.00 SELECTION 80000 2.99 INSERTION 80000 0.00 SHELL 80000 0.00 SELECTION 160000 12.04 INSERTION 160000 0.00 SHELL 160000 0.00 REVERSE ORDERED ARRAY SELECTION 10000 0.10 INSERTION 10000 0.25 SHELL 10000 0.00 SELECTION 20000 0.33 INSERTION 20000 0.62 SHELL 20000 0.00 SELECTION 40000 1.15 INSERTION 40000 2.45 SHELL 40000 0.00 SELECTION 80000 4.85 INSERTION 80000 10.65 SHELL 80000 0.01 SELECTION 160000 17.66 INSERTION 160000 42.76 SHELL 160000 0.01 SAME KEYS ARRAY SELECTION 10000 0.09 INSERTION 10000 0.00 SHELL 10000 0.00 SELECTION 20000 0.29 INSERTION 20000 0.00 SHELL 20000 0.00 SELECTION 40000 1.16 INSERTION 40000 0.00 SHELL 40000 0.00 SELECTION 80000 4.64 INSERTION 80000 0.00 SHELL 80000 0.00 SELECTION 160000 17.01 INSERTION 160000 0.00 SHELL 160000 0.00 TWO VALUES ARRAY SELECTION 10000 0.07 INSERTION 10000 0.04 SHELL 10000 0.00 SELECTION 20000 0.29 INSERTION 20000 0.17 SHELL 20000 0.00 SELECTION 40000 1.07 INSERTION 40000 0.65 SHELL 40000 0.00 SELECTION 80000 4.68 INSERTION 80000 2.66 SHELL 80000 0.00 SELECTION 160000 18.59 INSERTION 160000 10.46 SHELL 160000 0.00 SIZE 1 ARRAY SELECTION 1 0.00 INSERTION 1 0.00 SHELL 1 0.00 SIZE 0 ARRAY SELECTION 0 0.00 INSERTION 0 0.00 SHELL 0 0.00
[]
{ "number": "2.4.34", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Sampling from a discrete probability distribution. Write a class Sample with a constructor that takes an array p[] of double values as argument and supports the following two operations: random()—return an index i with probability p[i]/T (where T is the sum of the numbers in p[])—and change(i, v)—change the value of p[i] to v. Hint: Use a complete binary tree where each node has implied weight p[i]. Store in each node the cumulative weight of all the nodes in its subtree. To generate a random index, pick a random number between 0 and T and use the cumulative weights to determine which branch of the subtree to explore. When updating p[i], change all of the weights of the nodes on the path from the root to i. Avoid explicit pointers, as we do for heaps.
2.1.35 - Nonuniform distributions Hypotheses: Selection sort will run with the same speed in all distributions. Insertion sort will run faster than selection sort for Gaussian and Discrete distributions, but with a similar speed for Poisson and Geometric distributions. Shell sort will run faster than selection sort and insertion sort for all distributions. After testing the hypotheses: Selection sort ran faster for Gaussian and Discrete distributions than for Poisson and Geometric distributions. Insertion sort ran faster than selection sort for all distributions. Shell sort ran faster than selection sort and insertion sort for all distributions. Sort Type Array Length Time GAUSSIAN DISTRIBUTION SELECTION 10000 0.13 INSERTION 10000 0.10 SHELL 10000 0.01 SELECTION 20000 0.49 INSERTION 20000 0.38 SHELL 20000 0.01 SELECTION 40000 2.04 INSERTION 40000 1.86 SHELL 40000 0.01 SELECTION 80000 8.53 INSERTION 80000 8.37 SHELL 80000 0.02 SELECTION 160000 39.54 INSERTION 160000 35.89 SHELL 160000 0.06 POISSON DISTRIBUTION SELECTION 10000 0.28 INSERTION 10000 0.16 SHELL 10000 0.00 SELECTION 20000 1.24 INSERTION 20000 0.59 SHELL 20000 0.00 SELECTION 40000 4.67 INSERTION 40000 2.44 SHELL 40000 0.00 SELECTION 80000 19.11 INSERTION 80000 9.71 SHELL 80000 0.01 SELECTION 160000 67.54 INSERTION 160000 39.29 SHELL 160000 0.01 GEOMETRIC DISTRIBUTION SELECTION 10000 0.26 INSERTION 10000 0.16 SHELL 10000 0.00 SELECTION 20000 1.06 INSERTION 20000 0.63 SHELL 20000 0.00 SELECTION 40000 4.33 INSERTION 40000 1.41 SHELL 40000 0.00 SELECTION 80000 15.51 INSERTION 80000 5.32 SHELL 80000 0.01 SELECTION 160000 69.14 INSERTION 160000 20.67 SHELL 160000 0.02 DISCRETE DISTRIBUTION SELECTION 10000 0.09 INSERTION 10000 0.06 SHELL 10000 0.00 SELECTION 20000 0.35 INSERTION 20000 0.22 SHELL 20000 0.00 SELECTION 40000 1.37 INSERTION 40000 0.88 SHELL 40000 0.00 SELECTION 80000 5.58 INSERTION 80000 3.47 SHELL 80000 0.00 SELECTION 160000 22.31 INSERTION 160000 14.13 SHELL 160000 0.00
[]
{ "number": "2.4.35", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.4, "section_title": "Priority Queues", "type": "Creative Problem" }
Consider the following implementation of the compareTo() method for String. How does the third line help with efficiency? public int compareTo(String that) { if (this == that) return 0; // this line int n = Math.min(this.length(), that.length()); for (int i = 0; i < n; i++) { if (this.charAt(i) < that.charAt(i)) return -1; else if (this.charAt(i) > that.charAt(i)) return +1; } return this.length() - that.length(); }
2.1.1 a[] i min 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 1 E A S Y Q U E S T I O N 1 1 A E S Y Q U E S T I O N 2 6 A E S Y Q U E S T I O N 3 9 A E E Y Q U S S T I O N 4 11 A E E I Q U S S T Y O N 5 10 A E E I N U S S T Y O Q 6 11 A E E I N O S S T Y U Q 7 7 A E E I N O Q S T Y U S 8 11 A E E I N O Q S T Y U S 9 11 A E E I N O Q S S Y U T 10 10 A E E I N O Q S S T U Y 11 11 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.5.1", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.5, "section_title": "Applications", "type": "Exercise" }
Write a program that reads a list of words from standard input and prints all two-word compound words in the list. For example, if after, thought, and afterthought are in the list, then afterthought is a compound word.
2.1.2 The maximum number of exchanges involving any particular item during selection sort is N - 1. This happens when the first item has the highest value in the unsorted array and the other values are sorted. For example, in the array 4 1 2 3, the element 4 will be swapped N - 1 times. The average number of exchanges involving an item is exactly 2, because there are exactly N exchanges and N items (and each exchange involves two items). Reference: https://algs4.cs.princeton.edu/21elementary/
[]
{ "number": "2.5.2", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.5, "section_title": "Applications", "type": "Exercise" }
Criticize the following implementation of a class intended to represent account balances. Why is compareTo() a flawed implementation of the Comparable interface? public class Balance implements Comparable<Balance> { ... private double amount; public int compareTo(Balance that) { if (this.amount < that.amount - 0.005) return -1; if (this.amount > that.amount + 0.005) return +1; return 0; } ... } Describe a way to fix this problem.
2.1.3 Array: H G F E D C B A Thanks to QiotoF (https://github.com/QiotoF) for mentioning a better array solution for this exercise https://github.com/reneargento/algorithms-sedgewick-wayne/issues/76
[]
{ "number": "2.5.3", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.5, "section_title": "Applications", "type": "Exercise" }
Implement a method String[] dedup(String[] a) that returns the objects in a[] in sorted order, with duplicates removed.
2.1.4 a[] i j 0 1 2 3 4 5 6 7 8 9 10 11 E A S Y Q U E S T I O N 0 0 E A S Y Q U E S T I O N 1 0 A E S Y Q U E S T I O N 2 2 A E S Y Q U E S T I O N 3 3 A E S Y Q U E S T I O N 4 2 A E Q S Y U E S T I O N 5 4 A E Q S U Y E S T I O N 6 2 A E E Q S U Y S T I O N 7 5 A E E Q S S U Y T I O N 8 6 A E E Q S S T U Y I O N 9 3 A E E I Q S S T U Y O N 10 4 A E E I O Q S S T U Y N 11 4 A E E I N O Q S S T U Y A E E I N O Q S S T U Y
[]
{ "number": "2.5.4", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.5, "section_title": "Applications", "type": "Exercise" }
Explain why selection sort is not stable.
2.1.5 Condition 1: j > 0 -> When the array is reverse ordered Z Q K D C B A Condition 2: less(a[j], a[j - 1]) -> When the array is ordered A B C D E F G
[]
{ "number": "2.5.5", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.5, "section_title": "Applications", "type": "Exercise" }
Implement a recursive version of select().
2.1.6 Insertion sort because it will only make one comparison with the previous element (per element) and won't exchange any elements, running in linear time. Selection sort will exchange each element with itself and will run in quadratic time. Thanks to glucu (https://github.com/glucu) for correcting the number of exchanges done in selection sort.
[]
{ "number": "2.5.6", "code_execution": false, "url": null, "params": null, "dependencies": null, "chapter": 2, "chapter_title": "Sorting", "section": 2.5, "section_title": "Applications", "type": "Exercise" }