Section 8.4 Insertion Sort CS340 1. Insertion Sort Another quadratic sort, insertion sort, is based...

158
Section 8.4 Insertion Sort CS340 1

Transcript of Section 8.4 Insertion Sort CS340 1. Insertion Sort Another quadratic sort, insertion sort, is based...

Page 1: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

1

CS340

Section 8.4

Insertion Sort

Page 2: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Insertion Sort

Another quadratic sort, insertion sort, is based on the technique used by card players to arrange a hand of cards The player keeps the cards that have been

picked up so far in sorted order When the player picks up a new card, the

player makes room for the new card and then inserts it in its proper place

Page 3: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

To adapt the insertion algorithm to an array that is

filled with data, we start with a sorted subarray

consisting of only the first element

Page 4: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextPos

Page 5: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextPos

Page 6: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextPos

Page 7: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextPos

Page 8: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextPos

Page 9: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextPos

Page 10: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextPos

Page 11: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextPos

Page 12: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. Insert the element at nextPos where it belongs in the array, increasing the length of the sorted subarray by 1 element

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos -

Page 13: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

Page 14: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

nextPos 1

nextVal

loop position

Page 15: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

nextPos 1

nextVal

nextPosloop position

Page 16: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 25

nextPosloop position

Page 17: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

30

25

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 25

nextPosloop position

Page 18: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

30

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 25

nextPosloop position

Page 19: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

30

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

nextPos

loop position

Page 20: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

30

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

nextPos

loop position

Page 21: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

nextPos

loop position

Page 22: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 25

loop position

Page 23: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 25

loop position nextPos

Page 24: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 15

loop position nextPos

Page 25: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

15

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 15

loop position nextPos

Page 26: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 15

loop position nextPos

Page 27: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 15

loop position

nextPos

Page 28: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

30

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 15

loop position

nextPos

Page 29: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 15

loop position

nextPos

Page 30: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 31: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

25

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 32: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 33: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 0

nextVal 15

loop position

nextPos

Page 34: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 15

loop position nextPos

Page 35: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 20

loop position nextPos

Page 36: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

20

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 20

loop position nextPos

Page 37: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 20

loop position nextPos

Page 38: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 20

loop position

nextPos

Page 39: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

30

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 20

loop position

nextPos

Page 40: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 2

nextVal 20

loop position

nextPos

Page 41: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

nextPos

Page 42: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

25

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

nextPos

Page 43: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

nextPos

Page 44: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 1

nextVal 20

loop position

Page 45: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 20

loop position nextPos

Page 46: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 28

loop position nextPos

Page 47: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

28

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 28

loop position nextPos

Page 48: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

30

[0]

[1]

[2]

[3]

[4]

nextPos 4

nextVal 28

loop position nextPos

Page 49: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

loop position

nextPos

Page 50: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

30

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

loop position

nextPos

Page 51: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

loop position

nextPos

Page 52: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Insertion Sort Refinement (cont.)

1.for each array element from the second (nextPos = 1) to the last

2. nextPos is the position of the element to insert

3. Save the value of the element to insert in nextVal

4. while nextPos > 0 and the element at nextPos – 1 > nextVal

5. Shift the element at nextPos – 1 to position nextPos

6. Decrement nextPos by 1

7. Insert nextVal at nextPos

15

20

25

28

30

[0]

[1]

[2]

[3]

[4]

nextPos 3

nextVal 28

Page 53: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Insertion Sort The insertion step is performed n – 1

times In the worst case, all elements in the

sorted subarray are compared to nextVal for each insertion

The maximum number of comparisons will then be:

1 + 2 + 3 + ... + (n – 2) + (n – 1) which is O(n2)

Page 54: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Insertion Sort (cont.)

In the best case (when the array is sorted already), only one comparison is required for each insertion

In the best case, the number of comparisons is O(n) The number of shifts performed during an insertion is one

less than the number of comparisons Or, when the new value is the smallest so far, it is the

same as the number of comparisons A shift in an insertion sort requires movement of only 1

item, while an exchange in a bubble or selection sort involves a temporary item and the movement of three items The item moved may be a primitive or an object reference The objects themselves do not change their locations

Page 55: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Code for Insertion Sort

Listing 8.3 (InsertionSort.java, page 434)

Page 56: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Section 8.5

Comparison of Quadratic Sorts

Page 57: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Comparison of Quadratic Sorts

Number of Comparisons

Number of Exchanges

Best Worst Best Worst

Selection Sort

Bubble Sort

Insertion Sort

Page 58: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Comparison of Quadratic Sorts (cont.)

Comparison of growth rates

Page 59: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Comparison of Quadratic Sorts (cont.)

Comparison of growth rates

Page 60: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Comparison of Quadratic Sorts (cont.)

Insertion sort gives the best performance for most arrays takes advantage of any partial sorting in the

array and uses less costly shifts Bubble sort generally gives the worst

performance—unless the array is nearly sorted big-O analysis ignores constants and overhead

None of the quadratic search algorithms are particularly good for large arrays (n > 1000)

