Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting arrays/tables Top Down sort Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Sorting arrays/tables Top Down sort Please use speaker notes for additional information!"— Presentation transcript:

1 Sorting arrays/tables Top Down sort Please use speaker notes for additional information!

2 Top down sort - PASS #1 Original numbers: 15 36 24 12 20 SUB1 1 SUB2 2 15 36 24 12 20 Compare 15 which is what SUB1 is pointing to and 36 which is what SUB2 is pointing to. In this instance, 15 < 36 so nothing is done. After numbers: 15 36 24 12 20 HOLD

3 Top down sort - PASS #1 Before numbers: 15 36 24 12 20 SUB1 1 SUB2 2 3 15 36 24 12 20 Compare 15 which is what SUB1 is pointing to and 24 which is what SUB2 is pointing to. In this instance, 15 < 24 so nothing is done. After numbers: 15 36 24 12 20 HOLD

4 Top down sort - PASS #1 Before numbers: 15 36 24 12 20 SUB1 1 SUB2 2 3 4 15 36 24 12 20 Compare 15 which is what SUB1 is pointing to and 12 which is what SUB2 is pointing to. In this instance, 15 >12 so the numbers must be flipped. After numbers: 12 36 24 15 20 HOLD 15 36 24 12 20 12 36 24 12 20 12 36 24 15 20 Three steps in flip: 1) Number SUB1 is pointing to is moved to hold 2) Number SUB2 is pointing to is moved to the spot where SUB1 is pointing 3) Number from HOLD is moved to spot where SUB2 is pointing

5 Top down sort - PASS #1 Before numbers: 12 36 24 15 20 SUB1 1 SUB2 2 3 4 5 12 36 24 15 20 Compare 12 which is what SUB1 is pointing to and 29 which is what SUB2 is pointing to. In this instance, 12 < 20 so nothing is done. After numbers: 12 36 24 15 20 HOLD

6 Top down sort - PASS #1 Before numbers: 12 36 24 15 20 SUB1 1 SUB2 2 3 4 5 6 12 36 24 15 20 Numbers: 12 36 24 15 20 SUB2 3 Adding 1 to SUB2 puts SUB2 outside the range of the table. Therefore the pass is done. SUB1 is incremented by 1 and SUB2 is reset to 1 more than SUB1. PASS2 can now start. SUB1 1 2 At the end of the first pass, the smallest number is located in the first slot of the array/table.

7 Top down sort - PASS #2 Before numbers: 12 36 24 15 20 SUB1 2 SUB2 3 12 36 24 15 20 Compare 36 which is what SUB1 is pointing to and 24 which is what SUB2 is pointing to. In this instance, 36 >24 so the numbers must be flipped. After numbers: 12 24 36 15 20 HOLD 36 12 36 24 15 20 12 24 15 20 12 24 36 15 20 Three steps in flip: 1) Number SUB1 is pointing to is moved to hold 2) Number SUB2 is pointing to is moved to the spot where SUB1 is pointing 3) Number from HOLD is moved to spot where SUB2 is pointing

8 Top down sort - PASS #2 Before numbers: 12 24 36 15 20 SUB1 2 SUB2 3 4 12 24 36 15 20 Compare 24 which is what SUB1 is pointing to and 15 which is what SUB2 is pointing to. In this instance, 24 >15 so the numbers must be flipped. After numbers: 12 15 36 24 20 HOLD 24 12 24 36 15 20 12 15 36 15 20 12 15 36 24 20 Three steps in flip: 1) Number SUB1 is pointing to is moved to hold 2) Number SUB2 is pointing to is moved to the spot where SUB1 is pointing 3) Number from HOLD is moved to spot where SUB2 is pointing

9 Top down sort - PASS #2 Before numbers: 12 15 36 24 20 SUB1 2 SUB2 3 4 5 12 15 36 24 20 Compare 15 which is what SUB1 is pointing to and 20 which is what SUB2 is pointing to. In this instance, 15 < 20 so nothing is done. After numbers: 12 15 36 24 20 HOLD The two smallest numbers are locked into the top positions in the array.

