Sorting

In this chapter we discuss the problem of sorting an array of elements. To simplify matters, we will assume in our examples that the array contains only integers, although, obviously, more complicated structures are possible. For most of this chapter, we will also assume that the entire sort can be done in main memory, so that the number of elements is relatively small (less than a million). Sorts that cannot be performed in main memory and must be done on disk or tape are also quite important. This type of sorting, known as external sorting, will be discussed at the end of the chapter.

  • Our investigation of internal sorting will show that There are several easy algorithms to sort in O(n2), such as insertion sort.

  • There is an algorithm, Shellsort, that is very simple to code, runs in o (n2), and is efficient in practice.

  • There are slightly more complicated O(n log n) sorting algorithms.

  • Any general-purpose sorting algorithm requires (n log n) comparisons.

The rest of this chapter will describe and analyze the various sorting algorithms. These algorithms contain interesting and important ideas for code optimization as well as algorithm design. Sorting is also an example where the analysis can be precisely performed. Be forewarned that where appropriate, we will do as much analysis as possible.


Classes
Quiz
Videos
References
Books