3

I understand that the use of sentinel is somewhat equivalent to zero padding(in the sense of complexity).

For those who don't know what zero-padding is:

we append a number of zeros(or some other values,doesn't matter) to make the size as the power of 2 and after we apply the merge sort algorithm, we eliminate those appended zeros.

However, is there any other way that we can get rid of sentinels except zeros padding?

svick
  • 236,525
  • 50
  • 385
  • 514
Cancan
  • 691
  • 3
  • 14
  • 23

1 Answers1

4

You are confusing two unrelated concepts. They're not close to equivalent. A sentinel is an extra element placed at the (logical) end of a list.

It intentionally has some "limit" value that allows a test that would normally stop traversal of the array to also stop the traversal at the array end.

For example, if we were looking for the first element with value greater than 5 in the list [1, 8, 4, 7], with no sentinel the algorithm would be:

i = 0; 
while i < 4 { if a[i] > 5 return i; i = i + 1 }
return "not found"

we would proceed by adding an element greater than 5, say 6. [1, 8, 4, 7, 6]. Now the search becomes

i = 0;
while a[i] <= 5 { i = i + 1 }
return if i < 4 then i else "not found"

Note the second code has one comparison per iteration. The first has two. This is the reason for sentinels. They allow fewer comparisons.

So you don't want to "get rid" of sentinels. They're a tool to make loops run faster. They only apply in certain cases. When you have one of these cases, they're a useful option.

Zero padding on the other hand is normally used to make recursive divide-and-conquer algorithms simpler. Fast Fourier Transform is one example. It has a very simple implementation for inputs of length 2^n. While algorithms for other size data exist, they're much more complicated. Therefore it's pretty common to zero-pad the data to the next power-of-2 size.

So you don't want to get rid of zero padding either. It's just an implementation option.

Neither sentinels nor aero padding apply very well to mergesort.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • This explains pretty well. But do you have any idea how we can still mergesort without using neither sentinels or zeros padding? – Cancan Sep 15 '13 at 12:02
  • 1
    I have never seen a mergesort that _did_ use either zero padding or sentinels. Look for any merge sort code. Here's one. http://www.vogella.com/articles/JavaAlgorithmsMergesort/article.html – Gene Sep 15 '13 at 16:35