Insertion Sort - PowerPoint PPT Presentation

About This Presentation
Title:

Insertion Sort

Description:

void insertion (itemType a[], int N) { int i, j; itemType v; // For each element after the first: for (i = 1; i – PowerPoint PPT presentation

Number of Views:171
Avg rating:3.0/5.0
Slides: 167
Provided by: Alyce
Learn more at: http://max.cs.kzoo.edu
Category:
Tags: insertion | sort

less

Transcript and Presenter's Notes

Title: Insertion Sort


1
Insertion Sort
  • void insertion (itemType a, int N)
  • int i, j itemType v
  • // For each element after the first
  • for (i 1 i lt N i)
  • // Look back through the previous (sorted)
    elts.
  • // Insert this elt after first that is smaller,
  • // moving the others up as you go by them.
  • v ai
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

2
Insertion Sort
  • A S O R T I N G E X A M P L
    E
  • for (i 1 i lt N i) i 1
  • v ai v S
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

3
Insertion Sort
  • A S O R T I N G E X A M P L
    E
  • S
  • for (i 1 i lt N i) i 1
  • v ai v S
  • for (j i-1 j gt 0 aj gt v j--) j 0
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

4
Insertion Sort
  • A S O R T I N G E X A M P L
    E
  • S
  • for (i 1 i lt N i)
  • v ai
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a1 S

5
Insertion Sort
  • A S O R T I N G E X A M P L
    E
  • for (i 1 i lt N i) i 2
  • v ai v O
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

6
Insertion Sort
  • A S O R T I N G E X A M P L
    E
  • O
  • for (i 1 i lt N i) i 2
  • v ai v O
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

7
Insertion Sort
  • A S S R T I N G E X A M P L
    E
  • O
  • for (i 1 i lt N i) i 2
  • v ai v O
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up
  • aj1 aj a2 S
  • // aj lt v put v after aj
  • aj1 v

8
Insertion Sort
  • A S S R T I N G E X A M P L
    E
  • O
  • for (i 1 i lt N i) i 2
  • v ai v O
  • for (j i-1 j gt 0 aj gt v j--) j 0
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

9
Insertion Sort
  • A O S R T I N G E X A M P L
    E
  • O
  • for (i 1 i lt N i) i 2
  • v ai v O
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a1 O

10
Insertion Sort
  • A O S R T I N G E X A M P L
    E
  • for (i 1 i lt N i) i 3
  • v ai v R
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

11
Insertion Sort
  • A O S R T I N G E X A M P L
    E
  • R
  • for (i 1 i lt N i) i 3
  • v ai v R
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

12
Insertion Sort
  • A O S S T I N G E X A M P L
    E
  • R
  • for (i 1 i lt N i) i 3
  • v ai v R
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up
  • aj1 aj a3 S
  • // aj lt v put v after aj
  • aj1 v

13
Insertion Sort
  • A O S S T I N G E X A M P L
    E
  • R
  • for (i 1 i lt N i) i 3
  • v ai v R
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

14
Insertion Sort
  • A O R S T I N G E X A M P L
    E
  • R
  • for (i 1 i lt N i) i 3
  • v ai v R
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a2 R

15
Insertion Sort
  • A O R S T I N G E X A M P L
    E
  • for (i 1 i lt N i) i 4
  • v ai v T
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

16
Insertion Sort
  • A O R S T I N G E X A M P L
    E
  • T
  • for (i 1 i lt N i) i 4
  • v ai v T
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

17
Insertion Sort
  • A O R S T I N G E X A M P L
    E
  • T
  • for (i 1 i lt N i) i 4
  • v ai v T
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a4 T

18
Insertion Sort
  • A O R S T I N G E X A M P L
    E
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

19
Insertion Sort
  • A O R S T I N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

20
Insertion Sort
  • A O R S T T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up
  • aj1 aj a5 T
  • // aj lt v put v after aj
  • aj1 v

21
Insertion Sort
  • A O R S T T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

22
Insertion Sort
  • A O R S S T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up
  • aj1 aj a4 S
  • // aj lt v put v after aj
  • aj1 v

23
Insertion Sort
  • A O R S S T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

24
Insertion Sort
  • A O R R S T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up
  • aj1 aj a3 R
  • // aj lt v put v after aj
  • aj1 v

25
Insertion Sort
  • A O R R S T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

26
Insertion Sort
  • A O O R S T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up
  • aj1 aj a2 O
  • // aj lt v put v after aj
  • aj1 v

27
Insertion Sort
  • A O O R S T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--) j 0
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

