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

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.