'Does the Block swap algorithm has any advantage over array reversal method, given that both have the same time complexity?

Both these algorithm has a time complexity O(n) and space complexity is constant, the block swap seems more difficult. Is there any disadvantage of using it in CP or inteviews?



Solution 1:[1]

Unless I'm mistaken reversal is just a form of block swapping. You could build your own reversal algorithm I guess, but that would likely be an insufficient use of time. In java there is the Collections.reverse(class_obj). This is sufficient to use in an interview.

If you're tasked with doing another block swapping task then reverse is not what you're going to be using. You will have to likely come up with an iterative approach to successfully swap the array. Because of the nature of arrays in java you'll usually need an additional array to accomplish your task.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cayce K