Chapter 12 - Sorting and Searching Algorithms
Chapter 12 - Sorting and Searching Algorithms
1.1 Objectives
In this section, we examine fundamental sorting and searching algorithms and how they
can be implemented.
After completing this lesson, you should be able to:
Explain the algorithms used in insertion sort, bubble sort, linear search and binary
search
Give your own implementation of these algorithms
1.2 Introduction
The task of sorting and searching are so fundamental and are also frequently used. In
fact, most complex algorithms are based on basic sorting and searching algorithms For
these reasons, examining existing algorithms would be very beneficial.
1.3 Sorting
Sorting is the task of arranging elements in a particular order and it is implemented in a
variety of applications. Consider a banking application, which displays the list of active
client accounts, for instance. Users of this system most probably prefer having the list in
an ascending order for convenience. Here, we take a look at two fundamental sorting
algorithms: bubble sort and insertion sort.
1.3.1.2 An Example
Given 1st Pass 2nd Pass 3rd Pass 4th Pass
The insertion sort algorithm basically divides the data elements to be sorted into two
groups, the unsorted (analogous to the 1st table) and the sorted (analogous to the 2 nd
table) sections. The first available element is selected from the unsorted section of the
array and it is properly positioned in the sorted section of the array. This step is repeated
until there are no more elements left in the unsorted part of the array.
1.3.2.2 An Example
Given 1st Pass 2nd Pass 3rd Pass 4th Pass
At the end of this module, you will be asked to give your own Java implementation of the
different sorting algorithms discussed in this section.
1.4 Searching
Searching on the other hand is the task of looking for a particular item in a collection.
The importance of such task is most evident when we use search engines to look for a
particular topic (if available) on the Web. For this, we take a look at two algorithms:
linear search and binary search.