28
Insertion Sort
  • A I O R S T N G E X A M P L
    E
  • I
  • for (i 1 i lt N i) i 5
  • v ai v I
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a1 I

29
Insertion Sort
  • A I O R S T N G E X A M P L
    E
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

30
Insertion Sort
  • A I O R S T N G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

31
Insertion Sort
  • A I O R S T T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up
  • aj1 aj a6 T
  • // aj lt v put v after aj
  • aj1 v

32
Insertion Sort
  • A I O R S T T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

33
Insertion Sort
  • A I O R S S T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up
  • aj1 aj a5 S
  • // aj lt v put v after aj
  • aj1 v

34
Insertion Sort
  • A I O R S S T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

35
Insertion Sort
  • A I O R R S T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up
  • aj1 aj a4 R
  • // aj lt v put v after aj
  • aj1 v

36
Insertion Sort
  • A I O R R S T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

37
Insertion Sort
  • A I O O R S T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up
  • aj1 aj a3 O
  • // aj lt v put v after aj
  • aj1 v

38
Insertion Sort
  • A I O O R S T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

39
Insertion Sort
  • A I N O R S T G E X A M P L
    E
  • N
  • for (i 1 i lt N i) i 6
  • v ai v N
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a2 N

40
Insertion Sort
  • A I N O R S T G E X A M P L
    E
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

41
Insertion Sort
  • A I N O R S T G E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

42
Insertion Sort
  • A I N O R S T T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up
  • aj1 aj a7 T
  • // aj lt v put v after aj
  • aj1 v

43
Insertion Sort
  • A I N O R S T T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

44
Insertion Sort
  • A I N O R S S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up
  • aj1 aj a6 S
  • // aj lt v put v after aj
  • aj1 v

45
Insertion Sort
  • A I N O R S S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

46
Insertion Sort
  • A I N O R R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up
  • aj1 aj a5 R
  • // aj lt v put v after aj
  • aj1 v

47
Insertion Sort
  • A I N O R R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

48
Insertion Sort
  • A I N O O R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up
  • aj1 aj a4 O
  • // aj lt v put v after aj
  • aj1 v

49
Insertion Sort
  • A I N O O R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

50
Insertion Sort
  • A I N N O R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up
  • aj1 aj a3 N
  • // aj lt v put v after aj
  • aj1 v

51
Insertion Sort
  • A I N N O R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

52
Insertion Sort
  • A I I N O R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up
  • aj1 aj a2 I
  • // aj lt v put v after aj
  • aj1 v

53
Insertion Sort
  • A I I N O R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--) j 0
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

54
Insertion Sort
  • A G I N O R S T E X A M P L
    E
  • G
  • for (i 1 i lt N i) i 7
  • v ai v G
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a1 G

55
Insertion Sort
  • A G I N O R S T E X A M P L
    E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

56
Insertion Sort
  • A G I N O R S T E X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

57
Insertion Sort
  • A G I N O R S T T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up
  • aj1 aj a8 T
  • // aj lt v put v after aj
  • aj1 v

58
Insertion Sort
  • A G I N O R S T T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

59
Insertion Sort
  • A G I N O R S S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up
  • aj1 aj a7 S
  • // aj lt v put v after aj
  • aj1 v

60
Insertion Sort
  • A G I N O R S S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

61
Insertion Sort
  • A G I N O R R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up
  • aj1 aj a6 R
  • // aj lt v put v after aj
  • aj1 v

62
Insertion Sort
  • A G I N O R R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

63
Insertion Sort
  • A G I N O O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up
  • aj1 aj a5 O
  • // aj lt v put v after aj
  • aj1 v

64
Insertion Sort
  • A G I N O O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

65
Insertion Sort
  • A G I N N O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up
  • aj1 aj a4 N
  • // aj lt v put v after aj
  • aj1 v

66
Insertion Sort
  • A G I N N O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

67
Insertion Sort
  • A G I I N O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up
  • aj1 aj a3 I
  • // aj lt v put v after aj
  • aj1 v

68
Insertion Sort
  • A G I I N O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

69
Insertion Sort
  • A G G I N O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up
  • aj1 aj a2 G
  • // aj lt v put v after aj
  • aj1 v

70
Insertion Sort
  • A G G I N O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--) j 0
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

