zoukankan      html  css  js  c++  java
  • 排序

    1.快速排序

     1 int* partition2(int* begin, int* end) {
     2     auto* p = begin;
     3     if (begin != end) {
     4         auto* last = end - 1;
     5         while (p != last) {
     6             if (p < last) {
     7                 if (*p < *last) {
     8                     --last;
     9                     continue;
    10                 }
    11             } else {
    12                 if (*p >= *last) {
    13                     ++last;
    14                     continue;
    15                 }
    16             }
    17             std::swap(*p, *last);
    18             std::swap(p, last);
    19         }
    20     }
    21     return p;
    22 }
    23 
    24 int* partition(int* begin, int* end) {
    25     auto* p = begin;
    26     if (begin != end) {
    27         auto* last = end - 1;
    28         while (p != last) {
    29             if (*p < *(p + 1)) {
    30                 std::swap(*(p + 1), *last);
    31                 --last;
    32             } else {
    33                 std::swap(*p, *(p + 1));
    34                 ++p;
    35             }
    36         }
    37     }
    38     return p;
    39 }
    40 
    41 void QuickSort(int* begin, int* end) {
    42     if (begin != end) {
    43         const auto p = partition(begin, end);
    44         QuickSort(begin, p);
    45         QuickSort(p + 1, end);
    46     }
    47 }

    首先函数本体是非常优雅的。简直如算法本身一般。然后 两种拆分函数。实现1代码看起来很优雅。实现2也不错,貌似效率还高一点。估计书写方式还可以简化。

  • 相关阅读:
    centos vsftpd
    centos nginx
    linux 修改配色
    面试题讲解
    文件操作
    Python
    Python-linux作业
    python(12.17)笔记
    python周末作业(12.14--16)
    python作业(12.12)
  • 原文地址:https://www.cnblogs.com/waterfall/p/13290866.html
Copyright © 2011-2022 走看看