ELC in dev
ELC-lang
载入中...
搜索中...
未找到
elc::defs::base::sort_n 命名空间参考

类型定义

typedef compare_t default_compare_t
 

函数

template<typename iter_t , typename compare_t = default_compare_t>
void heapify (iter_t begin, iter_t end, iter_t i, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
void build_max_heap (iter_t begin, iter_t end, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
void heapsort (iter_t begin, iter_t end, compare_t compare=compare_t())
 
template<typename array_t , typename compare_t = default_compare_t>
void heapify (array_t &array, size_t i, compare_t compare=compare_t())
 
template<typename array_t , typename compare_t = default_compare_t>
void build_max_heap (array_t &array, compare_t compare=compare_t())
 
template<typename array_t , typename compare_t = default_compare_t>
array_t && heapsort (array_t &&array, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
void insertion_sort (iter_t begin, iter_t end, compare_t compare=compare_t())
 
template<typename array_t , typename compare_t = default_compare_t>
array_t && insertion_sort (array_t &&array, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
iter_t partition (iter_t begin, iter_t end, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
void quicksort (iter_t begin, iter_t end, compare_t compare=compare_t())
 
template<typename array_t , typename compare_t = default_compare_t>
array_t && quicksort (array_t &&array, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
void introsort_loop (iter_t begin, iter_t end, size_t depth_limit, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
void introsort (iter_t begin, iter_t end, compare_t compare=compare_t())
 
template<typename array_t , typename compare_t = default_compare_t>
array_t && introsort (array_t &&array, compare_t compare=compare_t())
 
template<typename iter_t , typename compare_t = default_compare_t>
void sort (iter_t begin, iter_t end, compare_t compare=compare_t())
 
template<typename array_t , typename compare_t = default_compare_t>
array_t && sort (array_t &&array, compare_t compare=compare_t())
 

变量

constexpr size_t size_threshold = 1<<4
 

类型定义说明

◆ default_compare_t

函数说明

◆ build_max_heap() [1/2]

template<typename array_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::build_max_heap ( array_t &  array,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11687 行定义.

11687 {
11688 build_max_heap(array.begin(), array.end(), compare);
11689 }
void build_max_heap(iter_t begin, iter_t end, compare_t compare=compare_t())
函数调用图:

◆ build_max_heap() [2/2]

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::build_max_heap ( iter_t  begin,
iter_t  end,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11660 行定义.

11660 {
11661 auto n = end - begin;
11662
11663 // 从最后一个非叶子节点开始进行heapify,直到根节点
11664 for (auto i = n / 2 - 1; i >= 0; i--)
11665 heapify(begin, end, begin + i, compare);
11666 }
void heapify(iter_t begin, iter_t end, iter_t i, compare_t compare=compare_t())
函数调用图:
这是这个函数的调用关系图:

◆ heapify() [1/2]

template<typename array_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::heapify ( array_t &  array,
size_t  i,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11683 行定义.

11683 {
11684 heapify(array.begin(), array.end(), array.begin() + i, compare);
11685 }
函数调用图:

◆ heapify() [2/2]

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::heapify ( iter_t  begin,
iter_t  end,
iter_t  i,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11639 行定义.

11639 {
11640 auto largest = i; // 初始化largest为根节点
11641 auto left_child = begin + 2 * (i - begin) + 1; // 左子节点
11642 auto right_child = left_child + 1; // 右子节点
11643
11644 // 如果左子节点比根节点大,则更新largest
11645 if (left_child < end && compare(*left_child, *largest)==strong_ordering::greater)
11646 largest = left_child;
11647
11648 // 如果右子节点比根节点大,则更新largest
11649 if (right_child < end && compare(*right_child, *largest)==strong_ordering::greater)
11650 largest = right_child;
11651
11652 // 如果largest不是根节点,则交换根节点和largest的值,并递归地对largest子树进行heapify
11653 if (largest != i) {
11654 swap(*i,*largest);
11655 heapify(begin, end, largest, compare);
11656 }
11657 }
constexpr struct elc::defs::base::compare_t compare
constexpr void swap(T1 &a, T2 &b)
函数调用图:
这是这个函数的调用关系图:

◆ heapsort() [1/2]

template<typename array_t , typename compare_t = default_compare_t>
array_t && elc::defs::base::sort_n::heapsort ( array_t &&  array,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11691 行定义.

11691 {
11692 heapsort(array.begin(), array.end(), compare);
11693 return forward<array_t>(array);
11694 }
void heapsort(iter_t begin, iter_t end, compare_t compare=compare_t())
函数调用图:

◆ heapsort() [2/2]

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::heapsort ( iter_t  begin,
iter_t  end,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11669 行定义.

11669 {
11670 auto n = end - begin;
11671
11672 // 构建最大堆
11673 build_max_heap(begin, end, compare);
11674
11675 // 从最后一个元素开始,依次将其与堆顶元素交换,并对堆顶元素进行heapify
11676 for (auto i = n - 1; i >= 0; i--) {
11677 swap(*begin,begin[i]);
11678 heapify(begin, begin + i, begin, compare);
11679 }
11680 }
函数调用图:
这是这个函数的调用关系图:

◆ insertion_sort() [1/2]

template<typename array_t , typename compare_t = default_compare_t>
array_t && elc::defs::base::sort_n::insertion_sort ( array_t &&  array,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11713 行定义.

11713 {
11714 insertion_sort(array.begin(), array.end(), compare);
11715 return forward<array_t>(array);
11716 }
void insertion_sort(iter_t begin, iter_t end, compare_t compare=compare_t())
函数调用图:

◆ insertion_sort() [2/2]

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::insertion_sort ( iter_t  begin,
iter_t  end,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11698 行定义.

11698 {
11699 // 从第二个元素开始,依次将其插入到前面已排序的数组中
11700 for (auto i = begin + 1; i < end; i++) {
11701 auto key = move(*i);
11702 auto j = i - 1;
11703 // 将比key大的元素向后移动
11704 while (j >= begin && compare(*j, key)==strong_ordering::greater) {
11705 *(j + 1) = move(*j);
11706 j--;
11707 }
11708 // 将key插入到正确的位置
11709 *(j + 1) = move(key);
11710 }
11711 }
函数调用图:
这是这个函数的调用关系图:

◆ introsort() [1/2]

template<typename array_t , typename compare_t = default_compare_t>
array_t && elc::defs::base::sort_n::introsort ( array_t &&  array,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11787 行定义.

11787 {
11788 introsort(array.begin(), array.end(), compare);
11789 return forward<array_t>(array);
11790 }
void introsort(iter_t begin, iter_t end, compare_t compare=compare_t())
函数调用图:

◆ introsort() [2/2]

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::introsort ( iter_t  begin,
iter_t  end,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11777 行定义.

11777 {
11778 const auto n = end - begin;
11779 // 如果数组长度小于等于size_threshold,使用插入排序
11780 if (n <= size_threshold)
11781 return insertion_sort(begin, end, compare);
11782 // 否则使用introsort
11783 const auto depth_limit = decltype(n)(2 * log(n, 2));
11784 introsort_loop(begin, end, depth_limit, compare);
11785 }
void introsort_loop(iter_t begin, iter_t end, size_t depth_limit, compare_t compare=compare_t())
函数调用图:
这是这个函数的调用关系图:

◆ introsort_loop()

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::introsort_loop ( iter_t  begin,
iter_t  end,
size_t  depth_limit,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11758 行定义.

11758 {
11759 while (end - begin > size_threshold) {
11760 // 如果递归深度达到了限制,使用heapsort排序
11761 if (depth_limit == 0) {
11762 heapsort(begin, end, compare);
11763 return;
11764 }
11765 depth_limit--;
11766
11767 // 分割数组并递归排序
11768 const auto pivot = partition(begin, end, compare);
11769 introsort_loop(pivot + 1, end, depth_limit, compare);
11770 end = pivot;
11771 }
11772 insertion_sort(begin, end, compare);
11773 }
函数调用图:
这是这个函数的调用关系图:

◆ partition()

template<typename iter_t , typename compare_t = default_compare_t>
iter_t elc::defs::base::sort_n::partition ( iter_t  begin,
iter_t  end,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11721 行定义.

11721 {
11722 // 选取最后一个元素作为pivot
11723 auto&pivot = end[-1];
11724 auto i = begin;
11725 for (auto j = begin; j < end - 1; j++) {
11726 // 如果当前元素小于pivot,将其交换到左边
11727 if (compare(*j, pivot)==strong_ordering::less) {
11728 swap(*i,*j);
11729 i++;
11730 }
11731 }
11732 // 将pivot放到正确的位置
11733 swap(*i,pivot);
11734 return i;
11735 }
函数调用图:
这是这个函数的调用关系图:

◆ quicksort() [1/2]

template<typename array_t , typename compare_t = default_compare_t>
array_t && elc::defs::base::sort_n::quicksort ( array_t &&  array,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11748 行定义.

11748 {
11749 quicksort(array.begin(), array.end(), compare);
11750 return forward<array_t>(array);
11751 }
void quicksort(iter_t begin, iter_t end, compare_t compare=compare_t())
函数调用图:

◆ quicksort() [2/2]

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::quicksort ( iter_t  begin,
iter_t  end,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11739 行定义.

11739 {
11740 while (end - begin > 1) {
11741 // 分割数组并递归排序
11742 const auto pivot = partition(begin, end, compare);
11743 quicksort(begin, pivot, compare);
11744 begin = pivot + 1;
11745 }
11746 }
iter_t partition(iter_t begin, iter_t end, compare_t compare=compare_t())
函数调用图:
这是这个函数的调用关系图:

◆ sort() [1/2]

template<typename array_t , typename compare_t = default_compare_t>
array_t && elc::defs::base::sort_n::sort ( array_t &&  array,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11798 行定义.

11798 {
11799 sort(array.begin(), array.end(), compare);
11800 return forward<array_t>(array);
11801 }
void sort(iter_t begin, iter_t end, compare_t compare=compare_t())
函数调用图:

◆ sort() [2/2]

template<typename iter_t , typename compare_t = default_compare_t>
void elc::defs::base::sort_n::sort ( iter_t  begin,
iter_t  end,
compare_t  compare = compare_t() 
)

在文件 all_defs.cpp11794 行定义.

11794 {
11795 introsort(begin, end, compare);
11796 }
函数调用图:
这是这个函数的调用关系图:

变量说明

◆ size_threshold

constexpr size_t elc::defs::base::sort_n::size_threshold = 1<<4
constexpr

在文件 all_defs.cpp11754 行定义.