10 Top down sort - PASS #2 Before numbers: 12 15 36 24 20 SUB1 2 SUB2 3 4 5 6 12 15 36 24 20 Numbers: 12 15 36 24 20 SUB2 4 Adding 1 to SUB2 puts SUB2 outside the range of the table. Therefore the pass is done. SUB1 is incremented by 1 and SUB2 is reset to 1 more than SUB1. PASS3 can now start. SUB1 1 2 3 At the end of the second pass, the 2 smallest numbers are located in the first and second slots of the array/table.

11 Top down sort - PASS #3 Before numbers: 12 15 36 24 20 SUB1 3 SUB2 4 12 15 36 24 20 Compare 36 which is what SUB1 is pointing to and 24 which is what SUB2 is pointing to. In this instance, 36 > 24 so the numbers must be flipped. After numbers: 12 15 24 36 20 HOLD 36 12 15 36 24 20 12 15 24 20 12 15 24 36 20 Three steps in flip: 1) Number SUB1 is pointing to is moved to hold 2) Number SUB2 is pointing to is moved to the spot where SUB1 is pointing 3) Number from HOLD is moved to spot where SUB2 is pointing

12 Top down sort - PASS #3 Before numbers: 12 15 24 36 20 SUB1 3 SUB2 4 5 12 15 24 36 20 Compare 24 which is what SUB1 is pointing to and 20 which is what SUB2 is pointing to. In this instance, 24 > 20 so the numbers must be flipped. After numbers: 12 15 20 36 24 HOLD 24 12 15 24 36 20 12 15 20 36 20 12 15 20 36 24 Three steps in flip: 1) Number SUB1 is pointing to is moved to hold 2) Number SUB2 is pointing to is moved to the spot where SUB1 is pointing 3) Number from HOLD is moved to spot where SUB2 is pointing

13 Top down sort - PASS #3 Before numbers: 12 15 20 36 24 SUB1 3 SUB2 4 5 6 12 15 20 36 24 Numbers: 12 15 20 36 24 SUB2 5 Adding 1 to SUB2 puts SUB2 outside the range of the table. Therefore the pass is done. SUB1 is incremented by 1 and SUB2 is reset to 1 more than SUB1. PASS4 can now start. SUB1 1 2 3 4 At the end of the third pass, the 3 smallest numbers are located in the first, second and third slots of the array/table.

14 Before numbers: 12 15 20 36 24 SUB1 4 SUB2 5 12 15 20 36 24 Compare 36 which is what SUB1 is pointing to and 24 which is what SUB2 is pointing to. In this instance, 36 > 24 so the numbers must be flipped. After numbers: 12 15 20 24 36 HOLD 36 12 15 20 36 24 12 15 20 24 12 15 20 24 36 Three steps in flip: 1) Number SUB1 is pointing to is moved to hold 2) Number SUB2 is pointing to is moved to the spot where SUB1 is pointing 3) Number from HOLD is moved to spot where SUB2 is pointing Top down sort - PASS #4

15 Before numbers: 12 15 20 24 36 SUB1 4 SUB2 5 6 12 15 20 24 36 Numbers: 1215202436 Adding 1 to SUB2 puts SUB2 outside the range of the table. Therefore the pass is done. When SUB1 is incremented it points it at the last element in the table. This is the trigger to tell us that the sort is done. The sort is complete. SUB1 1 2 3 4 5 At the end of the fourth pass, all of the numbers are in order. Top down sort - PASS #4

16 Original numbers: Original names: 15 Rake 36 Hoe 24 Shovel 12 Trowel 20 Sprinkler Number and name SUB1 1 SUB2 2 3 4 15 36 24 12 20 After numbers and names: 12 Trowel 36 Hoe 24 Shovel 15 Rake 20 Sprinkler HOLD 15 Rake 36 Hoe 24 Shovel 12 Trowel 20 Sprinkler 12 Trowel 36 Hoe 24 Shovel 12 Trowel 20 Sprinkler 12 Trowel 36 Hoe 24 Shovel 15 Rake 20 Sprinkler Three steps in flip: 1) Number and name SUB1 is pointing to is moved to hold 2) Number and name SUB2 is pointing to is moved to the spot where SUB1 is pointing 3) Number and from HOLD is moved to spot where SUB2 is pointing


Download ppt "Sorting arrays/tables Top Down sort Please use speaker notes for additional information!"

Similar presentations


Ads by Google