Vous êtes sur la page 1sur 27

Introduction

• Review of Fundamental Data Structure


• Introduction to Algorithm
• Fundamentals of Algorithmic Problem
Solving
• Example of Computation Problem
Review of Fundamental Data
Structures
• Data Structure - refers to a mechanism for
organizing information to provide convenient and
efficient mechanisms for accessing and
manipulating it. (Sedgewick, 1998)

• It is a particular way of storing and organizing


data in a computer so that it can be used
efficiently
Data Structure

Arrays Linked Stacks Queues Binary


List Trees

General Graphs Sets Hash


Trees Tables
Representation

index 0 1 2 3 4 5 6 7 8

test_score 89 79 88 100 90 56 85 40 10
Recall on Stacks
Get ball 1, how?

3
Remove ball 3.
Remove ball 2.
2

1
Seatwork: Tower of Hanoi

1
2
3
4

* 15 or 17 moves are only accepted


Binary Search Tree
0 1 2 3 4 5 6 7 8 9

2 5 6 8 10 12 15 18 20 21

10

5 18

2 6 12 20

8 15 21
What is an algorithm?
• An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of time.
• It is a step-by-step procedure in solving a problem in a
finite amount of time.
Experimental Studies
• Write a program
implementing the algorithm
• Run the program with inputs
of varying size and
composition.
• Use a function (like a built-in
clock() function), to get an
accurate measure of the
actual running time
• Plot the results
Limitations of Experiments
• It is necessary to implement the algorithm,
which may be difficult
• Result may not be indicative of the running
time on other inputs not included in the
experiment
• In order to compare two algorithms, the
same hardware and software
environments must be used.
Algorithm
• Can be represented various forms
• Unambiguity / clearness
• Effectiveness
• Finiteness / termination
• Correctness
Following an Algorithm
Developing an Algorithm
• There are two methodologies used to
develop computer solution to a problem
– Top-Down Design focuses on the tasks to be
done
– Object-Oriented Design focuses on the data
involved in the solution
Top Down Design
• Top-Down Design
– Problem solving technique in which the problem is
divided into subproblems; process is applied to each
subproblem.
• Modules
– Self-contained collection of steps, that solve a problem
or subproblem.
• Abstract Step
– An algorithmic step containing unspecified details
• Concrete Step
– An algorithm step in which all details are specified
Top Down Design
Example: Planning a Large Party
Testing the Algorithm
Important distinction

We test the answer We test the process


Notion of algorithm and problem
problem

algorithm

input “computer” output


(or instance)

Algorithmic Solution
(different from a conventional solution)
Example of Computation Problem: Sorting

• Sorting – is the process of transforming a list into an


equivalent list, in which the elements are arranged in
ascending or descending order.

• It is a frequent operation performed on lists.

• Ordered List – a sorted list

• Sorting algorithm : Bubble Sort,


Sort Selection Sort , Insertion
Sort and many others
Bubble Sort
int arraybubble[] = {43, 22, 17, 36, 16};
Algorithm:
1. Compare arraybubble[0] and arraybubble[1]. If the first
element is greater than the second, swap. If not, do not
swap.
2. Compare arraybubble[1] and arraybubble[2]. If greater
than, swap. If not, do not swap.
3. Compare arraybubble[2] and arraybubble[3]. If greater
than, swap. If not, do not swap.
4. Do the same process until the order is correct.
Bubble Sort Illustrated
Pass 1:
[0] [1] [2] [3] [4] Operation
Step 1: 43 22 17 36 16 Compare [0] and [1]. Swap

Step 2: 22 43 17 36 16 Compare [1] and [2]. Swap

Step 3: 22 17 43 36 16 Compare [2] and [3]. Swap

Step 4: 22 17 36 43 16 Compare [3] and [4]. Swap

22 17 36 16 43
Bubble Sort Illustrated
Pass 2:
[0] [1] [2] [3] [4] Operation
Step 1: 22 17 36 16 43 Compare [0] and [1]. Swap

Step 2: 17 22 36 16 43 Compare [1] and [2]. No Swap

Step 3: 17 22 36 16 43 Compare [2] and [3]. Swap

17 22 16 36 43
Bubble Sort Illustrated
Pass 3:
[0] [1] [2] [3] [4] Operation
Step 1: 17 22 16 36 43 Compare [0] and [1]. No Swap

Step 2: 17 22 16 36 43 Compare [1] and [2]. Swap

17 16 22 36 43
Bubble Sort Illustrated
Pass 4:
[0] [1] [2] [3] [4] Operation
Step 1: 17 16 22 36 43 Compare [0] and [1]. Swap

16 17 22 36 43 Sorted Array
Values from each pass
int arraybubble[] = {43, 22, 17, 36, 16};

Pass 1: 22 17 36 16 43

Pass 2: 17 22 16 36 43

Pass 3: 17 16 22 36 43

Pass 4: 16 17 22 36 43 Sorted Array


Short Quiz 1
• Arrange the following into an ascending
order using Bubble Sort Algorithm and
write the order of data for each pass.

35 15 18 28 34 4 19 45 63 23

Vous aimerez peut-être aussi