28
1 0.56 1.12 1.17 0.32 2.78 7.42 3.14 7.71 Value 6.21 4.42 Iteration 0: step 0. Insertion Sort 2 3 4 5 0 1 8 9 Array index 6 7 Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Insertion Sort

  • Upload
    kisha

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

Array index. 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. Insertion Sort. Iteration i. Repeatedly swap element i with the one to its left if smaller. Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order. Value. 2.78. 7.42. 0.56. 1.12. 1.17. - PowerPoint PPT Presentation

Citation preview

Page 1: Insertion Sort

1

0.56 1.12 1.17 0.322.78 7.42 3.14 7.71Value 6.21 4.42

Iteration 0: step 0.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 2: Insertion Sort

2

0.56 1.12 1.17 0.322.78 7.42 3.14 7.71Value 6.21 4.42

Iteration 1: step 0.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 3: Insertion Sort

3

0.56 1.12 1.17 0.322.78 7.42 3.14 7.71Value 6.21 4.42

Iteration 2: step 0.

Insertion Sort

0.56 7.42

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 4: Insertion Sort

4

7.42 1.12 1.17 0.322.78 0.56 3.14 7.71Value 6.21 4.42

Iteration 2: step 1.

Insertion Sort

0.56 2.78

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 5: Insertion Sort

5

7.42 1.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 2: step 2.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 6: Insertion Sort

6

7.42 1.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 3: step 0.

Insertion Sort

1.12 7.42

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 7: Insertion Sort

7

7.421.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 3: step 1.

Insertion Sort

1.12 2.78

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 8: Insertion Sort

8

7.421.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 3: step 2.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 9: Insertion Sort

9

7.421.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 4: step 0.

Insertion Sort

1.17 7.42

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 10: Insertion Sort

10

7.421.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

2 3 4 50 1 8 9Array index 6 7

Iteration 4: step 1.

Insertion Sort

1.17 2.78

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 11: Insertion Sort

11

7.421.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 4: step 2.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 12: Insertion Sort

12

7.421.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 5: step 0.

Insertion Sort

0.32 7.42

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 13: Insertion Sort

13

7.421.12 1.17 0.322.780.56 3.14 7.71Value 6.21 4.42

Iteration 5: step 1.

Insertion Sort

0.32 2.78

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 14: Insertion Sort

14

7.421.12 1.17 0.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 5: step 2.

Insertion Sort

0.32 1.17

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 15: Insertion Sort

15

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 5: step 3.

Insertion Sort

0.32 1.12

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 16: Insertion Sort

16

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 5: step 4.

Insertion Sort

0.32 0.56

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 17: Insertion Sort

17

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 5: step 5.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 18: Insertion Sort

18

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 6: step 0.

6.21 7.42

2 3 4 50 1 8 9Array index 6 7

Insertion Sort

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 19: Insertion Sort

19

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 6: step 1.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 20: Insertion Sort

20

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 7: step 0.

Insertion Sort

4.42 7.42

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 21: Insertion Sort

21

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.21 4.42

Iteration 7: step 1.

Insertion Sort

4.42 6.21

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 22: Insertion Sort

22

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.214.42

Iteration 7: step 2.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 23: Insertion Sort

23

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.214.42

Iteration 8: step 0.

Insertion Sort

3.14 7.42

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 24: Insertion Sort

24

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.214.42

Iteration 8: step 1.

Insertion Sort

3.14 6.21

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 25: Insertion Sort

25

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.214.42

Iteration 8: step 2.

Insertion Sort

3.14 4.42

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 26: Insertion Sort

26

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.214.42

Iteration 8: step 3.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 27: Insertion Sort

27

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.214.42

Iteration 9: step 0.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.

Page 28: Insertion Sort

28

7.421.12 1.170.32 2.780.56 3.14 7.71Value 6.214.42

Iteration 10: DONE.

Insertion Sort

2 3 4 50 1 8 9Array index 6 7

Iteration i. Repeatedly swap element i with the one to its left if smaller.

Property. After ith iteration, a[0] through a[i] contain first i+1 elements in ascending order.