1 0.561.121.170.322.787.423.147.71 value 6.214.42 iteration 0: step 0. insertion sort 23450189array...

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.

Upload: brianne-hunt

Post on 23-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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: 1 0.561.121.170.322.787.423.147.71 Value 6.214.42 Iteration 0: step 0. Insertion Sort 23450189Array index67 Iteration i. Repeatedly swap element i with

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.