• 剑指offer 快速排序


      题目链接: 剑指offer

      题目描述: 快速排序

      解题思路: 快速排序

      代码: 

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iterator>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <deque>
    #include <map>
    #include <set>
    #include <queue>
    #define lson l, m, rt<<1
    #define rson m+1, r, rt<<1|1
    #define mem0(a) memset(a,0,sizeof(a))
    #define mem1(a) memset(a,-1,sizeof(a))
    #define sca(x) scanf("%d",&x)
    #define de printf("=======
    ")
    typedef long long ll;
    using namespace std;
    
    int RandomInRange(int s, int e) {
        return rand() % (e-s+1) + s;
    }
    
    void swap(int &a, int &b) {
        int temp = a;
        a = b;
        b = temp;
    }
    
    int Partition(int data[], int len, int start, int end) {
        if( data == NULL || len <= 0 || start < 0 || end >= len ) {
            throw new std::exception();
        }
        int index = RandomInRange(start, end);
        if( index != end ) swap(data[index], data[end]);
        int small = start-1;
        for( int index = start; index < end; index++ ) {
            if( data[index] < data[end] ) {
                small++;
                if( small != index ) {
                    swap(data[small], data[index]);
                }
            }
        }
        small++;
        swap(data[small], data[end]);
        return small;
    }
    
    void QuickSort(int data[], int len, int start, int end) {
        if(start == end) return;
        int index = Partition(data, len, start, end);
        if( index > start ) {
            QuickSort(data, len, start, index-1);
        }
        if( index < end ) {
            QuickSort(data, len, index+1, end);
        }
    }
    
    int main() {
        int data[10] = {134,32,13,510,5,5,7,78,96,15};
        QuickSort(data, 10, 0, 9);
        for( int i = 0; i < 10; i++ ) {
            cout << data[i] << " ";
        }
        cout << endl;
        return 0;
    }
    View Code

      思考: 写写剑指offer里的题, 练练自己的代码能力

  • 相关阅读:
    Java反射实现几种方式
    kettle之几个.bat初步介绍
    eclipse 启动项目时候报的错误
    转:kettle中调度kitchen.bat后面接参数,写定时更新任务
    hibernate4,manytomany
    hibernate4,onetomany
    Javascript系列之在HTML中使用JavaScript
    Javascript系列之js简介
    004 C++ 指针
    003 C++ 函数
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/7506374.html
Copyright © 2020-2023  润新知