You are on page 1of 30

Course : CCPS305 - Data Structures and

Algorithms
CCPS 305
Salah Sharieh, Ph.D.
Insertion sort

2
Insertion Sort - Getting Started

1. Review

2. Growth of Functions

3. Recurrences

4. Summations

5. Problem- Sorting problem

6. Code - ``Pseudo code``

7. Algorithm - insertion sort algorithm

8. Analyzing of algorithms

9. Designing of algorithms

3
Review : An algorithm

An algorithm is any well-defined computational procedure that takes


some value, or set of values, as input and produces some value, or set of
values, as output.

An algorithm is thus a sequence of computational steps that transform


the input into the output.

4
Review : Design and Analysis of Algorithms

•Analysis:
predict the cost of an algorithm in terms of resources and
performance

•Design: design algorithms which minimize the cost

5
Growth

The order of growth of the running time of an algorithm gives:


A simple characterization of the algorithm's efficiency
Allows us to compare the relative performance of alternative algorithms.

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.

Example : That the worst-case running time T (n) of the MERGE-SORT


procedure could be described by the recurrence

7
Summations

When an algorithm contains an iterative control construct such as a while


or for loop, its running time can be expressed as the sum of the times
spent on each execution of the body of the loop.

By adding up the time spent on each iteration, we obtained the


summation (or series)

Evaluating this summation yielded a bound of Θ(n2) on the worst-case


running time of the algorithm.

This example indicates the general importance of understanding how to


manipulate and bound summations.

8
Problem: Sorting Problem

Sort a sequence of numbers into no decreasing order.

Here is how we formally define the sorting problem:

Input sequence 〈31, 41, 59, 26, 41, 58〉


- a sorting algorithm returns the output below
output the sequence 〈26, 31, 41, 41, 58, 59〉

The numbers that we wish to sort are also known as the keys
9
Code - Pseudo code

In this course, we shall describe algorithms as programs written in a pseudocode and


when possible give examples using the following languages
1. C
2. Java
3. Java Script

What separates pseudocode from "real" code is that in pseudocode, we employ


whatever expressive method is most clear and concise to specify a given algorithm.

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

Indentation indicates block structure.


Using indentation instead of conventional indicators of block structure, such as
begin and end statements, greatly reduces clutter while preserving, or even
enhancing, clarity.

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.

A multiple assignment of the form i ← j ← e assigns to both variables i


and j the value of expression e; it should be treated as equivalent to the
assignment j ← e followed by the assignment i ← j.

Variables (such as i, j, and key) are local to the given procedure.

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

Insertion sort : an efficient algorithm for sorting a


small number of elements.

We start with an empty left hand and the cards face


down on the table.
We then remove one card at a time from the table
and insert it into the correct position in the left hand.
To find the correct position for a card, we compare it
with each of the cards already in the hand, from right
to left
At all times, the cards held in the left hand are
sorted, and these cards were originally the top cards
of the pile on the table.

13
INSERTION-SORT (A)

for j <- 2 to length[A]

do key <- A[j]

Insert A[j] into the sorted sequence A[1 . . j - 1].

i <- j – 1

while i > 0 and A[i] > key http://www.ee.ryerson.ca/~courses/coe428/


do A[i + 1] <- A[i] sorting/insertionsort.html
i <- i - 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

Properties of A[1 ‥ j -1] formally as a loop invariant

We use loop invariants to help us understand why an algorithm is correct.


We must show three things about a loop invariant:
Initialization: It is true prior to the first iteration of the loop.
Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration.
Termination: When the loop terminates, the invariant gives us a useful property that helps show that
the algorithm is correct.

15
Insertion sort

INSERTION-SORT (A, n) ⊳ A[1 . . n]


for j ← 2 to n
do key ← A[ j]
“pseudocode” i←j–1
while i > 0 and A[i] > key
do A[i+1] ← A[i]
i←i–1
A[i+1] = key
1 i j n
A:

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

Input 2, 4, 9, 6, 23, 12, 34, 0, 1,

Output 0, 1, 2, 4, 6, 9, 12, 23, 34,

28
Insertion Sort Java

Number of elements 9

Input 2, 4, 9, 6, 23, 12, 34, 0, 1,

Output 0, 1, 2, 4, 6, 9, 12, 23, 34,

29
Insertion Sort JavaScript

Number of elements 9

Input 2, 4, 9, 6, 23, 12, 34, 0, 1,

Output 0, 1, 2, 4, 6, 9, 12, 23, 34,

30

You might also like