Content deleted Content added
m add pseudo-assignment to disambiguate "comment". improve formatting of comments. |
make comment formatting consistent. change assignments to use := instead of = sign, for consistency with rest of article. |
||
Line 96:
The ''Select'' algorithm divides the list into groups of five elements. (Left over elements are ignored for now.) Then, for each group of five, the median is calculated (an operation that can potentially be made very fast if the five values can be loaded into [[Processor register|registers]] and compared). (If sorting in-place, then these medians are moved into one contiguous block in the list.) ''Select'' is then called recursively on this sublist of ''n''/5 elements to find their true median. Finally, the "median of medians" is chosen to be the pivot.
''//returns the index of the median of medians.''
''//requires a variant of select, "selectIdx"''
''//which returns the index of the selected item rather than the value'' '''function''' medianOfMedians(list, left, right)
numMedians = (right - left) / 5
'''for''' i '''from''' 0 '''to''' numMedians
subLeft := left + i*5
subRight := subLeft + 4
if (subRight > right) subRight := right
medianIdx := selectIdx(list, subLeft, subRight, (subRight - subLeft) / 2)
swap list[left+i] and list[medianIdx]
'''return''' selectIdx(list, left, left + numMedians, numMedians / 2)
|