Question

Show what happens at each step when doing the converging pointers algorithm on the following data...

Show what happens at each step when doing the converging pointers algorithm on the following data

0, 0, 14, 0, 23, 19, 0, 6, 0, 81, 0, 5

Show the steps and give the legit value at the end and number of copies done.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The converging pointers algorithm is used to clean up data. This is done by scanning over the list and placing all the zeros in the list to the right side of the list.

We can use 3 variables legit, left, right. Also L for the list.

The initial values are,

legit = n (number of entries in the list)

left = 1

right = n

The scanning starts from left side of the list by checking if left >=right. Also left is incremented by 1 if L​​​​​​left​​ not equals to zero.

First L​​​​​​left​ is to be scanned, which is 0 as we can see.

Next step is to reduce legit by one.

Then L​​​​​​right is being copied to L​​​​​​left​​. Also right = right-1

So, our list will be 5, 0, 14, 0, 33, 19, 0, 6, 0, 81, 0

Now L​left​​ != 0 so left = left +1.

The above steps are repeated and the list will be,

5, 0, 14, 0, 33, 19, 0, 6, 0, 81

Now L​left​​ = 0 so left will be the same

The above steps are repeated and the list will be,

5, 81, 14, 0, 33, 19, 0, 6, 0

Now L​left != 0 so left is incremented and we reached the next 0 element.

We do the same as above and the list will be,

5, 81, 14, 0, 33, 19, 0, 6

Again 0 is found and steps repeated.

5, 81, 14, 6, 33, 19, 0

Now since after the element 6, there are no 0s found. So left will reach the right. So left >= right condition is met.

Next step is to check if L​​​​​​left = 0 and if the condition is true legit=legit - 1

Hence the algorithm stops by producing a list as follows,

5, 81, 14, 6, 33, 19

So the legit value is 6

The copies done are 5

Add a comment
Know the answer?
Add Answer to:
Show what happens at each step when doing the converging pointers algorithm on the following data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Show what happens at each step when doing the converging pointers algorithm on the following data...

    Show what happens at each step when doing the converging pointers algorithm on the following data 0, 0, 14, 0, 23, 19, 0, 6, 0, 81, 0, 5 Show the steps and give the legit value at the end and number of copies done.

  • 1.             (5 marks) Perform a selection sort on the list 23, 59, 34, 13, 31, 10. Show...

    1.             (5 marks) Perform a selection sort on the list 23, 59, 34, 13, 31, 10. Show the list after each exchange that has an effect on the list ordering. 2.             (5 marks) Explain why the bubble sort algorithm does Ө(n2) comparisons on an n-element list. The bubble-sort algorithm is shown just after question 10 on p. 141. 3.              (5 marks) Write the resulting data list, give the ending value of legit, and find the exact number of copies done by the converging...

  • Show the steps of the binary search algorithm (pseudocode is given below); low = index of...

    Show the steps of the binary search algorithm (pseudocode is given below); low = index of first item in list high = index of last item in list while low is less than or equal to high mid = index halfway between low and high if item is less than list[mid] high = mid - 1 else if item is greater than list[mid] low = mid + 1 else return mid end while return not found For each step of...

  • H19 X fx ESEF 1 Given the following... Show in excel what happens when the bond...

    H19 X fx ESEF 1 Given the following... Show in excel what happens when the bond is sold at par value. 2 Face Value $2,000 3 Yearly Interest Rate 10% 4 Coupon Rate 5% 5 Frequency of Payments Semi-Annual 6 Payments/year 7 Number of Payments 8 Maturity Date 1-Jul-50 9 a. Show how the order of YTM, Coupon rate and Current Yield changes when the bond is sold at a discount. 10 b. Show how the order of YTM, Coupon...

  • Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary....

    Please answer all three parts. And show step-by-step answers for each part. Draw anything if necessary. And please don't copy other answers to be at risk being downvoted. Thank you. Question 1 (50 POINTS): Given a graph G and the Breadth First Search (BFS) and Depth First Search (DFS) traversal algorithms as follows: BFSG) 1 for each vertex u € G.V – {3} 1 2 u.color = WHITE 3 u.d = 0 4 un = NIL 3 5 S.color =...

  • Data Structure and Algorithm (a) Given the following integer list: 10 23 12 34 a[0] a[1]...

    Data Structure and Algorithm (a) Given the following integer list: 10 23 12 34 a[0] a[1] a[2] a[3] a[4] Show a trace (step by step) for each execution of Bubble Sort based on the following algorithm. //passes llone pass l/one comparison for (pass = 1 ; pass<= n ; pass++) for (i = 0); i<=n-2; i++) if (a[i] > a[i+1]) { hold = a[i]; a[i] = a[i+1]; a[i+1] = hold; } l/one swap (6 marks)

  • (Weight: 10%) Show what happens in the figure below when the following statement executes: v2.pop_back); kw:...

    (Weight: 10%) Show what happens in the figure below when the following statement executes: v2.pop_back); kw: : vector<int> vl num items = 5 current_capacity 10 int the data [0] [1] 2 4 [5] [6 7 8] [9] 2 4 kw: :vector<int> v2 numitems = 5 current_capacity-10 int+ the data -

  • With the following code answer the following questions. describe what happens when the following code is...

    With the following code answer the following questions. describe what happens when the following code is executed: String[] searchMe = {"apple","bear","cat","dog","elephant"}; describe what is being created when this statement executes System.out.println(linearFind("cat",searchMe)); describe the values passed to the method describe how each of the specific values are compared to each other describe when the method stops executing and/or when the loop stops executing describe what is returned to beoutprinted System.out.println(binaryFind("apple",searchMe)); describe the values passed to the method describe how each of...

  • . PART 3 – Output matrix data to text files using low-level File I/O What happens...

    . PART 3 – Output matrix data to text files using low-level File I/O What happens when you try to use the dlmwrite function with a delimiter that is more than one character? For example, say we require that the file must have two pipe symbols (I 1) between each value? For this part, you will need to output a magic square to a text file, but you will need to use low-level File I/O functions so that you can...

  • please show each law for each step when working out the question. please do not skip...

    please show each law for each step when working out the question. please do not skip and steps and one law per line please and state every line even if it is the same law please. Show that ¬(?∧(?∨¬¬?))≡¬? i need to answer like this format. i have tried many times, but the questions are random. • You can enter formulas using either the operations 7,1,V, or English words not, and, or. So, the input "p and (q or not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT