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

    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    int Partition(int list[], int low, int high)
    {
      int pivotkey = list[low];
      while(low < high)
      {
        while(low<high && list[high]>= pivotkey) 
          high--;
        //找到第一个小于key的记录
        if(low < high) 
          list[low++] = list[high];//相当于交换了list[i]和list[j]
    
        while(low<high && list[low]<= pivotkey) 
          low++; 
        if(low < high) 
          list[high--] = list[low]; 
      }
      list[low] = pivotkey;
      return low;
    
    }
    void QSort(int list[], int low, int high)
    {
      int pivotpos;
      if(low < high)
      {
        pivotpos = Partition(list, low, high);
        QSort(list, low, pivotpos - 1); 
        QSort(list, pivotpos + 1, high); 
      }
    }
    int main(int argc, char *argv[])
    {
      int list[] = {2,52,12,45,86,36,75,33,65,99};
      int i;
      for(i = 0;i < 10;i++)
      {
        printf("%5d",list[i]);
      }
      printf("\n\n");
      QSort(list, 0, 9);
    //out
    
      for(i = 0;i < 10;i++)
      {
        printf("%5d",list[i]);
      }
      system("PAUSE");
      return EXIT_SUCCESS;
    }
  • 相关阅读:
    Eureka的使用
    自定义类加载器
    继承
    Active Objects模式
    Future设计模式
    mysql备份与还原
    多生产者多消费者(第一种方式),基于synchronized,wait,notifyAll
    自己实现阻塞队列(基于数组)
    linux定时任务每隔5分钟向文本追加一行
    自己实现CountDownLatch
  • 原文地址:https://www.cnblogs.com/hbf369/p/2675199.html
Copyright © 2011-2022 走看看