71
Insertion Sort
  • A E G I N O R S T X A M P L
    E
  • E
  • for (i 1 i lt N i) i 8
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a1 E

72
Insertion Sort
  • A E G I N O R S T X A M P L
    E
  • for (i 1 i lt N i) i 9
  • v ai v X
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

73
Insertion Sort
  • A E G I N O R S T X A M P L
    E
  • X
  • for (i 1 i lt N i) i 9
  • v ai v X
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

74
Insertion Sort
  • A E G I N O R S T X A M P L
    E
  • X
  • for (i 1 i lt N i) i 9
  • v ai v X
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a9 X

75
Insertion Sort
  • A E G I N O R S T X A M P L
    E
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

76
Insertion Sort
  • A E G I N O R S T X A M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

77
Insertion Sort
  • A E G I N O R S T X X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up
  • aj1 aj a10 X
  • // aj lt v put v after aj
  • aj1 v

78
Insertion Sort
  • A E G I N O R S T X X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

79
Insertion Sort
  • A E G I N O R S T T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up
  • aj1 aj a9 T
  • // aj lt v put v after aj
  • aj1 v

80
Insertion Sort
  • A E G I N O R S T T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

81
Insertion Sort
  • A E G I N O R S S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up
  • aj1 aj a8 S
  • // aj lt v put v after aj
  • aj1 v

82
Insertion Sort
  • A E G I N O R S S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

83
Insertion Sort
  • A E G I N O R R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up
  • aj1 aj a7 R
  • // aj lt v put v after aj
  • aj1 v

84
Insertion Sort
  • A E G I N O R R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

85
Insertion Sort
  • A E G I N O O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up
  • aj1 aj a6 O
  • // aj lt v put v after aj
  • aj1 v

86
Insertion Sort
  • A E G I N O O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

87
Insertion Sort
  • A E G I N N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up
  • aj1 aj a5 N
  • // aj lt v put v after aj
  • aj1 v

88
Insertion Sort
  • A E G I N N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

89
Insertion Sort
  • A E G I I N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 3
  • // aj is bigger move it up
  • aj1 aj a4 I
  • // aj lt v put v after aj
  • aj1 v

90
Insertion Sort
  • A E G I I N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

91
Insertion Sort
  • A E G G I N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 2
  • // aj is bigger move it up
  • aj1 aj a3 G
  • // aj lt v put v after aj
  • aj1 v

92
Insertion Sort
  • A E G G I N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

93
Insertion Sort
  • A E E G I N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 1
  • // aj is bigger move it up
  • aj1 aj a2 E
  • // aj lt v put v after aj
  • aj1 v

94
Insertion Sort
  • A E E G I N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--) j 0
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

95
Insertion Sort
  • A A E G I N O R S T X M P L
    E
  • A
  • for (i 1 i lt N i) i 10
  • v ai v A
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a1 A

96
Insertion Sort
  • A A E G I N O R S T X M P L
    E
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

97
Insertion Sort
  • A A E G I N O R S T X M P L
    E

  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 10
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

98
Insertion Sort
  • A A E G I N O R S T X X P L
    E

  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 10
  • // aj is bigger move it up
  • aj1 aj a11 X
  • // aj lt v put v after aj
  • aj1 v

99
Insertion Sort
  • A A E G I N O R S T X X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

100
Insertion Sort
  • A A E G I N O R S T T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up
  • aj1 aj a10 T
  • // aj lt v put v after aj
  • aj1 v

101
Insertion Sort
  • A A E G I N O R S T T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

102
Insertion Sort
  • A A E G I N O R S S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up
  • aj1 aj a9 S
  • // aj lt v put v after aj
  • aj1 v

103
Insertion Sort
  • A A E G I N O R S S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

104
Insertion Sort
  • A A E G I N O R R S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up
  • aj1 aj a8 R
  • // aj lt v put v after aj
  • aj1 v

105
Insertion Sort
  • A A E G I N O R R S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

106
Insertion Sort
  • A A E G I N O O R S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up
  • aj1 aj a7 O
  • // aj lt v put v after aj
  • aj1 v

107
Insertion Sort
  • A A E G I N O O R S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

108
Insertion Sort
  • A A E G I N N O R S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up
  • aj1 aj a6 N
  • // aj lt v put v after aj
  • aj1 v

109
Insertion Sort
  • A A E G I N O O R S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

110
Insertion Sort
  • A A E G I M N O R S T X P L
    E
  • M
  • for (i 1 i lt N i) i 11
  • v ai v M
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a5 M

