/*
To print binary search's average case and the worst complexity
for various value of N (array size).
The average case complexity is obtained by simulation (and counting
number of comparisons.
The worst case complexity is obtained by the formula.
The res
Insertion Sort - Average Case Analysis
Claim 1:
Let CS (i ) denote the average number of comparisons made to insert the i th element
into a sorted segment of length i 1 to form a sorted segment of length i . Then
1i
CS (i ) = i+1
2
Proof:
Consider the ca
Lecture 2
Covers:
Big-Oh Notation
Bubble sort
Selection sort
Reading: Lafore chapters 2 & 3
1
> The Big-Oh Notation
2
The big-oh notation is widely used to describe
performance of algorithms
The performance of an algorithm is expressed as
some func
Example to introduce the idea of Shell sort
We want to sort the array
0
9
1
3
2
2
3
6
4
0
5
8
6
7
7
1
8
4
9
5
7
1
8
4
9
5
Suppose we choose to do this in 3 steps
Sort elements at distance 4 a part
Sort elements at distance 2 apart
Sort elements at distanc
Bubble Sort Tracing
The tracing below allows us to easily count the swaps. Note that each times we swap a
pair (indicated by underlining the pair), we move down a line.
Example:
0
1
2
3
4
6
4
10
2
8
6
4
10
2
8
Pass 1
*
6
4
swap
10
2
8
4
6
10
No swap
2
8
4
Lecture 4
Covers:
Shell sort
Reading: Lafore chapter 7
1
> The Basic Idea
2
Shell sort uses insertion sort and improve on it
For insertion sort, we can observe that
Fact 1: On lists that are mostly sorted, it performs quite
well, of O(N)
Fact 2: In other
/* LECTURE 4 - SUPPLEMENTARY
Contains:
1) The shell sort method that can be called with a user-defined sequence of
diminishing gaps.
2) Methods to generate Shell's, Knuth's and Flamig's sequences
*/
Also, note how we carry out the testing incrementally. W
Welcome to
Algorithms and Data Structures
CSE2ALG / CSE5ALG
1
Lecture 1
Covers:
Overview of the Subject
Linear Search
Binary Search
Complexity Analysis
Complexity of Linear Search
Complexity of Binary Search
Reading:
Lafore chapters 1 and 2
2
> O
Selection Sort Tracing
In the tracing below, underlined are the actions we know in advance that we have to
perform for each pass
Example:
0
1
2
3
4
6
4
10
2
8
Pass 1
max index (how it change as we move through the indexes)
0
0
2
2
2
*
Swap a[2] and a[4],
Lecture 3
Covers:
Insertion Sort
Reading: Lafore chapter 3
1
> Insertion Sort
The idea
2
The idea
Insertion sort is similar to what card players often
use to sort a hand of cards when they pick up the
cards one by one
3
To start off, the player picks up t
Binary Search - A program to calculate for the number of
comparisons for the average case and to compare it with
that of the worst case
/*
To print binary search's average case and the worst complexity for various
value of N (array size).
The average case
Tracing the Execution of the Insertion Sort
Java Code (Algorithm)
The Java code for this sorting algorithm (which is just as readable as the pseudo code) is as
follows:
public static void insertionSort( int [] a, int left, int right)
cfw_
/ perform a numb