
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <time.h>
#include <algorithm>
#include <functional>
int n_cp, n_cmp, total_cp[2], total_cmp[2];
struct E {
int d;
E& operator=(int d_) { d = d_; return *this; }
E& operator=(const E& e) { ++n_cp; d = e.d; return *this; }
};
bool operator> (const E&x, const E& y) { ++n_cmp; return x.d > y.d; }
E a[2000], b[2000];
void isort(int n) {
for (int i = 1; i < n; i++) {
E cur = b[i];
int j;
for (j = i - 1; j >= 0 && cur > b[j]; j--) b[j + 1] = b[j];
b[j + 1] = cur;
}
}
int main() {
srand(time(0));
for (int n = 30; n <= 100; n++) {
for (int i = 0; i < n; i++) a[i] = b[i] = rand();
n_cp = n_cmp = 0;
std::sort(a, a + n, std::greater<E>());
printf("%8d %8d", n_cp, n_cmp);
total_cp[0] += n_cp; total_cmp[0] += n_cmp;
n_cp = n_cmp = 0;
isort(n); if (memcmp(a, b, n * sizeof(E))) { puts("error"); break; }
printf("\t%8d %8d\n", n_cp, n_cmp);
total_cp[1] += n_cp; total_cmp[1] += n_cmp;
}
printf("%8d %8d\t%8d %8d\n", total_cp[0], total_cmp[0], total_cp[1], total_cmp[1]);
getchar();
return 0;
}
/*
VC6:
22091 33697 85384 85125
22007 33391 85562 85294
22448 33899 85977 85710
g++ (tdm-1) 10.3.0:
23883 33615 84767 84491
23262 33302 85890 85635
23465 33411 85371 85070
1. 以上是次数,时间可不一定。
2. 如果对数据形态有了解,比如知道有很多0,那为啥不遇到0直接往最后面甩呢?需要自己写。
*/
#if 0 // VC6
const int SORT_MAX = 16;
if (LAST - FIRST <= SORT_MAX) Insertion_sort
template<class T> struct greater : binary_function<T, T, bool> {
bool operator()(const T& x, const T& y) const { return (x > y); }
};
template<class _A1, class _A2, class _R>
struct binary_function {
typedef _A1 first_argument_type;
typedef _A2 second_argument_type;
typedef _R result_type;
};
原代码好多下划线,排版也不是上面这样式。可step into进去看:
// TEMPLATE FUNCTION sort WITH PRED
template<class _RI, class _Pr> inline
void sort(_RI _F, _RI _L, _Pr _P)
{_Sort_0(_F, _L, _P, _Val_type(_F)); }
template<class _RI, class _Ty, class _Pr> inline
void _Sort_0(_RI _F, _RI _L, _Pr _P, _Ty *)
{if (_L - _F <= _SORT_MAX)
_Insertion_sort(_F, _L, _P);
else
{_Sort(_F, _L, _P, (_Ty *)0);
_Insertion_sort(_F, _F + _SORT_MAX, _P);
for (_F += _SORT_MAX; _F != _L; ++_F)
_Unguarded_insert(_F, _Ty(*_F), _P); }}
#endif
#if 0
//===-- algorithm_impl.h --------------------------------------------------===//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
//===----------------------------------------------------------------------===//
// <algorithm> -*- C++ -*-
// Copyright (C) 2001-2020 Free Software Foundation, Inc.
* Copyright (c) 1994 Hewlett-Packard Company
* Copyright (c) 1996,1997 Silicon Graphics Computer Systems, Inc.
template <class _ExecutionPolicy, class _RandomAccessIterator, class _Compare, class _IsVector,
class _IsMoveConstructible>
void
__pattern_sort(_ExecutionPolicy&&, _RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp,
_IsVector /*is_vector*/, /*is_parallel=*/std::false_type, _IsMoveConstructible) noexcept
{
std::sort(__first, __last, __comp);
}
#endif

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
2021-12-18 AI vs. Machine Learning vs. Deep Learning vs. Neural Networks: What's the Difference?
2021-12-18 Automate Debugging with GDB Python API
2021-12-18 What is deep learning?
2021-12-18 非线性回归和偏微分
2021-12-18 STL的set有test_and_set功能
2021-12-18 JS实现粘贴上传图片原理
2021-12-18 C++写的推箱子搜索,速度超快