Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.

Similar presentations


Presentation on theme: "Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the."— Presentation transcript:

1 Lecture 4 1 Advance Analysis of Algorithms

2 Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the first position Find the second smallest element and exchange it with the element in the second position Continue until the array is sorted

3 Selection Sort 3 The algorithm works as follows: 1. Find the minimum value in the list 2. Swap it with the value in the first position 3. Repeat the steps above for the remainder of the list (starting at the second position and advancing each time)

4 Example 4 1329648 8329641 8349621 8649321 8964321 8694321 9864321 9864321 Step 1 Step 2 Step 3 Step 7 Sorted Array

5 Selection Sort 5 Alg.: SELECTION-SORT(A) n ← length[A] for j ← 1 to n - 1 smallest ← j for i ← j + 1 to n if A[i] < A[smallest] then smallest ← i exchange A[j] ↔ A[smallest]

6 6

7 7 Selection Sort

8 8

9 9

10 Example 10 Dry Run the Selection-Sort algorithm on the following example 7,-5,2, 16, 4

11 Selection Sort 11 Worst case О (n 2 ) Best case О (n 2 ) Average case О (n 2 )

12 Insertion Sort 12 Idea: like sorting a hand of playing cards Start with an empty left hand and the cards facing down on the table. Remove one card at a time from the table, and insert it into the correct position in the left hand compare it with each of the cards already in the hand, from right to left The cards held in the left hand are sorted these cards were originally the top cards of the pile on the table

13 Example 13

14 Summary 14 Insertion sort algorithm somewhat resembles selection sort. Array is imaginary divided into two parts - sorted one and unsorted one. At the beginning, sorted part contains first element of the array and unsorted one contains the rest. At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. When unsorted part becomes empty, algorithm stops.

15 INSERTION-SORT 15 Alg.: INSERTION-SORT(A) for j ← 2 to n key ← A[ j ] i ← j - 1 while i > 0 and A[i] > key A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key a8a8 a7a7 a6a6 a5a5 a4a4 a3a3 a2a2 a1a1 12345678 key

16 Example 16 Dry Run the Insertion Sort algorithm on the following example 7,-5,2, 16, 4

17 Analysis of Insertion Sort 17 INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key cost times c 1 n c 2 n-1 c 4 n-1 c 5 c 6 c 7 c 8 n-1

18 Best Case Analysis 18 The array is already sorted A[i] ≤ key upon the first time the while loop test is run (when i = j -1) t j = 1 T(n) = c 1 n + c 2 (n -1) + c 4 (n -1) + c 5 (n -1) + c 8 (n-1) = (c 1 + c 2 + c 4 + c 5 + c 8 )n + (c 2 + c 4 + c 5 + c 8 ) = an + b =  (n) “while i > 0 and A[i] > key”

19 Worst Case Analysis The array is in reverse sorted order Always A[i] > key in while loop test Have to compare key with all elements to the left of the j -th position  compare with j-1 elements  t j = j a quadratic function of n T(n) =  (n 2 ) order of growth in n 2 19 “while i > 0 and A[i] > key”

20 Insertion Sort 20 Best case О (n) Worst case О (n 2 ) Average caseO(n 2 )

21 Sorting 21 Insertion sort Design approach: Sorts in place: Best case: Worst case: Bubble Sort Design approach: Sorts in place: Running time: Yes  (n)  (n 2 ) incremental Yes  (n 2 ) incremental

22 Sorting 22 Selection sort Design approach: Sorts in place: Running time: Yes  (n 2 ) incremental

23 Analysis of algorithm(revision) 23 It isn’t sufficient that our algorithms perform the required tasks. We want them to do so efficiently, making the best use of Space Time

24 Time and Space 24 Time Instructions take time. How fast does the algorithm perform? Space Data structures take space. What kind of data structures can be used?


Download ppt "Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the."

Similar presentations


Ads by Google