Book contents
- Frontmatter
- Contents
- Preface
- Chapter 1 An Introduction to Collections, Generics, and the Timing Class
- Chapter 2 Arrays and ArrayLists
- Chapter 3 Basic Sorting Algorithms
- Chapter 4 Basic Searching Algorithms
- Chapter 5 Stacks and Queues
- Chapter 6 The BitArray Class
- Chapter 7 Strings, the String Class, and the StringBuilder Class
- Chapter 8 Pattern Matching and Text Processing
- Chapter 9 Building Dictionaries: The DictionaryBase Class and the SortedList Class
- Chapter 10 Hashing and the Hashtable Class
- Chapter 11 Linked Lists
- Chapter 12 Binary Trees and Binary Search Trees
- Chapter 13 Sets
- Chapter 14 Advanced Sorting Algorithms
- Chapter 15 Advanced Data Structures and Algorithms for Searching
- Chapter 16 Graphs and Graph Algorithms
- Chapter 17 Advanced Algorithms
- References
- Index
Chapter 4 - Basic Searching Algorithms
Published online by Cambridge University Press: 05 June 2012
- Frontmatter
- Contents
- Preface
- Chapter 1 An Introduction to Collections, Generics, and the Timing Class
- Chapter 2 Arrays and ArrayLists
- Chapter 3 Basic Sorting Algorithms
- Chapter 4 Basic Searching Algorithms
- Chapter 5 Stacks and Queues
- Chapter 6 The BitArray Class
- Chapter 7 Strings, the String Class, and the StringBuilder Class
- Chapter 8 Pattern Matching and Text Processing
- Chapter 9 Building Dictionaries: The DictionaryBase Class and the SortedList Class
- Chapter 10 Hashing and the Hashtable Class
- Chapter 11 Linked Lists
- Chapter 12 Binary Trees and Binary Search Trees
- Chapter 13 Sets
- Chapter 14 Advanced Sorting Algorithms
- Chapter 15 Advanced Data Structures and Algorithms for Searching
- Chapter 16 Graphs and Graph Algorithms
- Chapter 17 Advanced Algorithms
- References
- Index
Summary
Searching for data is a fundamental computer programming task and one that has been studied for many years. This chapter looks at just one aspect of the search problem—searching for a given value in a list (array).
There are two fundamental ways to search for data in a list: the sequential search and the binary search. Sequential search is used when the items in the list are in random order; binary search is used when the items are sorted in the list.
SEQUENTIAL SEARCHING
The most obvious type of search is to begin at the beginning of a set of records and move through each record until you find the record you are looking for or you come to the end of the records. This is called a sequential search.
A sequential search (also called a linear search) is very easy to implement. Start at the beginning of the array and compare each accessed array element to the value you're searching for. If you find a match, the search is over. If you get to the end of the array without generating a match, then the value is not in the array.
- Type
- Chapter
- Information
- Data Structures and Algorithms Using C# , pp. 55 - 67Publisher: Cambridge University PressPrint publication year: 2007