The best sorting algorithms provide n log n average case performance

Page 61: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Comparison of Quadratic Sorts (cont.)

All quadratic sorts require storage for the array being sorted

However, the array is sorted in place While there are also storage

requirements for variables, for large n, the size of the array dominates and extra space usage is O(1)

Page 62: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Comparisons versus Exchanges

In Java, an exchange requires a switch of two object references using a third object reference as an intermediary

A comparison requires an execution of a compareTo method

The cost of a comparison depends on its complexity, but is generally more costly than an exchange

For some other languages, an exchange may involve physically moving information rather than swapping object references. In these cases, an exchange may be more costly than a comparison

Page 63: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Section 8.6

Shell Sort: A Better Insertion Sort

Page 64: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Shell Sort: A Better Insertion Sort

A Shell sort is a type of insertion sort, but with O(n3/2) or better performance than the O(n2) sorts

It is named after its discoverer, Donald Shell

A Shell sort can be thought of as a divide-and-conquer approach to insertion sort

Instead of sorting the entire array, Shell sort sorts many smaller subarrays using insertion sort before sorting the entire array

Page 65: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

Page 66: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 1

Page 67: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 2

Page 68: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 3

Page 69: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 4

Page 70: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 5

Page 71: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 6

Page 72: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 7

Page 73: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 75 55 90

[5] [6] [7] [8] [9]

85 34 45 62 57

[10][11][12][13][14]

65

[15]

gap value 7

subarray 1

Sort subarray 1

Page 74: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 57 55 90

[5] [6] [7] [8] [9]

85 34 45 62 75

[10][11][12][13][14]

65

[15]

gap value 7

subarray 1

Sort subarray 1

Page 75: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 57 55 90

[5] [6] [7] [8] [9]

85 34 45 62 75

[10][11][12][13][14]

65

[15]

gap value 7

subarray 2

Sort subarray 2

Page 76: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 57 55 90

[5] [6] [7] [8] [9]

85 34 45 62 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 3

subarray 3

Page 77: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 57 55 90

[5] [6] [7] [8] [9]

85 34 45 62 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 4

subarray 4

Page 78: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 60

[0] [1] [2] [3] [4]

90 70 57 55 90

[5] [6] [7] [8] [9]

85 34 45 62 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 5

subarray 5

Page 79: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

90 70 57 55 90

[5] [6] [7] [8] [9]

85 60 45 62 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 5

subarray 5

Page 80: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

90 70 57 55 90

[5] [6] [7] [8] [9]

85 60 45 62 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 6

subarray 6

Page 81: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

45 70 57 55 90

[5] [6] [7] [8] [9]

85 60 90 62 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 6

subarray 6

Page 82: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

45 70 57 55 90

[5] [6] [7] [8] [9]

85 60 90 62 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 7

subarray 7

Page 83: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

45 62 57 55 90

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

65

[15]

gap value 7Sort subarray 7

subarray 7

Page 84: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

45 62 57 55 90

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

65

[15]

gap value 7Sort on smaller gap value next

Page 85: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

45 62 57 55 90

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

65

[15]

gap value 3Sort on smaller

gap value

Page 86: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 75 34

[0] [1] [2] [3] [4]

45 62 57 55 90

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

65

[15]

gap value 3Sort subarray 1

subarray 1

Page 87: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 62 34

[0] [1] [2] [3] [4]

45 75 57 55 90

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

65

[15]

gap value 3Sort subarray 1

subarray 1

Page 88: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 62 34

[0] [1] [2] [3] [4]

45 65 57 55 75

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 1

subarray 1

Page 89: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 35 80 62 34

[0] [1] [2] [3] [4]

45 65 57 55 75

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 2

subarray 2

Page 90: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 80 62 35

[0] [1] [2] [3] [4]

45 65 57 55 75

[5] [6] [7] [8] [9]

85 60 90 70 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 2

subarray 2

Page 91: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 80 62 35

[0] [1] [2] [3] [4]

45 65 57 55 75

[5] [6] [7] [8] [9]

70 60 90 85 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 2

subarray 2

Page 92: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 80 62 35

[0] [1] [2] [3] [4]

45 65 57 55 75

[5] [6] [7] [8] [9]

70 60 90 85 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 3

subarray 3

Page 93: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 45 62 35

[0] [1] [2] [3] [4]

80 65 57 55 75

[5] [6] [7] [8] [9]

70 60 90 85 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 3

subarray 3

Page 94: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 45 62 35

[0] [1] [2] [3] [4]

55 65 57 80 75

[5] [6] [7] [8] [9]

70 60 90 85 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 3

subarray 3

Page 95: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 80 90 85 75

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 3

subarray 3

Page 96: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 3Sort subarray 3

subarray 3

Page 97: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 3 Sort on gap value of 1

(a regular insertion sort)

Page 98: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 99: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

40 34 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 100: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 40 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 101: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 40 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 102: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 40 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 103: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 40 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 104: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 40 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 105: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 40 45 62 35

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 106: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 62

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 107: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 62

