zoukankan      html  css  js  c++  java
  • 69 快速排序的递归和非递归实现

    【本文链接】

     http://www.cnblogs.com/hellogiser/p/quicksort-recursively-and-non-recursively.html

    【代码】

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
     
    // 69_QuickSort_NonRecursive.cpp : Defines the entry point for the console application.
    //
    /*
        version: 1.0
        author: hellogiser
        blog: http://www.cnblogs.com/hellogiser
        date: 2014/9/18
    */


    #include "stdafx.h"
    #include "iostream"
    #include <ctime>
    #include <algorithm>
    #include <stack>
    using namespace std;

    void myswap(int &a, int &b)
    {
        
    int t = a;
        a = b;
        b = t;
    }

    void print(int *a, int n)
    {
        
    for (int i = 0; i < n; i++)
        {
            cout << a[i] << 
    " ";
        }
        cout << endl;
    }

    //========================================================
    //   recursively
    //========================================================
    int partition1(int *a, int left, int right)
    {
        
    // a[left,...p-1]<=a[p]<a[p+1,...right]
        int i = left;
        
    int j = right;
        
    int key = a[left];
        
    while(i < j)
        {
            
    while(a[i] <= key) i++;
            
    while(a[j] > key) j--;
            
    if (i < j)
            {
                myswap(a[i], a[j]);
            }
        }
        
    // left---j---i---right
        myswap(a[left], a[j]);
        
    return j;
    }

    int partition2(int *a, int left, int right)
    {
        
    // left, a[left+1...i]<=key, a[i+1,...j-1]>key
        int i = left;
        
    int j = left + 1;
        
    int key = a[left];
        
    while(j <= right)
        {
            
    if (a[j] < key)
            {
                i++;  
    // so that  a[i]>=key
                myswap(a[i], a[j]);
            }
            j++;
        }
        
    //left---i---right---j
        myswap(a[left], a[i]);
        
    return i;
    }

    void quicksort_r(int *a, int left, int right)
    {
        
    if (left < right)
        {
            
    int p = partition1(a, left, right);
            quicksort_r(a, left, p - 
    1);
            quicksort_r(a, p + 
    1, right);
        }
    }

    void QuickSort_Recursively(int *a, int n)
    {
        
    if (a == NULL || n <= 1)
            
    return;
        quicksort_r(a, 
    0, n - 1);
    }

    //========================================================
    //  non recursively
    //========================================================
    void quicksort_non_r(int *a, int left, int right)
    {
        
    // view [left,right] range as a node, which then has 2 child nodes [left,p-1] and [p+1,right] after partitioning
        if (left >= right)
            
    return;
        
    // init stack with one range node
        stack<int> s;
        s.push(left);
        s.push(right);
        
    int temp_right, temp_left;
        
    int p;
        
    while(!s.empty())
        {
            temp_right = s.top();
            s.pop();
            temp_left = s.top();
            s.pop();
            
    if (temp_left < temp_right)
            {
                p = partition1(a, temp_left, temp_right);
                
    // add node [left,p-1] and [p+1,right] to stack
                s.push(temp_left);
                s.push(p - 
    1);

                s.push(p + 
    1);
                s.push(temp_right);
            }
        }
    }

    void QuickSort_NonRecursively(int *a, int n)
    {
        
    if (a == NULL || n <= 1)
            
    return;
        quicksort_non_r(a, 
    0, n - 1);
    }

    typedef void (*fun)(int *a, int n);
    void test_base(int *a, int n, fun f)
    {
        print(a, n);
        f(a, n);
        print(a, n);
        cout << 
    "============================== ";
    }

    void test_recursively()
    {
        srand((
    unsigned int)time(NULL));
        
    const int n = 10;
        
    int a[n];
        
    for (int i = 0; i < n; i++)
        {
            a[i] = rand() % 
    100;
        }
        cout << 
    "recursively..." << endl;
        test_base(a, n, QuickSort_Recursively);
    }

    void test_non_recursively()
    {
        srand((
    unsigned int)time(NULL));
        
    const int n = 20;
        
    int a[n];
        
    for (int i = 0; i < n; i++)
        {
            a[i] = rand() % 
    100;
        }
        cout << 
    "non recursively..." << endl;
        test_base(a, n, QuickSort_NonRecursively);
    }

    void test_main()
    {
        test_recursively();
        test_non_recursively();
    }

    int _tmain(int argc, _TCHAR *argv[])
    {
        test_main();
        
    return 0;
    }

    /*
    recursively...
    43 5 68 83 74 12 80 61 84 48
    5 12 43 48 61 68 74 80 83 84
    ==============================
    non recursively...
    43 5 68 83 74 12 80 61 84 48 67 95 57 2 17 17 98 94 17 76
    2 5 12 17 17 17 43 48 57 61 67 68 74 76 80 83 84 94 95 98
    ==============================
    */

    个人学习笔记,欢迎拍砖!---by hellogiser

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    模块(module)
    编译和解释性语言和python运行方式
    管道通信初级
    Page.TryUpdateModel 方法
    运用模型绑定和web窗体显示和检索数据(Retrieving and displaying data with model binding and web forms)
    HttpResponse 类
    ASP.NET中IsPostBack详解
    3.4.1 使用过滤式扩展方法(P43-44)
    使用扩展方法(Chapter3 P39-41)
    C#对象初始或器-Chapter3 P38
  • 原文地址:https://www.cnblogs.com/hellogiser/p/quicksort-recursively-and-non-recursively.html
Copyright © 2011-2022 走看看