• Home
  • Textbooks
  • Computer science with Mathematica: theory and practice for science, mathematics, and engineering
  • Algorithms for Searching and Sorting

Computer science with Mathematica: theory and practice for science, mathematics, and engineering

Roman Maeder

Chapter 6

Algorithms for Searching and Sorting - all with Video Answers

Educators


Chapter Questions

Problem 1

Write a function primePi $[x]$ that finds the number $\pi(x)$ of primes $\leq x$ using binary search (see Section 6.1.1). (This function already exists in Mathematica under the name PrimePi[].)

Check back soon!

Problem 2

Binary trees can be used to implement a sorting method.
1. Extend the package BinaryTree.m from Section 6.3 by a function InOrder[tree $]$. The function should return all records of the tree in a list in the following order: first, the elements of the left subtree (recursively); then, the element at the root; finally, the elements of the right subtree. Because of the ordering of the binary tree, the resulting list will be sorted.
2. Use InOrder[tree] to implement a sorting function TreeSort [list] that sorts the list by first inserting all its elements into a binary tree and then converting the tree back into a list.

Check back soon!
03:08

Problem 3

Write a function Merge $\left[l_1, l_2\right]$ that merges the elements of the two lists $l_1$ and $l_2$ into a single list. The elements of $l_1$ and $l_2$ are assumed to be numbers sorted in ascending order (you do not have to check this property). The result should also be sorted in ascending order. The result should be obtained without sorting the list anew.
Write a single procedural definition that works on the input lists in a loop. You can use any list operations, such as First $[l]$, Rest $[l]$, Prepend $[l$, elem $]$, Append $[l$, elem $]$, or Join $\left[l_1, l_2\right]$. Use Module [] to declare local variables.

The result contains all elements of the two input lists in ascending order.
$$
\begin{aligned}
& \operatorname{In}[1]:=\operatorname{Merge}[\{1,5,7\},\{2,3,8\}] \\
& \operatorname{Out}[1]=\operatorname{Merge}[\{1,5,7\},\{2,3,8\}]
\end{aligned}
$$

Your function should also treat such special inputs correctly.
$$
\begin{aligned}
& \operatorname{In}[2]:=\operatorname{Merge}[\{\},\{2,3\}] \\
& \operatorname{Out}[2]=\operatorname{Merge}[\{\},\{2,3\}]
\end{aligned}
$$

If an element appears more than once, it must appear in the result the same number of times.
$$
\begin{aligned}
& \operatorname{In}[3]:=\operatorname{Merge}[\{1,1,2,3\},\{2,3\}] \\
& \operatorname{Dut}[3]=\operatorname{Merge}[\{1,1,2,3\},\{2,3\}]
\end{aligned}
$$

Morgan Cheatham
Morgan Cheatham
Numerade Educator

Problem 4

We restrict the inputs of our sorting functions to permutations of the numbers $1 \ldots n$. A simple way to visualize the amount of "sortedness" in such a list of numbers is a picture obtained with ListPlot.
(Table Cant Copy)
Write a program that allows you to draw such pictures after each exchange step in the sorting procedures (insertion sort, selection sort, and quicksort). If you wish, you can animate these sequences of pictures to see how the algorithm runs.

Check back soon!

Problem 5

Develop an abstract version of quicksort (see Section 6.2.3). As in the abstract searching procedure from Section 6.1.3, you should assume that arbitrary records are to be sorted. The records have a key field that is used for comparisons.

Check back soon!