Close
Register
Close Window

Data Structures and Algorithms

Chapter 2 Arrays: Searching and Sorting

Show Source |    | About   «  1.6. Comparables and Comparators: An Example   ::   Contents   ::   2.2. Searching in an Array  »

2.1. Chapter Introduction: Arrays

Arrays are one of the fundamental data structures in programming [1]. This is because they are natively supported by the computer, and have good performance: reading or writing an element of the array takes very little time. Many important algorithms use arrays.

In this chapter we will study two algorithmic problems and how to solve them efficiently using arrays:

Note that if we search or sort according to a key, it doesn’t have to be explicitly stored in the object, but can instead be calculated on demand. E.g., if we want to sort a list of words case-insensitively, we can use a lower-case transformation when doing the comparisons. This is usually done by a comparator (in Java), or by a key function (in Python).

This chapter concentrates on membership testing and natural sorting, but all the algorithms in this chapter work just as well for lookup and key-based sorting.

Footnotes

1

Note to Python programmers: in Python, arrays are called lists, and are written like this: [1,2,3]. There is one difference between arrays and Python lists: in most programming languages, any given array has a fixed size. However, Python lists can change in size – for example, the append method adds a new element to the list, increasing its size. In this chapter, we will work with arrays that have a fixed size. Python lists are so-called dynamic arrays.

   «  1.6. Comparables and Comparators: An Example   ::   Contents   ::   2.2. Searching in an Array  »

nsf
Close Window