1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms,...

40
1 MAT MAT 231: 231: คคคคคคคคคคคคค คคคคคคคคคคคคค คคคคคคคคค คคคคคคคคค (2) (2) Algorithms, Pseud Algorithms, Pseud o- o- code, code, Searching and Sorting Searching and Sorting คค.คคค คคคคคคค คค.คคคคคคคคค คคคคค คคค SPU, Computer Science Dept.

Transcript of 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms,...

Page 1: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

1

MAT MAT 231: 231: คณิ�ตศาสตร์ไม่�ต�อเนื่��องคณิ�ตศาสตร์ไม่�ต�อเนื่��อง(2)(2)

Algorithms, PseudAlgorithms, Pseudo-o-code,code,Searching and SortingSearching and Sorting

ดร์.ธนื่า ส�ขวาร์�ดร์.ส�ร์ศ�กด�� ม่�งส�งห์ SPU, Computer Science Dept.

Page 2: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

2

ว�ตถุ�ปร์ะสงค

• เพื่��อศึ�กษาขั้� นตอนวิ�ธี� เบื้� องต�น• เพื่��อฝึ�กการเขั้�ยนรหั�ส

เที�ยม(pseudocode)

• เพื่��อศึ�กษาต�วิอย างขั้องขั้� นตอนวิ�ธี�การ ค้�นหัา การเร�ยงลำ#าดั�บื้ขั้�อม%ลำ เบื้� องต�น

Page 3: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

3

Algorithms: ข�!นื่ตอนื่ว�ธ� Definition: Algorithms is a finite set of precise

instructions for performing a computation or for solving a problem.

