4.9: Exercises
- Page ID
- 21112
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
The code for this chapter is in the ch13 directory of ThinkJavaCode. See [Section 0.4] for instructions on how to download the repository. Before you start the exercises, we recommend that you compile and run the examples.
Exercise \(\PageIndex{1}\)
You can learn more about the sorting algorithms in this chapter, and others, at www.sorting-algorithms.com/. This site includes explanations of the algorithms, animations that show how they work, and analysis of their efficiency.
Exercise \(\PageIndex{2}\)
The goal of this exercise is to implement the shuffling algorithm from this chapter.
- In the repository for this book, you should find a file called Deck.java that contains the code in this chapter. Check that you can compile it in your environment.
- Add a
Deckmethod calledrandomIntthat takes two integers,lowandhigh, and returns a random integer betweenlowandhigh, including both. You can use thenextIntprovided byjava.util.Random, which we saw in Section 8.7. But you should avoid creating aRandomobject every timerandomIntis invoked. - Write a method called
swapCardsthat takes two indexes and swaps the cards at the given locations. - Write a method called
shufflethat uses the algorithm in Section 13.2.
Exercise \(\PageIndex{3}\)
The goal of this exercise is to implement the sorting algorithms from this chapter. Use the Deck.java file from the previous exercise (or create a new one from scratch).
- Write a method called
indexLowestthat uses thecompareCardmethod to find the lowest card in a given range of the deck (fromlowIndextohighIndex, including both). - Write a method called
selectionSortthat implements the selection sort algorithm in Section 13.3. - Using the pseudocode in Section 13.4, write the method called
merge. The best way to test it is to build and shuffle a deck. Then usesubdeckto form two small subdecks, and use selection sort to sort them. Then you can pass the two halves tomergeto see if it works. - Write the simple version of
mergeSort, the one that divides the deck in half, usesselectionSortto sort the two halves, and usesmergeto create a new, sorted deck. - Write a recursive version of
mergeSort. Remember thatselectionSortis a modifier andmergeSortis a pure method, which means that they get invoked differently:deck.selectionSort(); // modifies an existing deck deck = deck.mergeSort(); // replaces old deck with new
Exercise \(\PageIndex{4}\)
The goal of this exercise is to practice top-down programming by implementing “insertion sort”. Read about insertion sort at www.sorting-algorithms.com/insertion-sort. Write a method named insertionSort that implements this algorithm.
Exercise \(\PageIndex{5}\)
Write a toString method for the Deck class. It should return a single string that represents the cards in the deck. When it’s printed, this string should display the same results as the print method in Section 13.1.
- Hint
-
You can use the
+operator to concatenate strings, but it is not very efficient. Consider usingjava.lang.StringBuilder; you can find the documentation by doing a web search for “Java StringBuilder”.


