Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinity as sentinel in mergesort?

Tags:

c

I am currently reading Cormen's "Introduction to Algorithms" and I found something called a sentinel.

It's used in the mergesort algorithm as a tool to decide when one of the two merging lists is exhausted. Cormen uses the infinity symbol for the sentinels in his pseudocode and I would like to know how such an infinite value can be implemented in C.

like image 991
kaiseroskilo Avatar asked Oct 20 '25 02:10

kaiseroskilo


2 Answers

A sentinel is just a dummy value. For strings, you might use a NULL pointer since that's not a sensible thing to have in a list. For integers, you might use a value unlikely to occur in your data set e.g. if you are dealing with a list ages, then you can use the age -1 to denote the list.

like image 176
Jeff Foster Avatar answered Oct 22 '25 17:10

Jeff Foster


You can get an "infinite value" for floats, but it's not the best idea. For arrays, pass the size explicitly; for lists, use a null pointer sentinel.

like image 41
Fred Foo Avatar answered Oct 22 '25 16:10

Fred Foo