演算法流程如下圖:主要使用的就是類似歸併排序,分為有快取和沒有快取兩種情況,其中快取的大小也影響程式的遞迴呼叫。
源**如下:
template
bidirectionaliterator1 __rotate_adaptive(bidirectionaliterator1 first,
bidirectionaliterator1 middle,
bidirectionaliterator1 last,
distance len1, distance len2,
bidirectionaliterator2 buffer,
distance buffer_size) else if (len1 <= buffer_size) else
}template
bidirectionaliterator3 __merge_backward(bidirectionaliterator1 first1,
bidirectionaliterator1 last1,
bidirectionaliterator2 first2,
bidirectionaliterator2 last2,
bidirectionaliterator3 result)
else }}
template
void __merge_adaptive(bidirectionaliterator first,
bidirectionaliterator middle,
bidirectionaliterator last,
distance len1, distance len2,
pointer buffer, distance buffer_size)
else if (len2 <= buffer_size)
else
else
bidirectionaliterator new_middle =
__rotate_adaptive(first_cut, middle, second_cut, len1 - len11,
len22, buffer, buffer_size);
__merge_adaptive(first, first_cut, new_middle, len11, len22, buffer,
buffer_size);
__merge_adaptive(new_middle, second_cut, last, len1 - len11,
len2 - len22, buffer, buffer_size);}}
template
void __merge_without_buffer(bidirectionaliterator first,
bidirectionaliterator middle,
bidirectionaliterator last,
distance len1, distance len2)
bidirectionaliterator first_cut = first;
bidirectionaliterator second_cut = middle;
distance len11 = 0;
distance len22 = 0;
if (len1 > len2)
else
rotate(first_cut, middle, second_cut);
bidirectionaliterator new_middle = first_cut;
advance(new_middle, len22);
__merge_without_buffer(first, first_cut, new_middle, len11, len22);
__merge_without_buffer(new_middle, second_cut, last, len1 - len11,
len2 - len22);
}template
void __inplace_stable_sort(randomaccessiterator first,
randomaccessiterator last)
randomaccessiterator middle = first + (last - first) / 2;
__inplace_stable_sort(first, middle);
__inplace_stable_sort(middle, last);
__merge_without_buffer(first, middle, last, middle - first, last - middle);
}template
void __merge_sort_loop(randomaccessiterator1 first,
randomaccessiterator1 last,
randomaccessiterator2 result, distance step_size)
step_size = min(distance(last - first), step_size);
merge(first, first + step_size, first + step_size, last, result);
}template
void __chunk_insertion_sort(randomaccessiterator first,
randomaccessiterator last, distance chunk_size)
__insertion_sort(first, last);
}template
void __merge_sort_with_buffer(randomaccessiterator first,
randomaccessiterator last,
pointer buffer, distance*)
}template
void __stable_sort_adaptive(randomaccessiterator first,
randomaccessiterator last, pointer buffer,
distance buffer_size) else
__merge_adaptive(first, middle, last, distance(middle - first),
distance(last - middle), buffer, buffer_size);
}template
inline void __stable_sort_aux(randomaccessiterator first,
randomaccessiterator last, t*, distance*)
template
inline void stable_sort(randomaccessiterator first,
randomaccessiterator last)
穩定排序和非穩定排序
首先,排序演算法的穩定性大家應該都知道,通俗地講就是能保證排序前2個相等的數其在序列的前後位置順序和排序後它們兩個的前後位置順序相同。在簡單形式化一下,如果ai aj,ai原來在位置前,排序後ai還是要在aj位置前。其次,說一下穩定性的好處。排序演算法如果是穩定的,那麼從乙個鍵上排序,然後再從另乙個...
排序 穩定排序和不穩定排序
這幾天筆試了好幾次了,連續碰到乙個關於常見排序演算法穩定性判別的問題,往往還是多選,對於我以及和我一樣拿不準的同學可不是乙個能輕易下結論的題目,當然如果你筆試之前已經記住了資料結構書上哪些是穩定的,哪些不是穩定的,做起來應該可以輕鬆搞定。本文是針對老是記不住這個或者想真正明白到底為什麼是穩定或者不穩...
穩定排序和不穩定排序
這幾天筆試了好幾 次了,連續碰到乙個關於常見排序演算法穩定性判別的問題,往往還是多選,對於我以及和我一樣拿不準的同學可不是乙個能輕易下結論的題目,當然如果你筆試之前 已經記住了資料結構書上哪些是穩定的,哪些不是穩定的,做起來應該可以輕鬆搞定。本文是針對老是記不住這個或者想真正明白到底為什麼是穩定或者...