Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. Algorithm Design: Iterative (13 points) An iterative strategy to move any and

ID: 3890245 • Letter: 8

Question

8. Algorithm Design: Iterative (13 points) An iterative strategy to move any and all zeroes in an array A of n numbers, n1, to the left end of the array: 1. Let leftp be a pointer to the leftmost index of A and rightp be a pointer to the rightmost index of A. 2. Move leftp right until leftp is pointing to a cell containing a non-zero number or leftp reaches the right end of A. 3. Move rightp left until rightp is pointing to a cell containing a zero or r reaches the left end of A. 4. If leftp and rightp are not equal or have not crossed (passed) each other, swap the numbers in cells pointed to by leftp and rightp and repeat 2-4. 5. If leftp and rightp are equal or have crossed (passed) each other then stop. The corresponding iterative algorithm is given in part below. Complete it.

Move-zeroes-iterative(A: array [p..r] of number, rp0)

1. leftp = p; rightp = r

2. repeat

3. swap(A[leftp], A[rightp])

4. while leftpr and ___________

5. __________

6. while rightpp and __________

7. ______________

8. until_____________

Explanation / Answer

Algorithm-

1. leftp = p; rightp = r
2. repeat
3. swap(A[leftp], A[rightp])
4. while leftpr and A[leftp]==0
5. leftp=leftp+1;
6. while rightpp and A[rightp]!=0
7. rightp=rightp-1
8. until leftp<=rightp

explanation- move the left pointer right until it encouters a non- zero number and the right pointer moves left until it encounters a zero. This whole process continues until leftp becomes greater than rightp and then stops making sure all zeroes have been placed left.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote