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

    快速排序的思想:

    在整个数列中,选取一个特征点,一般以数列的中心为该特征点,使其小于特征点的数放在特征点的左侧,大于特征点的数放在右侧。

    (1)若特征点最左侧的数小于特征点,而特征点最右侧的数大于特征点,则左侧数右移一位,或右侧色数左移一位

    (2)若此时左侧的数值大于特征点,而右侧的数字小于特征点,则两者数值调换

    (3)若左侧数值大于特征点,右侧数值也大于特征点,则右侧特征点左移一位,直到此时特征点右侧的数值小于特征点,将特征点左右两侧色数值互调

    (4)若左侧数值小于特征点,右侧数值也小于特征点,则左侧特征点右移一位,直到此时特征点左侧的数值大于特征点,将特征点左右两侧色数值互调

    (5)然后采用递归的方式,使其整个数列呈现递增。

    动画演示:

    #include<iostream>
    using namespace std;
    
    void quick_sort(int num[], int left, int right)
    {
        int i = left;                 //数列的左下标赋给i
        int j = right;                //数列的右下标赋给j
        int povit = num[(i+j) / 2];   //找特征值
        while ( i <= j)               //当i<j时
        {
            while (num[i] < povit)    //左侧值小于特征点
            {
                i++;                  //右移
            }
            while (num[j]>povit)      //右侧值大于特征点
            {
                j--;                  //左移
            } 
            if (i <= j)               //特征点左右两侧数值对调
            {
                int temp = num[i];
                num[i] = num[j];
                num[j] = temp;
                i++;
                j--;
            }
        }
        if (i < right)                // 例: 1,2,3,4,5,6,8,7状况
        {
            quick_sort(num, i, right);
        }
        if (left < j)                 // 列: 2,1,3,4,5,6,7,8状况
        {
            quick_sort(num, left, j);
        }
    }
  • 相关阅读:
    Python2 cmp() 函数
    Python round() 函数
    Python floor() 函数
    Python ceil() 函数
    Python abs() 函数
    Python oct() 函数
    Python ord() 函数
    Python hex() 函数
    Python2 unichr() 函数
    Android--------工具类StatusBarUtil实现完美状态栏
  • 原文地址:https://www.cnblogs.com/zhuifeng-mayi/p/10658050.html
Copyright © 2011-2022 走看看