Question: Please write an algorithm in Java that is going to print an answer after put random sequence. Given a sequence of numbers = 4 1
Please write an algorithm in Java that is going to print an answer after put random sequence.
Given a sequence of numbers = 4 1 17 18 1 8 7 15 7 1 2, if you are going to percolate down from the 2nd number , the sequence becomes = _?_
Answer: Given:
- Perforation starts at = 2nd number
- Sequence = 4 1 17 18 1 8 7 15 7 1 2
Process:
- Iteration parent at 2
- --Children at 4 and 5: 4 1 17 18 1 8 7 15 7 1 2 *
- --First child is bigger. Focused child is at 4
- --Compare the bigger child and the parent
- --Child is bigger, then swap: 4 18 17 1 1 8 7 15 7 1 2 *
- Iteration parent at 4
- --Children at 8 and 9: 4 18 17 1 1 8 7 15 7 1 2 *
- --First child is bigger. Focused child is at 8
- --Compare the bigger child and the parent
- --Child is bigger, then swap: 4 18 17 15 1 8 7 1 7 1 2 *
- --DONE2: because no more children: 4 18 17 15 1 8 7 1 7 1 2
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