111
Insertion Sort
  • A A E G I M N O R S T X P L
    E
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

112
Insertion Sort
  • A A E G I M N O R S T X P L
    E

  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 11
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

113
Insertion Sort
  • A A E G I M N O R S T X X L
    E

  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 11
  • // aj is bigger move it up
  • aj1 aj a12 X
  • // aj lt v put v after aj
  • aj1 v

114
Insertion Sort
  • A A E G I M N O R S T X X L
    E

  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 10
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

115
Insertion Sort
  • A A E G I M N O R S T T X L
    E

  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 10
  • // aj is bigger move it up
  • aj1 aj a11 T
  • // aj lt v put v after aj
  • aj1 v

116
Insertion Sort
  • A A E G I M N O R S T T X L
    E
  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

117
Insertion Sort
  • A A E G I M N O R S S T X L
    E
  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up
  • aj1 aj a10 S
  • // aj lt v put v after aj
  • aj1 v

118
Insertion Sort
  • A A E G I M N O R S S T X L
    E
  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

119
Insertion Sort
  • A A E G I M N O R R S T X L
    E
  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up
  • aj1 aj a9 R
  • // aj lt v put v after aj
  • aj1 v

120
Insertion Sort
  • A A E G I M N O R R S T X L
    E
  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

121
Insertion Sort
  • A A E G I M N O P R S T X L
    E
  • P
  • for (i 1 i lt N i) i 12
  • v ai v P
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a8 M

122
Insertion Sort
  • A A E G I M N O P R S T X L
    E
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

123
Insertion Sort
  • A A E G I M N O P R S T X L
    E

  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 12
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

124
Insertion Sort
  • A A E G I M N O P R S T X X
    E

  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 12
  • // aj is bigger move it up
  • aj1 aj a13 X
  • // aj lt v put v after aj
  • aj1 v

125
Insertion Sort
  • A A E G I M N O P R S T X X
    E

  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 11
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

126
Insertion Sort
  • A A E G I M N O P R S T T X
    E

  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 11
  • // aj is bigger move it up
  • aj1 aj a12 T
  • // aj lt v put v after aj
  • aj1 v

127
Insertion Sort
  • A A E G I M N O P R S T T X
    E

  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 10
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

128
Insertion Sort
  • A A E G I M N O P R S S T X
    E

  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 10
  • // aj is bigger move it up
  • aj1 aj a11 S
  • // aj lt v put v after aj
  • aj1 v

129
Insertion Sort
  • A A E G I M N O P R S S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

130
Insertion Sort
  • A A E G I M N O P R R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 9
  • // aj is bigger move it up
  • aj1 aj a10 R
  • // aj lt v put v after aj
  • aj1 v

131
Insertion Sort
  • A A E G I M N O P R R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

132
Insertion Sort
  • A A E G I M N O P P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 8
  • // aj is bigger move it up
  • aj1 aj a9 P
  • // aj lt v put v after aj
  • aj1 v

133
Insertion Sort
  • A A E G I M N O P P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

134
Insertion Sort
  • A A E G I M N O O P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 7
  • // aj is bigger move it up
  • aj1 aj a8 O
  • // aj lt v put v after aj
  • aj1 v

135
Insertion Sort
  • A A E G I M N O O P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

136
Insertion Sort
  • A A E G I M N N O P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 6
  • // aj is bigger move it up
  • aj1 aj a7 N
  • // aj lt v put v after aj
  • aj1 v

137
Insertion Sort
  • A A E G I M N N O P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up YES!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

138
Insertion Sort
  • A A E G I M M N O P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 5
  • // aj is bigger move it up
  • aj1 aj a6 M
  • // aj lt v put v after aj
  • aj1 v

139
Insertion Sort
  • A A E G I M N O O P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--) j 4
  • // aj is bigger move it up NO!
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

140
Insertion Sort
  • A A E G I L M N O P R S T X
    E
  • L
  • for (i 1 i lt N i) i 13
  • v ai v L
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v a5 M

141
Insertion Sort
  • A A E G I L M N O P R S T X
    E
  • for (i 1 i lt N i) i 14
  • v ai v E
  • for (j i-1 j gt 0 aj gt v j--)
  • // aj is bigger move it up
  • aj1 aj
  • // aj lt v put v after aj
  • aj1 v

142
Insertion Sort
Write a Comment
User Comments (0)
About PowerShow.com