0% found this document useful (0 votes)
2 views

COE428-1

The document outlines the course COE428: Engineering Algorithms & Data Structures, taught by Professor Reza Sedaghat, including contact information and course references. It defines algorithms, emphasizing their characteristics such as finiteness and definiteness, and provides an example of the sorting problem along with the insertion sort algorithm in pseudocode. The document includes detailed steps and examples illustrating how the insertion sort algorithm processes an input sequence to produce a sorted output.

Uploaded by

hani.ahmed524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

COE428-1

The document outlines the course COE428: Engineering Algorithms & Data Structures, taught by Professor Reza Sedaghat, including contact information and course references. It defines algorithms, emphasizing their characteristics such as finiteness and definiteness, and provides an example of the sorting problem along with the insertion sort algorithm in pseudocode. The document includes detailed steps and examples illustrating how the insertion sort algorithm processes an input sequence to produce a sorted output.

Uploaded by

hani.ahmed524
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Algorithms & Data Structures

• Professor Reza Sedaghat

• COE428: Engineering Algorithms & Data Structures


•Email address: rsedagha@ee.ryerson.ca
• Course outline: www.ee.ryerson.ca/~courses/COE428/
• Course References:
1) Thomas H. Cormen, Charles E. Leiserson, Ronald L.
Rivest. Introduction to Algorithms, MIT, 2002, ISBN: 0‐07‐
013151‐1 (McGraw‐Hill) (Course Text)

1
Algorithms & Data Structures
• Algorithm
• Steps to solve a problem
• Well-defined computational procedure that takes a
set of values as inputs and a set of values as outputs

• In other words, an algorithm is a sequence of


computational steps that transforms the inputs into
outputs to solve a problem

• The statement of a problem specifies the desired


input/output relationship

2
Algorithms & Data Structures

• An algorithm is defined as follows:

• Finiteness:An algorithm must terminate in finite time

• Definiteness: Each step must be precisely defined

• Input: A sequence of n numbers a1, a2, . . . , an

•Output: A permutation (reordering) of the input sequence


such that a1< a2 < . . . < an

3
The problem of sorting

Input: sequence a1, a2, …, an of numbers.

Output: permutation a'1, a'2, …, a'n such


that a'1  a'2  …  a'n .

Example:
Input: 8 2 4 9 3 6
Output: 2 3 4 6 8 9
4
Insertion sort

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


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

5
Insertion sort

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


for j ← 2 to n
do key ← A[ j]
i←j–1
“pseudocode” 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
6
Example of insertion sort
1 2 3 4 5 6 INSERTION-SORT (A, n) ⊳ A[1 . . n]
for j ← 2 to n
8 2 4 9 3 6 do key ← A[ j]
i←j–1
while i > 0 and A[i] > key
do A[i+1] ← A[i]
i←i–1
A[i+1] = key

j=2
A[2] = 2, key = 2

i=1
A[1] = 8, Key=2 8 > 2
Change A[i], A[i+1]
i=0
A[0+1]= 2, key = 2

1 2 3 4 5 6

8 8 4 9 3 6 7
2
Example of insertion sort
INSERTION-SORT (A, n) ⊳ A[1 . . n]
for j ← 2 to n
8 2 4 9 3 6 do key ← A[ j]
i←j–1
while i > 0 and A[i] > key
do A[i+1] ← A[i]
i←i–1
1 2 3 4 5 6 A[i+1] = key

j=3
A[3] = 4, key = 4

i=2
A[2] = 8, Key=4 8 > 4
Change A[i], A[i+1]
i=1
A[1+1]= 4, key = 4
1 2 3 4 5 6

2 8 8 9 3 6 8
4
Example of insertion sort
1 2 3 4 5 6 INSERTION-SORT (A, n) ⊳ A[1 . . n]
for j ← 2 to n
8 2 4 9 3 6 do key ← A[ j]
i←j–1
while i > 0 and A[i] > key
do A[i+1] ← A[i]
i←i–1
A[i+1] = key

9
Example of insertion sort
1 2 3 4 5 6 INSERTION-SORT (A, n) ⊳ A[1 . . n]
for j ← 2 to n
8 2 4 9 3 6 do key ← A[ j]
i←j–1
while i > 0 and A[i] > key
do A[i+1] ← A[i]
i←i–1
A[i+1] = key

10
Example of insertion sort
1 2 3 4 5 6 INSERTION-SORT (A, n) ⊳ A[1 . . n]
8 2 4 9 3 6 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
A[i+1] = key

11
Example of insertion sort
1 2 3 4 5 6
INSERTION-SORT (A, n) ⊳ A[1 . . n]
for j ← 2 to n
8 2 4 9 3 6 do key ← A[ j]
i←j–1
while i > 0 and A[i] > key
do A[i+1] ← A[i]
i←i–1
A[i+1] = key

12
Example
1 2
of insertion
3 4
sort
5 6
INSERTION-SORT (A, n) ⊳ A[1 . . n]
8 2 4 9 3 6 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
A[i+1] = key

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy