You are on page 1of 4

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 Bubble Sort


Bubble sort is one of the most fundamental sorting algorithms implemented, if not the
most fundamental. The idea behind bubble sort is that we treat items to be sorted as
bubbles, and we let heavier bubbles (i.e. bigger values) "bubble down" (i.e. moved
towards the right side of the array), while lighter bubbles (i.e. smaller values) "bubble
up" (i.e. moved towards the left side of the array).

1.3.1.1 The Algorithm

void bubbleSort(Object array[]) {


int min;
for (int i = 0; i < array.length; i++) {
for (int j = array.length; j > i; j++) {
if (((Comparable) array[j-1]).compareTo(array[j])>0) {
swap(array[j-1], array[j]);
}
}
}
}

1.3.1.2 An Example
Given 1st Pass 2nd Pass 3rd Pass 4th Pass

Maricar Hannah Hannah Hannah Hannah

Vanessa Maricar Margaux Margaux Margaux

Margaux Vanessa Maricar Maricar Maricar

Hannah Margaux Vanessa Rowena Rowena

Rowena Rowena Rowena Vanessa Vanessa

Figure 1: Bubble sort example

1.3.2 Insertion Sort


One of the simplest algorithms developed is the insertion sort. The idea for the algorithm
is quite intuitive and is analagous to a way of arranging a collection of cards. The
following scenario describes how insertion sort works for arranging a set of cards. Say
you want to arrange a standard deck of cards from lowest to highest rank. All cards are
initially laid on a table, call this 1st table, face up in an orderly fashion from left to right,
top to bottom. We have another table, call this 2 nd table, where the arranged cards
would be positioned. Pick the first available card from upper left corner of the 1 st table
and place this card in its proper (i.e., sorted) position on the 2 nd table. Pick the next
available card from the 1st table and compared this with the cards on the 2nd table and
place it in its proper position. The process continues until all cards are placed on the 2 nd
table.

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.1 The Algorithm

void insertionSort(Object array[], int startIdx, int endIdx) {


for (int i = startIdx; i < endIdx; i++) {
int k = i;
for (int j = i + 1; j < endIdx; j++) {
if (((Comparable) array[k]).compareTo(array[j])>0) {
k = j;
}
}
swap(array[i], array[k]);
}
}

1.3.2.2 An Example
Given 1st Pass 2nd Pass 3rd Pass 4th Pass

Mango Mango Apple Apple Apple

Apple Apple Mango Mango Banana

Peach Peach Peach Orange Mango

Orange Orange Orange Peach Orange

Banana Banana Banana Banana Peach

Figure 2: Insertion sort example

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.

1.4.1 Linear Search


Linear search does nothing more than scan an array from the very first index to the last
index for the occurrence of a particular search key. If the object is found within an array,
then it returns the index where the (first) occurrence was found. Otherwise, it returns a
value (usually -1) that signifies that the key was not found within the array. An
implementation of linear search is as follows,

int linearSearch(Object array[], Object key) {


int index = -1;
for (int i = 0; i < array.length; i++) {
if (array[i].equals(key)) {
index = i;
break;
}
}
return index;
}

1.4.2 Binary Search


Binary search is a faster search algorithm than linear search in that it effectively cuts the
search time in half. The idea behind binary search is cutting the search space in half:
initially you have the whole array to which you look into the value in the middle index. If
the value corresponds to our search key, then we return the index. Otherwise, we see if
the key is greater than the middle value, and if so we do a binary search the right
subarray, otherwise we perform binary search on the left subarray. Binary search
though, as you may have guessed, requires that the array be sorted prior to the search
for it to work. An implementation of binary search is as follows,

int binarySearch(Object array[], Object key) {


int index = -1;
int leftIndex = 0;
int rightIndex = array.length;
int midpoint = (leftIndex + rightIndex) / 2;
while (leftIndex < rightIndex) {
if (array[midpoint].equals(key)) {
index = i;
break;
} else if (((Comparable) array[k]).compareTo(key)>0) {
rightIndex = midpoint;
} else {
leftIndex = midpoint;
}
midpoint = (leftIndex + rightIndex) / 2;
}
return index;
}

You might also like