Download presentation
Presentation is loading. Please wait.
1
1 Efficiency of Algorithms: Analyzing algorithm segments, Insertion sort algorithm
2
2 Computing an Order of an Algorithm Segment Consider the following algorithm segment: max:=a[1]; min:=b[1] for i:=2 to n if max < a[i] then max:=a[i] ; if min > b[i] then min:=b[i] next i if min ≥ max then med:=(min+max)/2 Analysis: Number of loop iterations: n-1 Comparisons in each iteration: 2 Thus, elementary operations in the loop: 2(n-1) Operations after the loop: 3 Total number of operations in the segment: 2(n-1)+3 = 2n+1 Order of the segment: O(n)
3
3 The order of an Algorithm with a Nested Loop Consider the following algorithm segment: bigwin:=0; tie:=0 for i:=1 to n for j:=i+1 to n if h[i][j]==r[i][j] then tie:=tie+1; if (h[i][j]-r[i][j] ≥3 or r[i][j]-h[i][j] ≥3) then bigwin:=bigwin+1 next j next i Analysis: Number of times the inner loop is iterated: (n-1)+(n-2)+…+1 = n(n-1)/2 At most 6 operations in each iteration. Maximum number of operations: 6∙n(n-1)/2 = 3n 2 -3n The algorithm is O(n 2 ).
4
4 The Insertion Sort Algorithm Insertion sort arranges the elements of one-dimensional array a[1], …, a[n] into increasing order. for i:=2 to n (insert a[i] into the sorted sequence a[1],…,a[i-1]) key:=a[i] for j:=1 to i-1 if key<a[j] then goto (*) next j (*) (shift the values of a[j],…,a[i-1] to a[j+1],…,a[i]) for k:=i to j+1 a[k]:=a[k-1] next k a[j]:=key next i
5
5 The Insertion Sort Algorithm: Example Arrange array into increasing order. 827914827914 287914287914 278914278914 278914278914 127894127894 124789124789
6
6 The Insertion Sort Algorithm: Analysis For each i (outer loop), maximum number of elementary operations is i – 1. Thus, the total number of elementary operations: 1+2+…+(n-1) = n(n-1)/2 =.5n 2 -.5n The insertion sort algorithm is O(n 2 ).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.