Introduction

What is an algorithm?

A sequence of steps which provides a solution to a given problem, irrespective of the input.

What does "design & analysis of algorithms" mean?

We should understand this — it is the name of the course, after all.

Remarks:

Insertion Sort

Insertion sort demo.

Insertion sort demo.

Input: An array A[1..n] of n numbers.

Output: An array containing the numbers of A in increasing order.


// Note: all indices are 1-indexed
**for** *j* = 2 to *n* **do**
	*key* = *A*[*j*]
	*i* = *j* - 1;
	**while** *i* > 0 and *A*[*i*] > *key* **do**
		*A*[*i*+1] = *A*[*i*]
		*i* = *i* - 1
	**end while**
	*A*[*i*+1] = *key*
**end for**

What is the "best" input for Insertion Sort?

An array sorted in increasing order.

What is the "worst" input for Insertion Sort?