Time%20Complexity - PowerPoint PPT Presentation

About This Presentation
Title:

Time%20Complexity

Description:

Time Complexity Sorting Insertion sorting Time complexity – PowerPoint PPT presentation

Number of Views:188
Avg rating:3.0/5.0
Slides: 31
Provided by: Agam155
Category:

less

Transcript and Presenter's Notes

Title: Time%20Complexity


1
Time Complexity
  • Sorting
  • Insertion sorting
  • Time complexity

2
Sorting
  • Rearrange a0, a1, , an-1 into ascending
    order. When done, a0 lt a1 lt lt an-1
  • 8, 6, 9, 4, 3 gt 3, 4, 6, 8, 9

3
Sort Methods
  • Insertion Sort
  • Bubble Sort
  • Selection Sort
  • Count Sort
  • Shaker Sort
  • Shell Sort
  • Heap Sort
  • Merge Sort
  • Quick Sort

4
Insert An Element
  • Given a sorted list/sequence, insert a new
    element
  • Given 3, 6, 9, 14
  • Insert 5
  • Result 3, 5, 6, 9, 14

5
Insert an Element
  • 3, 6, 9, 14 insert 5
  • Compare new element (5) and last one (14)
  • Shift 14 right to get 3, 6, 9, , 14
  • Shift 9 right to get 3, 6, , 9, 14
  • Shift 6 right to get 3, , 6, 9, 14
  • Insert 5 to get 3, 5, 6, 9, 14

6
Insert An Element
  • // insert t into a0i-1
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t

7
Insertion Sort
  • Start with a sequence of size 1
  • Repeatedly insert remaining elements

8
Insertion Sort
  • Sort 7, 3, 5, 6, 1
  • Start with 7 and insert 3 gt 3, 7
  • Insert 5 gt 3, 5, 7
  • Insert 6 gt 3, 5, 6, 7
  • Insert 1 gt 1, 3, 5, 6, 7

9
Insertion Sort
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • // code to insert comes here

10
Insertion Sort
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • int t ai
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t

11
Insertion Sort
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • int t ai
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t

12
Complexity
  • Space/Memory
  • Time
  • Count a particular operation
  • Count number of steps
  • Asymptotic complexity

13
Comparison Count
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • int t ai
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t

14
Comparison Count
  • Pick an instance characteristic n, n a.length
    for insertion sort
  • Determine count as a function of this instance
    characteristic.

15
Comparison Count
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • How many comparisons are made?

16
Comparison Count
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • number of compares depends on
  • as and t as well as on i

17
Comparison Count
  • Worst-case count maximum count
  • Best-case count minimum count
  • Average count

18
Worst-Case Comparison Count
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj

a 1, 2, 3, 4 and t 0 gt 4 compares a
1,2,3,,i and t 0 gt i compares
19
Worst-Case Comparison Count
  • for (int i 1 i lt n i)
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj

total compares 1 2 3 (n-1)

(n-1)n/2
20
Step Count
  • A step is an amount of computing that does not
    depend on the instance characteristic n
  • 10 adds, 100 subtracts, 1000 multiplies
  • can all be counted as a single step
  • n adds cannot be counted as 1 step

21
Step Count
s/e
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • int t ai
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t


for (int i 1 i lt a.length i)
1 // insert ai into a0i-1
0 int t ai
1 int
j
0 for (j i - 1 j gt 0 t lt aj
j--) 1 aj 1 aj
1 aj 1 t
1

0
22
Step Count
  • s/e isnt always 0 or 1
  • x sum(a, n)
  • where n is the instance characteristic and sum
    adds a0n-1 has a s/e count of n

23
Step Count
s/e
steps
  • for (int i 1 i lt a.length i)
  • // insert ai into a0i-1
  • int t ai
  • int j
  • for (j i - 1 j gt 0 t lt aj j--)
  • aj 1 aj
  • aj 1 t


for (int i 1 i lt a.length i)
1 // insert ai into a0i-1
0 int t ai
1 int
j
0 for (j i - 1 j gt 0 t lt aj
j--) 1 aj 1 aj
1 aj 1 t
1

0
i 1
i
24
Step Count
  • for (int i 1 i lt a.length i)
  • 2i 3
  • step count for
  • for (int i 1 i lt a.length i)
  • is n
  • step count for body of for loop is
  • 2(123n-1) 3(n-1)
  • (n-1)n 3(n-1)
  • (n-1)(n3)

25
Asymptotic Complexity of Insertion Sort
  • O(n2)
  • What does this mean?

26
Complexity of Insertion Sort
  • Time or number of operations does not exceed c.n2
    on any input of size n (n suitably large).
  • Actually, the worst-case time is Q(n2) and the
    best-case is Q(n)
  • So, the worst-case time is expected to quadruple
    each time n is doubled

27
Complexity of Insertion Sort
  • Is O(n2) too much time?
  • Is the algorithm practical?

28
Practical Complexities
  • 109 instructions/second

29
Impractical Complexities
  • 109 instructions/second

30
Faster Computer Vs Better Algorithm
  • Algorithmic improvement more useful
  • than hardware improvement.
  • E.g. 2n to n3
Write a Comment
User Comments (0)
About PowerShow.com