zoukankan      html  css  js  c++  java
  • 八种排序整理(四)----快速排序

    基本概念:快速排序是一种非常高效的排序方法,采用“分而治之”的思想,把大的拆分为小的,小的在拆分为更小的。

    原理是:对于一组给定的记录,通过一趟排序后,将原序列分为两部分,其中前部分的所有记录均比后部分的所有记录

    小,然后再依次对前后两部分的记录进行快速排序,递归该过程,直到序列中的所有记录均为有序为止。

    快速排序特点:

    稳       定       性:不稳定。

    平均时间复杂度: O(nlog2n)。

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 void Sort(int array[], int low, int high)
     6 {
     7     int i, j;
     8     int index;
     9 
    10     if (low >= high)
    11     {
    12         return;
    13     }
    14 
    15     i = low;
    16     j = high;
    17     index = array[i];
    18 
    19     while (i < j)
    20     {
    21         while (i < j && array[j] >= index)
    22         {
    23             j--;
    24         }
    25         if (i < j)
    26         {
    27             array[i++] = array[j];  //互换位置
    28         }
    29         while (i < j && array[i] < index )
    30         {
    31             i++;
    32         }
    33         if (i < j)
    34         {
    35             array[j--] = array[i];  //互换位置
    36         }
    37     }
    38     array[i] = index;
    39     Sort(array, low, i - 1);
    40     Sort(array, i + 1, high);
    41 }
    42 
    43 void QuickSort(int array[], int length)
    44 {
    45     Sort(array, 0, length - 1);
    46 }
    47 
    48 int main()
    49 {
    50     int i = 0;
    51     int a[] = {29, 96, 18, 56, 3, 56, 39, 77};
    52     int length = sizeof (a) / sizeof(a[0]);
    53 
    54     QuickSort(a, length);
    55 
    56     for (i = 0; i < length; i++)
    57     {
    58         printf("%d ", a[i]);
    59     }
    60     printf("
    ");
    61     while(1);
    62 
    63     return 0;
    64 }
  • 相关阅读:
    delphi 控制音量 静音的类
    delphi java 日期 转换 获取Unix时间戳
    UI颜色值
    ios10 no route to host
    VMWare MAC系统调整磁盘
    手机传文字到电脑
    Delphi JCL JEDI使用 jclDebug
    PS 使用首记 修改png图片的颜色
    delphi Style TBitmapLink
    delphi IOS 获取电池信息
  • 原文地址:https://www.cnblogs.com/kutoli/p/8329249.html
Copyright © 2011-2022 走看看