####
UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104

# 14. Quicksort & Merge Sort

#### Tuesday October 20, 1998

[Previous Lecture]
[Next Lecture]

**Assigned Reading:**
online notes on asymptotic analysis

**Handouts (available on-line):**

**Programs from this lecture.**

**Topics Covered:**

- Insertion Sort can be somewhat
faster than Selection Sort. Our test
program and sample run show
that it took only 4.7 hours to sort 320,000 floating point numbers.

- A graph of the running times of our
experiments with Selection Sort and Insertion Sort show that the running
times are proportional to n
^{2}, where n is the number of items. By
extending these curves we estimate that the
running times to sort 640,000 floating point numbers would take about 27.8
and 18.8 hours for Selection Sort and Insertion Sort respectively. Sorting
1.28 million numbers would take approximately 4.6 and 3.1 days using these
algorithms.

- Merge Sort uses a
divide-and-conquer strategy and solves the sorting problem by recursion.
Our test program and sample runs show that Merge Sort takes only 36.4
seconds to sort 1.28 million floating point numbers.

- Merge Sort can be implemented without using recursion. However, the iterative program is much more complicated
than the recursive version. Somewhat surprisingly, we also discover that
this iterative implementation of the Merge Sort is a bit
**slower** than
the recursive version (sample runs). Hence,
using recursion results in cleaner code and sometimes faster code as well.

- Quicksort is another
divide-and-conquer algorithm for the sorting problem. One difference
between Quicksort and Merge Sort is that the
`partition()` function
is used to divide the array into two parts such that all the numbers in the
first half are smaller than the numbers in the second half. This
eliminates the need for the `merge()` function used in merge sort.
Testing the Quicksort function shows that the
running times are a bit faster than Merge Sort (sample runs).

- Graphing the running times of Merge Sort
versus Quicksort reveals that the running times are approximately
proportional to n log n, where n is the number of items to be sorted.

[Previous Lecture]
[Next Lecture]

Last Modified:
29 Oct 1998 17:14:03 EST
by
Richard Chang

Back up
to Fall 1998 CMSC 202 Section Homepage