Presentation is loading. Please wait.

Presentation is loading. Please wait.

ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.

Similar presentations


Presentation on theme: "ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept."— Presentation transcript:

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

2 วัตถุประสงค์ เพื่อศึกษาขั้นตอนวิธี เบื้องต้น เพื่อฝึกการเขียนรหัสเทียม(pseudocode) เพื่อศึกษาตัวอย่างของขั้นตอนวิธีการ ค้นหา การเรียงลำดับข้อมูล เบื้องต้น

3 Algorithms: ขั้นตอนวิธี
Definition: Algorithms is a finite set of precise instructions for performing a computation or for solving a problem. นิยาม : เซตของชุดคำสั่งที่ต้องการให้คอมพิวเตอร์ปฎิบัติเพื่อหาคำตอบของปัญหา

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.

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.

6 Algorithm Example Task: Given a sequence {ai}=a1,…,an, aiN, 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.

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.

8 Ex: Find_Max algorithm
Input: {ai}={25, 12, 40, 76, 03, 98, 55} No. {ai} v [25] [98] [1] 25 12 40 03 98 55 [5] 25 12 40 03 98 55 [25] [2] [98] 25 12 40 03 98 55 [6] 25 12 40 03 98 55 25 12 40 03 98 55 [40] [3] [40] [4] 25 12 40 03 98 55 Pseudocode of the Find_Max algorithm ?

9 Algorithm Characteristics
Some 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.

10 Pseudocode procedure find_max(a1, a2, . . . , an : integers ) v := a1
name arguments type 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 } comment statements

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…]

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 (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

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.}

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.

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.

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?

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

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!

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 {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}

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

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 end if x = ai then location := i else location := 0 return location

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 := {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

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 should not use them on large data sets! We’ll see some more efficient algorithms later in the course.

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 }

25 Bubble Sort aj aj>aj+1 aj+1 swap(aj,aj+1) 25 12 40 03 98 55 12 25

26 Bubble sort (i=1) 1 25 12 2 3 40 03 4 5 98 55 6 for i := 1 to n -1
for j := 1 to n - i if aj > aj+1 then swap( aj, aj+1) Bubble sort (i=1) 1 25 12 2 3 40 03 4 5 98 55 6 swap(25,12) swap(40,03) swap(98,55)

27 Bubble sort (i=2) 1 25 12 2 03 3 40 4 5 98 55 6 for i := 1 to n -1
for j := 1 to n - i if aj > aj+1 then swap( aj, aj+1) Bubble sort (i=2) 1 25 12 2 03 3 40 4 5 98 55 6 swap(25,03)

28 Bubble sort (i=3) 1 25 12 03 2 3 40 4 5 98 55 6 for i := 1 to n -1
for j := 1 to n - i if aj > aj+1 then swap( aj, aj+1) Bubble sort (i=3) 1 25 12 03 2 3 40 4 5 98 55 6 swap(25,03)

29 Bubble sort (i=4) 40 55 98 Sorted data 1 25 12 03 2 3 40 4 5 98 55 6
Result

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.

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 }

32 Insertion sort (j=2) 25 12 40 03 98 55 j i m k j-k-1 j-k 2 1 12 25 12
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 } 25 12 40 03 98 55 j i m k j-k-1 j-k 2 1 12 25 12 40 03 98 55 12 25 40 03 98 55

33 Insertion sort (j=3) 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 } 25 12 40 03 98 55 12 25 40 03 98 55 12 25 40 03 98 55

34 Insertion sort (j=4) 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 } 25 12 40 03 98 55 12 25 40 03 98 55 03 12 25 40 98 55

35 Insertion sort (j=5) 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 } 25 12 40 03 98 55 03 12 25 40 98 55 03 12 25 40 98 55

36 Insertion sort (j=6) 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 } 25 12 40 03 98 55 03 12 25 40 98 55 03 12 25 40 55 98

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

38 Quiz -II (1) ให้เขียนรหัสเทียมของขั้นตอนวิธีอย่างง่าย.
(2) ให้แสดงการเรียงลำดับข้อมูลตามชุดข้อมูลตัวเลขที่กำหนดให้. (20 นาที)

39 Exercises Describe an algorithm that takes a list of integers a1,a2,…,an (n ≥ 2) and finds the second-largest integer in the sequence. 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. Show how the binary search algorithm searches for 27 in the following list: { }

40 Exercises 4. Show how the pseudocode of this sorting output. 1 25 03 2
12 3 40 4 5 98 55 6


Download ppt "ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept."

Similar presentations


Ads by Google