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

     1 /**
     2  * 快速排序
     3  * 博客园:狗剩的美丽家园
     4  * 2017年12月26日
     5  */
     6 
     7 #include <iostream>
     8 #include <algorithm>
     9 
    10 using namespace std;
    11 
    12 void Swap(int &p, int &q)
    13 {
    14     int temp = p;
    15     p = q;
    16     q = temp;
    17 }
    18 int Partition(int data[], int start, int end)
    19 {
    20     int temp = data[end];
    21     int i = start - 1;
    22     for (int j = start; j < end; j++) {
    23         if (data[j] <= temp) {
    24             ++i;
    25             if (i != j) {
    26                 Swap(data[i], data[j]);
    27             }
    28         }
    29     }
    30     ++i;
    31     Swap(data[i], data[end]);
    32     return i;
    33 }
    34 
    35 void QuickSort(int data[], int start, int end)
    36 {
    37     if (start == end) return;
    38     int index = Partition(data, start, end);
    39     if (start < index) {
    40         QuickSort(data, start, index - 1);
    41     }
    42     if (end > index) {
    43         QuickSort(data, index + 1, end);
    44     }
    45 }
    46 
    47 
    48 int main()
    49 {
    50     int data[] = {2, 8, 7, 1, 3, 5, 6, 4};
    51     int start = 0;
    52     int end = sizeof(data) / sizeof(data[0]) - 1;
    53     QuickSort(data, start, end);
    54     for (int i = start; i < end + 1; i++) {
    55         cout << data[i] << ",";
    56     }
    57 }
  • 相关阅读:
    3.10 Go Map哈希表
    3.9 Go Slice切片
    3.8 Go Array数组
    3.7 Go指针
    3.6 Go String型
    3.5 Go布尔型
    3.4 Go字符型
    3.3 Go浮点型
    3.2 Go整数类型
    3.1Go变量
  • 原文地址:https://www.cnblogs.com/gousheng/p/8116843.html
Copyright © 2011-2022 走看看