首页 > 其他分享 >VC6的sort不是传说的那么差嘛

VC6的sort不是传说的那么差嘛

时间:2022-12-18 03:55:05浏览次数:35  
标签:sort __ cp int 8d 差嘛 VC6 total cmp

#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

标签:sort,__,cp,int,8d,差嘛,VC6,total,cmp
From: https://www.cnblogs.com/funwithwords/p/16989936.html

相关文章