n Internal Sorting Bubble Sort Insertion Sort Selection


การเรยงลำดบและการคนหาแบบง าย n Internal Sorting : Bubble Sort Insertion Sort Selection Sort n Searching : Ø Linear search Ø Binary search 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 2/30





Algorithm Bubble_Sort (to sort unsorted array with size num) 1. for (j 2, j<num, j j+1) for (i 2, i≤num, i i+1) 1. 1 if (data[i] < data[i-1]) 1. 1. 1 temp data [i] 1. 1. 2 data[i] data[i-1] 1. 1. 3 data[i-1] temp 2. end. 7/30

43 22 22 22 22 43 43 17 17 80 80 80 17 17 17 43 36 36 36 17 17 17 80 36 36 36 43 16 16 36 36 80 16 16 16 43 29 16 16 16 80 29 29 29 43 29 29 29 80 80 Bubble Sort Pass 1 Pass 2 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 8/30

22 17 17 17 22 22 36 36 36 16 16 16 36 29 29 29 36 43 43 43 80 80 80 17 17 17 16 16 16 22 22 16 16 16 17 17 17 16 16 22 22 22 29 29 36 36 43 43 80 80 Bubble Sort Pass 3 Pass 4 Pass 5 Pass 6 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 9/30


Algorithm Insertion_Sort (ascending order) 1. for(i 1, i<num, i i+1) 1. 1 temp = data[i] 1. 2 for(j i ; j>0 && data[j-1]>temp ; j--) 1. 2. 1 data[j] data[j-1]; 1. 3 data[j] temp; 2. end 11/30

Insertion Sort 43 22 17 17 16 16 22 43 22 22 17 17 80 80 43 36 22 22 17 17 80 43 36 29 36 36 36 80 43 36 16 16 80 43 29 29 29 80 #compare = 17, #move = 14, #pass = 5 What if descending order? 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 12/30


Algorithm Selection_Sort (ascending order) 1. for(i 0 ; i<max ; i++) 1. 1 min_position i 1. 2 for(j i+1 ; j<=max ; j++) 1. 2. 1 if(data[j] < data[min_position]) 1. 2. 1. 1 min_position j 1. 3 temp data[min_position] 1. 4 data[min_position] data[i] 1. 5 data[i] temp 2. end 14/30

Selection Sort (with smallest) 43 16 16 16 22 22 17 17 17 29 17 36 16 29 17 36 43 29 22 36 43 22 29 36 43 80 80 #move = 3, #compare = 21, #pass = 6 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 15/30

Selection Sort (with largest) 43 43 16 16 16 22 22 22 17 17 80 29 29 29 17 22 22 17 17 29 29 29 36 36 16 16 43 43 43 29 80 80 80 #move = 4, #compare = 21, #pass = 6 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 16/30




Linear/Sequential Search search key = 25 , (unsuccessful) 43 22 17 36 16 16 17 22 36 43 16 Sorted data What if descending sorted data? 43 22 17 36 Unsorted data 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 20/30


Binary Search 3. ถาไมตรงกน กรณขอมลเรยงลำดบจากนอยไปมาก ใหดำเนนการดงน 3. 1 ถา search key < middle value concentrate on the first half data and do (1) - (3) 3. 2ถา search key > middle value concentrate on the second half data and do (1) - (3) 22/30

Binary Search 12 15 18 23 26 Search key = 17 37 39 41 43 48 mid 12 15 18 23 26 37 39 41 43 48 mid 12 15 18 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 23/30

Binary Search 12 15 18 23 26 Search key = 37 37 39 41 43 48 mid 12 15 18 23 26 37 39 41 mid 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 24/30

Exercise 1 n Ascending Sort 23 78 45 8 32 99 19 56 n Searching 1) key = 32 2) key = 70 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 25/30

Exercise 2 n Descending Sort 90 31 9 25 43 11 68 57 88 n Searching 1) key = 89 2) key = 25 What if adds 18 at the end? 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 26/30

Have a courage to make your dream come true … 344 -211 Algorithmic Process and Programming , created by Dararat Saelee 27
- Slides: 27