In the canonical implementations of the Hoare algorithm, we need the starting and ending elements of the array as arguments, and the algo maintains a couple of flags for the start and end of the partitioned array. Here are some standard impls I found: QuickSort and Hoare Partition Hoare Partition Correctness in java
Now, I did the following, and tested it out with a few random arrays. I'm not quite sure if I've done anything wrong - are there any holes in this implementation? It sort of intuitively feels very similar to the implementations above, except for the fact that it takes less arguments. Does it have a better/worse performance (even marginally so)compared to the standard implementation? (even though, yes, both of these are O(n))
(MATLAB)
function partition(m_input)
pivot = m_input(1);
size = length(m_input);
flag = 1;
k = 1;
while(k<=size)
if(m_input(k)>pivot)
swap(m_input(size), m_input(k))
size = size-1;
else
swap(m_input(k), m_input(flag))
flag =k;
k=k+1;
end
end
end
Edit : input changed to m_input .