1 / 17

Task # 4, pp. 42

Task # 4, pp. 42. Write an insertion sort algorithm that uses Binary Search to find the position where the next insertion should take place. Example Task explanation. Unsorted array. 35 2 27 15 12 47. The goal is to sort array above Insertion sort. Unsorted part.

akina
Download Presentation

Task # 4, pp. 42

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Task # 4, pp. 42 • Write an insertion sort algorithm that uses Binary Search to find the position where the next insertion should take place.

  2. Example Task explanation Unsorted array 35 2 27 15 12 47 The goal is to sort array above Insertion sort Unsorted part 35 2 27 15 12 47 Sorted part 352 27 15 12 47 Element to be inserted into the sorted part

  3. Write an insertion sort algorithm that uses Binary Search to find the position where the next insertion should take place. Task # 4, pp. 42 352 27 15 12 47 Element to be inserted into the sorted part 2 35 27 15 12 47 Element to be inserted into the sorted part Extended sorted part 22735 15 12 47 Element to be inserted into the sorted part Extended sorted part

  4. Write an insertion sort algorithm that uses Binary Search to find the position where the next insertion should take place. Task # 4, pp. 42 227 3515 12 47 Element to be inserted into the sorted part 21527 35 12 47 • Element inserted into the sorted part • possible methods for identifying the insertion point • sequential search • binary search (is used in task #4 to find insertion position) • other methods

  5. Write an insertion sort algorithm that uses Binary Search to find the position where the next insertion should take place. Task # 4, pp. 42 More info see p. 260 (pseudocode) 227 3515 12 47 Element to be inserted into the sorted part 21527 35 12 47 binary search to find position for 15 • Insertion sort approach for • identifying the next element from the unsorted part and • inserting this element in to the sorted part in to the place • found by the binary search • moving the rest of the sorted part to the right

  6. Write an insertion sort algorithm that uses Binary Search to find the position where the next insertion should take place. Task # 4, pp. 42 More info see p. 260 (pseudocode) 227 3515 12 47 k sorted elements (binary search complexity -- lgk) Element to be inserted into the sorted part 21527 35 12 47 n number of elements binary search used to find position for 15 k+1 sorted elements (binary search complexity -- lg(k+1))

  7. Task 4 • k=1 lg1 number of comparisons • k=2 lg2 • …. • k=n lgn lg1 lg2 lg3 lg(n-1) 1 2 3 4 n

  8. Task 4 • k=1 lg1 number of comparisons • k=2 lg2 • …. • k=n lgn lg((n-1)!)= lg1 + lg2+ lg3+ + lg(n-1) 1 2 3 4 n

  9. Task 4 • (lg(n)) < (lg((n)!)) < (nlgn) = (lgnn) • (n!) < (nn)

  10. Task 4 • n < n! < nn • lg(n) < lg((n)!)) < lgnn • (n) < (n!) < (lgnn) • (lg(n)) < (lg((n)!)) < (nlgn) = (lgnn) • Note • n-4 < n but(n-4)=(n)

  11. Task 4 • Lim nn!/nn< Lim nA/nn • n!= [ 1* 2* *n/2 ] [n/2 +1)(n/2 +2) *n ] • n!<A=[(n/2)(n/2)…(n/2)][n* * n*… *n] • A= (n/2)n/2 * (n)n/2 = • A= (n n/2/2n/2 *(n)n/2 = • A= (n n/2/2n/2 *(n)n/2 = (n)n/2n/2 n/2 timesn/2 times

  12. Task 4 A=(n)n/2n/2 • Lim nn!/nn< Lim nA/nn • = Lim n1 (1/2n/2) =0 • exponential difference in complexity between n! and nn • This difference has the same order as the difference in complexity between n and 2n/2

  13. Difference in complexity 2n/2 nn n 1 2 3 4 5 n 1! 2! 3! n!

  14. Task 4 • Lim nn!/ 2n < Lim nA/n • n!= [ 1* 2* *n/2 ] [n/2 +1)(n/2 +2) *n ] • n!>A=[(n/2)(n/2)…(n/2)][n/2* * n/2*… *n/2] • A=(n/2)n/2 • Lim nn!/2n> Lim n(n/2)n/2/ 2n >Lim nB/ 2n • where B=4n/2 • (n/2)n/2 >= B=4n/2 for n>=16; 8 n/2 > 4n/2 n/2 times Delete from n!

  15. Task 4 • Lim nn!/2n/2< Lim nnn/2/ 2n/2 < Lim nB/ 2n • =Lim n 4n/2/2n/2 =Lim n (2*2)n/2/2n/2 = • =Lim n (2 n/2 *2 n/2)/2n/2 • =Lim n 2 n/2 =  • n! is growing exponentially (2 n/2) in comparison with 2 n

  16. Steps of exponential growth of complexity n  2an a>0, examples 20.1n,2n/2,2n , 21.5n , 22n ,210n Fig 1 Fig 2 Fig 3 nn n! 1 21 22 23 2n 1 2!3!4!n! 1 2122 23 2n/2 exp exp exp n  2n/2 2n  n! n!  nn exp 2n/2 2n

  17. 2n versus 2n/2 • (2 n/2 ) <  (2n) • Lim n 2 n/2n/2 =Lim n 2n/2 = • 2n is growing exponentially faster than 2n/2 • 2n =2n/2 2n/2 2n 2n/2 1 21 22 23 2n/2

More Related