Lecture01-Introduction to Algorithms
Lecture01-Introduction to Algorithms
Lecture 01
Introduction to Algorithms
Mohammed Alqmase
Outlines
• What is algorithm?
• How to describe problem?
• How to describe algorithm?
• Characteristics of a Good Algorithm
• How to analyze algorithm?
What is an Algorithm?
What is an Algorithm?
13 1 6 2
How to describe sorting problem?
1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template
How to describe sorting problem?
1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template
1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template
1. Natural Language
13 1 6 2
2. Mathematical Notation
3. Template Input: A set of 𝒏 sortable elements 𝑨 = {𝒂𝟏 , 𝒂𝟐 , … , 𝒂𝒏 }.
Output: A set of sorted elements 𝑨, = {𝒂, 𝟏 , 𝒂, 𝟐 , … , 𝒂, 𝒏 }
Variant:
• 𝑨 sorted in decreasing order.
• 𝑨 sorted in non-decreasing order.
Requirement: providing an algorithm to permute (reorder) the
given input set such that {𝒂, 𝟏 ≥ 𝒂, 𝟐 ≥ ⋯ ≥ 𝒂, 𝒏 } for decreasing
order or {𝒂, 𝟏 ≤ 𝒂, 𝟐 ≤ ⋯ ≤ 𝒂, 𝒏 } for increasing order.
How to describe selection sort Algorithm?
13 1 6 2
How to describe selection sort Algorithm?
1. Natural Language
2. Pseudocode
3. Flowchart
13 1 6 2
How to describe selection sort Algorithm?
1. Natural Language
Let 𝑨[𝟏. . 𝒏] be an array of 𝒏 elements. First,
2. Pseudocode we find the minimum element and interchange
it with the element in position 𝑨[𝟏]. Next, we
3. Flowchart
find the minimum element of the remaining
𝒏 − 𝟏 elements and interchange it with
element in position 𝑨[𝟐]. We continue this way
13 1 6 2 until the second largest element is stored in
𝑨[𝒏 − 𝟏].
How to describe selection sort Algorithm?
1. Natural Language
Algorithm 1.4 SELECTIONSORT
2. Pseudocode Input: An array 𝑨[𝟏. . 𝒏] of 𝒏 elements
3. Flowchart Output: 𝑨[𝟏. . 𝒏] sorted in non-decreasing order.
1. for 𝑖 ← 1 to 𝑛 − 1
2. 𝑘←𝑖
13 1 6 2 3. for 𝑗 ← 𝑖 + 1 to 𝑛
4. if 𝐴[𝑗] < 𝑘 then 𝑘 ← 𝑗
5. end for
6. if 𝑘 ≠ 𝑖 then interchange 𝐴[𝑖] and 𝐴[𝑗]
7. end for
How to describe selection sort Algorithm?
1. Natural Language
2. Pseudocode
3. Flowchart
13 1 6 2
Definitions
❖ Key Metrics:
• Time complexity: Measures how the time to run an algorithm
increases with input size.
• Space complexity: Measures how much memory the algorithm
uses with input size.
Why Analyze Algorithms?