CISC 1115
Introduction to Programming Using Java
Lecture #15
Sorting & Searching


Overview

Searching

To search an array for a particular value

The above is known as linear search (because it moves in a straight line through the array performing the search).

Sorting

Sorting involves rearranging the elements of the array so that the elements are either in ascending or descending order. That is: Note that the last element has no element to the right to compare against (similarly element 0 has no element to its left to be compared to).

Selection Sort

The algorithm (i.e., the approach) used by selection sort corresponds closely to the technique you might use if you were asked to sort a deck of cards, a pile of folders, a list of names:

Selection sort basically does the same thing, but we take into consideration that we're working with an array:

We can thus think of the array as being divided into two parts: What we will do is ave a pair of nested loops: Initially:
+----------------------------------------------------------------------------+
|                                                                            |
|                          unsorted                                          |
|                                                                            |
+----------------------------------------------------------------------------+

After one iteration through the outer loop:

+----------------------------------------------------------------------------+
|        |                                                                   |
|smallest|                 unsorted                                          |
|        |                                                                   |
+----------------------------------------------------------------------------+
Halfway through the outer loop:
+----------------------------------------------------------------------------+
|                               |                                            |
|         sorted                |          unsorted                          |
|                               |                                            |
+----------------------------------------------------------------------------+
At end:
+----------------------------------------------------------------------------+
|                                                                            |
|                            sorted                                          |
|                                                                            |
+----------------------------------------------------------------------------+

The Selection Sort Algorithm (SwappySelectionSort)

A very high-level statement of the approach:

Go through the array from beginning to end
   Find the smallest element in the array from the current position to the end,
	   and place it in that position

In a bit more detail:

Go through the array from beginning to end
   Go through the array from the current position to the end
      If an element is less than the value in the current position swap them
Let's start getting down to code-level
for (currentPosition = 0; currentPosition < size; currentPosition++)
   for (j = currentPosition+1; j < size; j++)
      if (arr[j] < arr[currentPosition] 
         swap(arr[j], arr[currentPosition])
Here it is turned into a method:
void selectionSort(int [] arr, int size) {
	for (currentPosition = 0; currentPosition < size; currentPosition++)
	   for (j = currentPosition+1; j < size; j++)
		 if (arr[j] < arr[currentPosition] 
		    swap(arr, j, currentPosition)
}

An Optimization of Selection Sort (SelectionSort)

Rather than constantly swapping each time we find a new minimum value, we remember the position of the smallest value encountered and it's only once we've gone completely through the array in the inner loop that we swap the smallest value into the current position.
void selectionSort(int [] arr, int size) {
	for (currentPosition = 0; currentPosition < size; currentPosition++)
		int pos = currentPosition;
		for (j = currentPosition+1; j < size; j++)
			if (arr[j] < arr[currentPosition] 
				pos = j;
		swap(arr, pos, currentPosition)
}

One More Optimization (LazySelectionSort)

Each time we've gone completely through the inner loop, before we swap the smallest element to the current position, we make sure the smallest value is not already at the current position.
void selectionSort(int [] arr, int size) {
	for (currentPosition = 0; currentPosition < size; currentPosition++)
		int smallestPosition = currentPosition;
		for (j = currentPosition+1; j < size; j++)
			if (arr[j] < arr[currentPosition] 
				smallestPosition = j;
		if (smallestPosition != currentPosition) swap(arr, pos, currentPosition)
}
and here it is with the inner loop (a single pass through the array) turned into a method:
void selectionSort(int [] arr, int size) {
	for (currentPosition = 0; currentPosition < size; currentPosition++)
		int smallestPosition = findSmallestPosition(arr, size);
		if (smallestPosition != currentPosition) swap(arr, pos, currentPosition)
}

int findSmallestPosition(int [] arr, int size) {
	int smallestPosition = currentPosition;
	for (j = currentPosition+1; j < size; j++)
		if (arr[j] < arr[currentPosition] 
			smallestPosition = j;
	return smallestPosition;
}

A Second Sort - Bubble Sort

In bubble sort:

Some Questions to Answer

Why does this work?

How do we know this will eventually end?

The Bubble Sort Algorithm(SmartBubbleSort)

A very high-level statement of the approach:
Pass through the array until a pass is completed with no swaps performed
		 Compare adjacent elements and swap if they're out of order

In a bit more detail:

   Go through the array from the beginning to end
      If an element at position i is less than the element at position i+1 swap them
If a swap was performed repeat
Let's start getting down to code-level
do {
   bool swapped = false;
   for (i = 0; i < n-1; i++)
      if (arr[i] > arr[i+1] { 
         swap(arr, i, i+1)
         swapped = true;
      }
} while (swapped)
Notice the pattern of how swapped is used: Here is a very simple version of bubble sort for you to remember:
for (int i = 0; i < n; i++)
	for (int j = 0; j < n-1; j++)
		if (arr[j] > arr[j+1])
			swap(arr, j, j+1);
Notes
Here is a minor variation that's a bit more efficient:
for (int last = n; last > 0; last--)
	for (int i = 0; i < last; i++)
		if (arr[i] > arr[i+1])
			swap(arr, i, i+1);
Notes
And here is this version with the swapped optimizationi (i.e. checking for no swaps in a single pass):
boolean swapped = true;
for (int last = n; last > 0 && swapped; last--) {
	swapped = false;
	for (int i = 0; i < last; i++)
		if (arr[i] > arr[i+1]) {
			swap(arr, i, i+1);
			swapped = true;
		}
}
Notes

Why do we have to bother with two sorts

Program 24.1

Write a program that initializes an array of integers prints them, sorts them using bubble sort, and prints the sorted array.

Binary Search — Searching a Sorted Array

We've already seen a linear search of an array, in which we begin at the first element and compare successive elements for equality with the value we are looking for. If the array is sorted, we can do quite better than searching the entire array:

A High-level Version of the Approach

A More Detailed Presentation

As we will be looking at various subarrays (left side right side, etc), we initially view the entire array as a subarray with bounds 0 … arr.length-1 (inclusive). We will call these bounds lo and hi respectively.
  1. if lo and hi have crossed, i.e., lo is greater than hi, i.e., the subarray is empty, report failure
  2. Calculate the location of the middle element of the subarray lohi; call it mid
  3. Compare the middle element with the value to be searched
  4. if equal, return the position
  5. if the value is less than the middle element, repeat step 1, using the subarray lomid-1
  6. otherwise repeat step 1, using the subarray mid+1hi
Here is the actual Java code for binary search:
public static int find(int [] arr, int val) {
	int lo = 0, hi = arr.length-1;
	while (lo <= hi) {
		int mid = (hi+lo)/2;
		if (val == arr[mid]) 
			return mid;
		else if (val < arr[mid]) 
			hi = mid - 1;
		else
			lo = mid + 1;
	}
	return -1;
}

Files used in this Lecture