标签:style blog class code c ext
template <class RandomAccessIterator> void __insertion_sort(RandomAccessIterator first, RandomAccessIterator last) { if (first == last) return; // 容器为空,直接返回 for (RandomAccessIterator i = first + 1; i != last; ++i) // 确定子区间 __linear_insert(first, i, value_type(first)); }
template <class RandomAccessIterator, class T> inline void __linear_insert(RandomAccessIterator first, RandomAccessIterator last, T*) { T value = *last; // 新插入元素 if (value < *first) { // 如果比已排序区间的第一个元素还要小 copy_backward(first, last, last + 1); // 使已排序区间整体后移一个位置 *first = value; // 新元素放在开头位置 } else __unguarded_linear_insert(last, value); }
template <class RandomAccessIterator, class T> void __unguarded_linear_insert(RandomAccessIterator last, T value) { RandomAccessIterator next = last; // 指向新插入元素 --next; // 指向之前一个元素 while (value < *next) { // 发现逆转对 *last = *next; // 较大的元素往后走 last = next; --next; } *last = value; // 新元素放入正确位置 }
template <class RandomAccessIterator, class T> // 快排的分割函数 RandomAccessIterator __unguarded_partition(RandomAccessIterator first, RandomAccessIterator last, T pivot) { while (true) { while (*first < pivot) ++first; // 向后移动直到*first大于或等于枢轴 --last; while (pivot < *last) --last; // 向前移动直到*last小于或等于枢轴 if (!(first < last)) return first; // 交叉后停止,返回的迭代器作为右子区间的开头 iter_swap(first, last); // 交换两个迭代器所指元素 ++first; } }
template <class RandomAccessIterator> inline void sort(RandomAccessIterator first, RandomAccessIterator last) { if (first != last) { __introsort_loop(first, last, value_type(first), __lg(last - first) * 2); __final_insertion_sort(first, last); } }
template <class Size> inline Size __lg(Size n) { // 控制分割恶化的情况,求2^k <= n的最大k值 Size k; for (k = 0; n != 1; n >>= 1) ++k; return k; }
template <class RandomAccessIterator, class T, class Size> void __introsort_loop(RandomAccessIterator first, RandomAccessIterator last, T*, Size depth_limit) { while (last - first > __stl_threshold) { // 大于某个值才进行快速排序,这里__stl_threshold = 16 if (depth_limit == 0) { partial_sort(first, last, last); // 分割恶化,采用堆排序 return; } --depth_limit; RandomAccessIterator cut = __unguarded_partition (first, last, T(__median(*first, *(first + (last - first)/2), *(last - 1)))); // 采用三点中值作为枢轴进行快速排序 __introsort_loop(cut, last, value_type(first), depth_limit); // 对右半部分递归进行排序 last = cut; // 调整last位置,下一次while循环将对左半部分排序 } }
template <class RandomAccessIterator> void __final_insertion_sort(RandomAccessIterator first, RandomAccessIterator last) { if (last - first > __stl_threshold) { // 超过16个元素,则分割为两段 __insertion_sort(first, first + __stl_threshold); // 前段使用插入排序 __unguarded_insertion_sort(first + __stl_threshold, last); // 剩余元素逐个插入 } else __insertion_sort(first, last); // 元素个数不超过16,直接使用插入排序 }
template <class RandomAccessIterator, class T> void __unguarded_insertion_sort_aux(RandomAccessIterator first, RandomAccessIterator last, T*) { for (RandomAccessIterator i = first; i != last; ++i) __unguarded_linear_insert(i, T(*i)); // 将i所指元素插入已排序的16个元素的序列中,前面已经介绍过了 } template <class RandomAccessIterator> inline void __unguarded_insertion_sort(RandomAccessIterator first, RandomAccessIterator last) { __unguarded_insertion_sort_aux(first, last, value_type(first)); }
标签:style blog class code c ext
原文地址:http://blog.csdn.net/nestler/article/details/25960139