170 likes | 368 Views
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.
E N D
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 35 2 27 15 12 47 Sorted part 352 27 15 12 47 Element to be inserted into the sorted part
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
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
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
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))
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
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
Task 4 • (lg(n)) < (lg((n)!)) < (nlgn) = (lgnn) • (n!) < (nn)
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)
Task 4 • Lim nn!/nn< Lim nA/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
Task 4 A=(n)n/2n/2 • Lim nn!/nn< Lim nA/nn • = Lim n1 (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
Difference in complexity 2n/2 nn n 1 2 3 4 5 n 1! 2! 3! n!
Task 4 • Lim nn!/ 2n < Lim nA/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 nn!/2n> Lim n(n/2)n/2/ 2n >Lim nB/ 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!
Task 4 • Lim nn!/2n/2< Lim nnn/2/ 2n/2 < Lim nB/ 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
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
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