•น�ยาม : เซตขั้องชุ(ดัค้#าส��งที��ต�องการใหั�ค้อมพื่�วิเตอร*ปฎิ�บื้�ต�เพื่��อหัาค้#าตอบื้ขั้องป-ญหัา

Page 4: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

4

Algorithms• The foundation of computer programming.• Most generally, an algorithm just means a

definite procedure for performing some sort of task.

• A computer program is simply a description of an algorithm, in a language precise enough for a computer to understand, requiring only operations that the computer already knows how to do.

• We say that a program implements (or “is an implementation of”) its algorithm.

Page 5: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

5

Programming Languages

• Some common programming languages:– Newer: Java, C, C++, C#, Visual Basic,

JavaScript, Perl, PHP, Pascal, many others…

– Older: FORTRAN, COBOL, LISP, BASIC,...– Assembly languages, for low-level coding.

• In this class we will use an informal, Pascal-like “pseudo-code” language.

Page 6: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

6

Algorithm Example

• Task: Given a sequence {ai}=a1,…,an, aiN, say what its largest element is.

• One algorithm for doing this, in English:– Set the value of a temporary variable v

(largest element seen so far) to a1’s value.– Look at the next element ai in the sequence.– If ai>v, then re-assign v to the number ai.– Repeat then previous 2 steps until there are

no more elements in the sequence, & return v.

Page 7: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

7

Executing an Algorithm

• When you start up a piece of software, we say the program or its algorithm are being run or executed by the computer.

• Given a description of an algorithm, you can also execute it by hand, by working through all of its steps with pencil & paper.

Page 8: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

8

Ex: Find_Max algorithm

• Input: {ai}={25, 12, 40, 76, 03, 98, 55}

25

12

40

03

98

55

25

12

40

03

98

55

[1] [25]

25

12

40

03

98

55

25

12

40

03

98

55

25

12

40

03

98

55

25

12

40

03

98

55

[25]

[40]

[40]

[2]

[3]

[4]

[6]

[5] [98]

[98]

{ai}No. v

Pseudocode of the Find_Max algorithm ?

Page 9: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

9

Algorithm CharacteristicsSome important general features of algorithms:• Input. Information or data that comes in.• Output. Information or data that goes out.• Definiteness. Algorithm is precisely defined.• Correctness. Outputs correctly relate to inputs.• Finiteness. Won’t take forever to describe or

run.• Effectiveness. Individual steps are all do-able.• Generality. Works for many possible inputs.• Efficiency. Takes little time & memory to run.

Page 10: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

10

Pseudocode

procedure find_max(a1, a2, . . . , an : integers ) v := a1

for i := 2 to n if ai > v then v := ai

{ max is the largest element }

arguments type

comment statements

name

Page 11: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

11

procedure procname(arg: type)

• Declares that the following text defines a procedure named procname that takes inputs (arguments) named arg which are data objects of the type type.– Example:

procedure maximum(L: list of integers)[statements defining maximum…]

Page 12: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

12

variable := expression

• An assignment statement evaluates the expression expression, then reassigns the variable variable to the value that results.– Example assignment statement:

v := 3x+7 (If x is 2, changes v to 13.)

• In pseudocode (but not real code), the expression might be informally stated:– x := the largest integer in the list L

Page 13: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

13

{comment}

• Not executed (does nothing).

• Natural-language text explaining some aspect of the procedure to human readers.

• Also called a remark in some real programming languages, e.g. BASIC.

• Example, might appear in a max program:– {Note that v is the largest integer seen so far.}

Page 14: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

14

if condition then statement

• Evaluate the propositional expression condition.– If the resulting truth value is True, then

execute the statement statement; – otherwise, just skip on ahead to the next

statement after the if statement.

• Variant: if cond then stmt1 else stmt2– Like before, but iff truth value is False,

executes stmt2.

Page 15: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

15

while condition statement

• Evaluate the propositional (Boolean) expression condition.

• If the resulting value is True, then execute statement.

• Continue repeating the above two actions over and over until finally the condition evaluates to False; then proceed to the next statement.

Page 16: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

16

for var := initial to final stmt

• Initial is an integer expression.• Final is another integer expression.• Semantics: Repeatedly execute stmt, first

with variable var := initial, then with var := initial+1, then with var := initial+2, etc., then finally with var := final.

• Question: What happens if stmt changes the value of var, or the value that initial or final evaluates to?

Page 17: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

17

Find_Max procedure in pseudocode

procedure max(a1, a2, …, an: integers)

v := a1 {largest element so far}

for i := 2 to n {go thru rest of elems}

if ai > v then v := ai {found bigger?}

{at this point v’s value is the same as the largest integer in the list}

return v

Page 18: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

18

Another example task

• Problem of searching an ordered list.– Given a list L of n elements that are sorted

into a definite order (e.g., numeric, alphabetical),

– And given a particular element x,– Determine whether x appears in the list,– and if so, return its index (position) in the list.

• Problem occurs often in many contexts.

• Let’s find an efficient algorithm!

Page 19: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

19

Search alg. #1: Linear Search

procedure linear search(x: integer, a1, a2, …, an: distinct integers)i := 1 {start at beginning of list}while (i n x ai) {not done, not found}

i := i + 1 {go to the next position}if i n then location := i {it was found}else location := 0 {it wasn’t found}return location {index or 0 if not found}

Page 20: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

20

Search alg. #2: Binary Search

• Basic idea: On each step, look at the middle element of the remaining list to eliminate half of it, and quickly zero in on the desired element.

<x >x<x <x

Page 21: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

21

Search alg. #2: Binary Search

procedure binary search(x:integer, a1, a2, …, an: distinct integers) i := 1 {left endpoint of search interval}j := n {right endpoint of search interval}while i<j begin {while interval has >1 item}

m := (i+j)/2 {midpoint}if x>am then i := m+1 else j := m

endif x = ai then location := i else location := 0return location

Page 22: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

22

Practice exercises

• Writing an algorithm that finds the sum of all the integers in a list. [2 min]

• procedure sum(a1, a2, …, an: integers)s := 0 {sum of elements so far}for i := 1 to n {go through all

elements}s := s + ai {add current item}

{at this point s is the sum of all items}return s

Page 23: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

23

Sorting Algorithms

• Sorting is a common operation in many applications.– E.g. spreadsheets and databases

• It is also widely used as a subroutine in other data-processing algorithms.

• Two sorting algorithms shown in textbook:– Bubble sort– Insertion sort However, these are not

very efficient, and you shouldnot use them on large data sets!

We’ll see some more efficient algorithms later in the course.

Page 24: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

24

Bubble sort

procedure bubble sort( a1, a2, . . . , an : integers with n ≥ 2 )for i := 1 to n -1 for j := 1 to n - i if aj > aj+1 then interchange(aj , aj+1 ){ a1, a2, . . . , an is in increasing order }

Page 25: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

25

Bubble Sort

25

12

40

03

98

55

aj

aj+1

aj>aj+1

swap(aj,aj+1)

12

25

40

03

98

55

Page 26: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

26

Bubble sort (i=1)

1 25 12

2 12 25

3 40 03

4 03 40

5 98 55

6 55 98

swap(25,12)

swap(40,03)

swap(98,55)

for i := 1 to n -1 for j := 1 to n - i if aj > aj+1 then swap( aj, aj+1)

Page 27: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

27

Bubble sort (i=2)

1 25 12 12

2 12 25 03

3 40 03 25

4 03 40 40

5 98 55 55

6 55 98 98

swap(25,03)

for i := 1 to n -1 for j := 1 to n - i if aj > aj+1 then swap( aj, aj+1)

Page 28: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

28

Bubble sort (i=3)

1 25 12 12 03

2 12 25 03 12

3 40 03 25 25

4 03 40 40 40

5 98 55 55 55

6 55 98 98 98

swap(25,03)

for i := 1 to n -1 for j := 1 to n - i if aj > aj+1 then swap( aj, aj+1)

Page 29: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

29

Bubble sort (i=4)

1 25 12 12 03

2 12 25 03 12

3 40 03 25 25

4 03 40 40 40

5 98 55 55 55

6 55 98 98 98

03

12

25

40

55

98

Result

Sorted data

Page 30: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

30

Insertion Sort Algorithm

• English description of algorithm:– For each item in the input list,

• “Insert” it into the correct place in the sorted output list generated so far. Like so:

– Use linear or binary search to find the location where the new item should be inserted.

– Then, shift the items from that position onwards down by one position.

– Put the new item in the hole remaining.

Page 31: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

31

Insertion sort

procedure insertion sort( a1, a2, . . . , an : integers with n ≥ 2 )for j := 2 to n begin i := 1 while ( aj > ai ) i := i + 1 m := aj for k := 0 to j – i – 1 aj-k := aj-k-1 ai := m end { a1, a2, . . . , an is in increasing order }

Page 32: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

32

Insertion sort (j=2)

25 12 40 03 98 55

25 12 40 03 98 55

j i m k j-k-1

j-k

2 1 12 0 1 2

procedure insertion sort( a1, a2, . . . , an : integers with n ≥ 2 )for j := 2 to n begin i := 1 while ( aj > ai ) i := i + 1 m := aj for k := 0 to j – i – 1 aj-k := aj-k-1

ai := m end { a1, a2, . . . , an is in increasing order } 12 25 40 03 98 55

Page 33: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

33

Insertion sort (j=3)

12 25 40 03 98 55

25 12 40 03 98 55procedure insertion sort( a1, a2, . . . , an : integers with n ≥ 2 )for j := 2 to n begin i := 1 while ( aj > ai ) i := i + 1 m := aj for k := 0 to j – i – 1 aj-k := aj-k-1

ai := m end { a1, a2, . . . , an is in increasing order }

12 25 40 03 98 55

Page 34: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

34

Insertion sort (j=4)

12 25 40 03 98 55

25 12 40 03 98 55procedure insertion sort( a1, a2, . . . , an : integers with n ≥ 2 )for j := 2 to n begin i := 1 while ( aj > ai ) i := i + 1 m := aj for k := 0 to j – i – 1 aj-k := aj-k-1

ai := m end { a1, a2, . . . , an is in increasing order }

03 12 25 40 98 55

Page 35: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

35

Insertion sort (j=5)

03 12 25 40 98 55

25 12 40 03 98 55procedure insertion sort( a1, a2, . . . , an : integers with n ≥ 2 )for j := 2 to n begin i := 1 while ( aj > ai ) i := i + 1 m := aj for k := 0 to j – i – 1 aj-k := aj-k-1

ai := m end { a1, a2, . . . , an is in increasing order }

03 12 25 40 98 55

Page 36: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

36

Insertion sort (j=6)

03 12 25 40 98 55

25 12 40 03 98 55procedure insertion sort( a1, a2, . . . , an : integers with n ≥ 2 )for j := 2 to n begin i := 1 while ( aj > ai ) i := i + 1 m := aj for k := 0 to j – i – 1 aj-k := aj-k-1

ai := m end { a1, a2, . . . , an is in increasing order }

03 12 25 40 55 98

Page 37: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

37

Review : Algorithms

• Characteristics of algorithms.• Pseudo code.• Examples: Max algorithm, linear search &

binary search algorithms. Sorting.• Intuitively we see that binary search is much

faster than linear search, but how do we analyze the efficiency of algorithms formally?

• Use methods of algorithmic complexity, which utilize the order-of-growth concepts

Page 38: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

38

(1) ใหั�เขั้�ยนรหั�สเที�ยมขั้องขั้� นตอนวิ�ธี�อย างง าย.

(2) ใหั�แสดังการเร�ยงลำ#าดั�บื้ขั้�อม%ลำตามชุ(ดัขั้�อม%ลำต�วิเลำขั้ที��ก#าหันดัใหั�.

(20 นาที�)

Quiz -II

Page 39: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

39

1. Describe an algorithm that takes a list of integers a1,a2,…,an (n ≥ 2) and finds the second-largest integer in the sequence.

2. Describe an algorithm that takes a list of n integers (n ≥1) and finds the average of the largest and smallest integers in the list.

3. Show how the binary search algorithm searches for 27 in the following list: {5 6 8 12 15 21 25 31}

Exercises

Page 40: 1 MAT 231: คณิตศาสตร์ไม่ ต่อเนื่อง (2) Algorithms, Pseudo-code, Searching and Sorting ดร. ธนา สุขวารี ดร. สุรศักดิ์

40

1 25 03 03 03 03 03

2 12 12 12 12 12 12

3 40 40 40 25 25 25

4 03 25 25 40 40 40

5 98 98 98 98 98 55

6 55 55 55 55 55 98

4. Show how the pseudocode of this sorting output.

Exercises