zoukankan      html  css  js  c++  java
  • 快速排序

    基本思想:第一趟取一个基点,然后把序列分成两部分,小于基点和大于基点,每一部分再递归调用快速排序。不稳定。

    //
    //  QuickSort.c
    //  libin
    //
    //  Created by 李宾 on 16/5/9.
    //  Copyright © 2016年 李宾. All rights reserved.
    //
    
    #include <stdio.h>
    #define SWAP(a, b) { int temp; temp = a; a = b; b = temp;}
    void Quick_Sort(int *p , int left, int right)
    {
        int left_index = left;
        int right_index = right;
        int pivot = p[(left_index + right_index)/2];
        while (left_index <= right_index) {
            while (p[left_index] < pivot) {
                left_index ++;
            }
            while (p[right_index] > pivot) {
                right_index --;
            }
            if (left_index <= right_index) {//相等时候也要交换并加加减减,否则一直递归,栈溢出
                SWAP(p[left_index], p[right_index]);
                left_index ++;
                right_index --;
            }
        }
        if(left_index < right )
        {
            Quick_Sort(p, left_index, right);
        }
        if(right_index > left )
        {
           Quick_Sort(p, left, right_index);
        }
        
    }
    
    int main()
    {
        int a[5] = { 10 , 4, 13, 6, 29 };
        Quick_Sort(a, 0, 4);
        for (int i = 0; i < 5; i ++) {
            printf("%d	", a[i]);
        }
        printf("
    ");
    }
  • 相关阅读:
    清除浮动解决父元素高度塌陷问题
    canvas画动图
    vue实现列表的循环滚动
    localStorage读写操作
    angularJS快速入门
    python模块
    python函数式编程
    python高级特性
    Flask 快速入门
    JQuery Ajax
  • 原文地址:https://www.cnblogs.com/masterlibin/p/5473611.html
Copyright © 2011-2022 走看看