[0] [1] [2] [3] [4]

55 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 108: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

62 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 109: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

62 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 110: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

62 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 111: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

62 65 57 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 112: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 62 65 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 113: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 62 65 60 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 114: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 115: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 116: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 117: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 75

[5] [6] [7] [8] [9]

70 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 118: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 119: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 120: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 121: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 122: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 123: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 90 85 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 124: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 85 90 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 125: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 85 90 80

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 126: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 85 80 90

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 127: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 85 80 90

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 128: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Shell Sort (cont.)

34 35 40 45 55

[0] [1] [2] [3] [4]

57 60 62 65 70

[5] [6] [7] [8] [9]

75 75 85 80 90

[10][11][12][13][14]

90

[15]

gap value 1 Sort on gap value of 1

(a regular insertion sort)

Page 129: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Shell Sort Algorithm

Shell Sort Algorithm

1. Set the initial value of gap to n / 2

2. while gap > 0

3. for each array element from position gap to the last element

4. Insert this element where it belongs in its subarray.

5. if gap is 2, set it to 1

6. else gap = gap / 2.2. // chosen by experimentation

Page 130: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Shell Sort Algorithm (cont.)

Refinement of Step 4, the Insertion Step

4.1 nextPos is the position of the element to insert

4.2 Save the value of the element to insert in nextVal

4.3 while nextPos > gap and the element at nextPos – gap > nextVal

4.4 Shift the element at nextPos – gap to position nextPos

4.5 Decrement nextPos by gap

4.6 Insert nextVal at nextPos

Page 131: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Shell Sort

Because the behavior of insertion sort is closer to O(n) than O(n2) when an array is nearly sorted, presorting speeds up later sorting

This is critical when sorting large arrays where the O(n2) performance becomes significant

Page 132: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Shell Sort (cont.) A general analysis of Shell sort is an open research

problem in computer science Performance depends on how the decreasing

sequence of values for gap is chosen If successive powers of 2 are used for gap,

performance is O(n2) If successive values for gap are based on Hibbard's

sequence, 2k – 1 (i.e. 31, 15, 7, 3, 1)

it can be proven that the performance is O(n3/2) Other sequences give similar or better performance

Page 133: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Shell Sort (cont.) 

Page 134: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Code for Shell Sort

Listing 8.4 (ShellSort.java, pages 439 – 440)

Page 135: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Section 8.7

Merge Sort

Page 136: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Merge

A merge is a common data processing operation performed on two sequences of data with the following characteristics Both sequences contain items with a

common compareTo method The objects in both sequences are ordered

in accordance with this compareTo method• The result is a third sequence

containing all the data from the first two sequences

Page 137: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Merge Algorithm

Merge Algorithm

1. Access the first item from both sequences.

2. while not finished with either sequence

3. Compare the current items from the two sequences, copy the smaller

current item to the output sequence, and access the next item from the

input sequence whose item was copied.

4. Copy any remaining items from the first sequence to the output sequence.

5. Copy any remaining items from the second sequence to the output sequence.

Page 138: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Merge

For two input sequences each containing n elements, each element needs to move from its input sequence to the output sequence

Merge time is O(n) Space requirements

The array cannot be merged in place Additional space usage is O(n)

Page 139: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Code for Merge

Listing 8.5 (MergeSort.java, page 442)

Page 140: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Merge Sort

We can modify merging to sort a single, unsorted array1. Split the array into two halves2. Sort the left half3. Sort the right half4. Merge the two

This algorithm can be written with a recursive step

Page 141: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

(recursive) Algorithm for Merge Sort

Page 142: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

Page 143: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 30

50 60 45 30

Page 144: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3050

50 60

60

Page 145: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3050 60

60

Page 146: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3045

45 30

30

Page 147: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 30

45

45 304530

Page 148: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 15

50 60 45 3045 304530

30 45 50 60

Page 149: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

90 20 80 15

Page 150: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

90 20 80 15

90 20

Page 151: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

90 20 80 15

90 20

20 90

Page 152: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

20 90 80 15

80 15

Page 153: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

20 90 80 15

80 15

15 80

Page 154: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 80 1530 45 50 60

20 90 15 80

15 20 80 90

Page 155: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Trace of Merge Sort (cont.)

50 60 45 30 90 20 80 15

50 60 45 30 90 20 85 1530 45 50 60 15 20 80 90

15 20 30 45 50 60 80 90

Page 156: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Merge Sort

Each backward step requires a movement of n elements from smaller-size arrays to larger arrays; the effort is O(n)

The number of lines which require merging at each step is log n because each recursive step splits the array in half

The total effort to reconstruct the sorted array through merging is O(n log n)

Page 157: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Analysis of Merge Sort (cont.) 

Page 158: Section 8.4 Insertion Sort CS340 1. Insertion Sort  Another quadratic sort, insertion sort, is based on the technique used by card players to arrange.

Code for Merge Sort

Listing 8.6 (MergeSort.java, pages 445 – 446)