Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Kotlin The Interactive Edition

Section 5.14 Searching and Sorting: Exercises

Exercises Exercises

5.

Devise alternative strategies for choosing the pivot value in a quicksort. For example, pick the middle item. Reimplement the algorithm and then execute it on random data sets. Under what criteria does your new strategy perform better or worse than the strategy from this chapter?

6.

Set up a random experiment to test the difference between a sequential search and a binary search on a list of integers.

7.

Use the binary search functions given in the text (recursive and iterative). Generate a random, ordered list of integers and do a benchmark analysis for each one. What are your results? Can you explain them?

8.

Implement the binary search using recursion without the slice operator. Recall that you will need to pass the list along with the starting and ending index values for the sublist. Generate a random, ordered list of integers and do a benchmark analysis.

10.

Using a random number generator, create a list of 500 integers. Perform a benchmark analysis using some of the sorting algorithms from this chapter. What is the difference in execution speed?

11.

A bubble sort can be modified to β€œbubble” in both directions. The first pass moves β€œup” the list, and the second pass moves β€œdown.” This alternating pattern continues until no more passes are necessary. Implement this variation and describe under what circumstances it might be appropriate.

12.

Perform a benchmark analysis for a shell sort, using different increment sets on the same list.

13.

Implement the mergeSort function without using the slice operator.

14.

One way to improve the quicksort is to use an insertion sort on lists that have a short length (call it the β€œpartition limit”). Why does this make sense? Reimplement the quicksort and use it to sort a random list of integers. Perform an analysis using different list sizes for the partition limit.

15.

Implement the median of three method for selecting a pivot value as a modification to quickSort. Run an experiment to compare the two techniques.
You have attempted of activities on this page.