zoukankan      html  css  js  c++  java
  • 对于快速排序的理解

    普通快速排序其实就是递归分割,首先去一个基准数将数组以此为大小分割成两份,然后再递归对此两份数组分别再进行同样的操作。


    int getIndex(int* pArry, int low, int high)
    {
    int temp = pArry[low];
    while (low < high)
    {
    while (low < high && pArry[high] >= temp)
    {
    high--;
    }
    pArry[low] = pArry[high];

    while (low < high && pArry[low] <= temp)
    {
    low++;
    }
    pArry[high] = pArry[low];

    if (high != low)
    {
    std::cout << "low : " << low << " " << "high: " << high << " " << "array : " << pArry[0] << " " << pArry[1] << " " << pArry[2] << " " << pArry[3] << " "
    << pArry[4] << " " << pArry[5] << " " << pArry[6] << " " << pArry[7] << " " << pArry[8] << " " << pArry[9] << endl;
    }
    }
    pArry[low] = temp;
    std::cout << "low : " << low << " " << "high: " << high << " " << "array : " << pArry[0] << " " << pArry[1] << " " << pArry[2] << " " << pArry[3] << " "
    << pArry[4] << " " << pArry[5] << " " << pArry[6] << " " << pArry[7] << " " << pArry[8] << " " << pArry[9] << endl;
    return low;
    }

    int quicksort_ordinary(int* pArry, int low, int high)
    {
    if (low < high)
    {
    int index = getIndex(pArry, low, high);
    quicksort_ordinary(pArry, low, index - 1);
    quicksort_ordinary(pArry, index + 1, high);
    }
    return 0;
    }

    int main(int argc, char* argv[])
    {

    srand(time(nullptr));
    int array[10] = { 0 }; // { 63, 85, 41, 74, 51, 30, 75, 4, 23 };
    for (int i = 0; i < 10; ++i)
    {
    array[i] = rand() % 100 + 1;
    }

    std::cout << "original array : " << array[0] << " " << array[1] << " " << array[2] << " " << array[3] << " "
    << array[4] << " " << array[5] << " " << array[6] << " " << array[7] << " " << array[8] << " " << array[9] << endl;

    quicksort_ordinary(array, 0, 9);


    char t = getchar();
    return 1;
    }

  • 相关阅读:
    Android控件之ListView探究二
    Android控件之Gallery探究
    Android控件之CheckBox、RadioButton探究
    如何解决虚拟机安装centos无法全屏显示问题!
    在Windows下远程桌面连接Linux XManager篇
    putty 中文乱码解决方法
    在IIS上启用Gzip压缩(HTTP压缩)
    LINUX下RPM的使用方法
    虚拟机和主机之间文本的复制和粘贴
    windows上透过Xmanager连接Centos的远程桌面
  • 原文地址:https://www.cnblogs.com/lingqingyu/p/12606409.html
Copyright © 2011-2022 走看看