Professional Documents
Culture Documents
Algorithms
CCPS 305
Salah Sharieh, Ph.D.
Insertion sort
2
Insertion Sort - Getting Started
1. Review
2. Growth of Functions
3. Recurrences
4. Summations
8. Analyzing of algorithms
9. Designing of algorithms
3
Review : An algorithm
4
Review : Design and Analysis of Algorithms
•Analysis:
predict the cost of an algorithm in terms of resources and
performance
5
Growth
6
Recurrences
When an algorithm contains a recursive call to itself, its running time can
often be described by a recurrence. A recurrence is an equation or
inequality that describes a function in terms of its value on smaller
inputs.
7
Summations
8
Problem: Sorting Problem
The numbers that we wish to sort are also known as the keys
9
Code - Pseudo code
Sometimes, the clearest method is English, so do not be surprised if you come across
an English phrase or sentence embedded within a section of "real" code.
Another difference between pseudocode and real code is that pseudocode is not
typically concerned with issues of software engineering. Issues of data abstraction,
modularity, and error handling are often ignored in order to convey the essence of the
algorithm more concisely.
10
Pseudocode conventions
The looping constructs while, for, and repeat and the conditional
constructs if, then, and else.
The symbol "▹" indicates that the remainder of the line is a comment.
11
Pseudo code conventions
Array elements are accessed by specifying the array name followed by the index in square brackets.
For example, A[i] indicates the ith element of the array A.
The notation "‥" is used to indicate a range of values within an array. Thus, A[1 ‥ j] indicates the subarray of
A consisting of the j elements A[1], A[2], . . . , A[j].
Compound data are typically organized into objects, which are composed of attributes or fields. A
particular field is accessed using the field name followed by the name of its object in square brackets.
For example, we treat an array as an object with the attribute length indicating how many elements it
contains.
To specify the number of elements in an array A, we write length[A].
A variable representing an array or object is treated as a pointer to the data representing the array or
object.
For all fields f of an object x, setting y ← x causes f[y] = f[x].
Moreover, if we now set f[x] ← 3, then afterward not only is f[x] = 3, but f[y] = 3 as well. In other words, x and y
point to ("are") the same object after the assignment y ← x.
Sometimes, a pointer will refer to no object at all. In this case, we give it the special value NIL.
Parameters are passed to a procedure by value: the called procedure receives its own copy of the
parameters, and if it assigns a value to a parameter, the change is not seen by the calling procedure.
12
Algorithm - Insertion sort algorithm
13
INSERTION-SORT (A)
i <- j – 1
• + pseudocode
A[i 1] <- key for insertion sort is presented as a procedure called
INSERTION-SORT, which takes as a parameter an array A[1 ‥ n]
containing a sequence of length n that is to be sorted.
• the number n of elements in A is denoted by length[A].
• The input numbers are sorted in place: the numbers are rearranged
within the array A, with at most a constant number of them stored
outside the array at any time.
• The input array A contains the sorted output sequence when
INSERTION-SORT is finished.
14
http://www.ee.ryerson.ca/~courses/coe428/sorting/insertionsort.html
Loop invariants and the correctness of insertion sort
15
Insertion sort
key
sorted
16
Example of insertion sort
8 2 4 9 3 6
L1.17 17
Example of insertion sort
8 2 4 9 3 6
18
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
19
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
20
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
21
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
22
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
23
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
24
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
25
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
26
Example of insertion sort
8 2 4 9 3 6
2 8 4 9 3 6
2 4 8 9 3 6
2 4 8 9 3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
27
Insertion Sort C
Number of elements 9
28
Insertion Sort Java
Number of elements 9
29
Insertion Sort JavaScript
Number of elements 9
30