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

          快速排序算法(quick sort)是对冒泡排序的一种改进,是目前内部排序中速度最快的一种排序方法。

    基本思想是:通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字比另一部分记录的关键字小,则可对这两部分记录分别继续进行排序,以达到整个序列有序。

    选一个关键字分成两部分,不断递归完成。

          整个算法的时间复杂度是:O(nlog2n).

          空间复杂度是:O(logn)

          快速排序是不稳定的排序算法.

          算法的实现代码如下:

    代码实现1:
    1
    #include <stdio.h> 2 3 /**************************************************** 4 **function: print 5 **description: print the element 6 **intput: the array 7 **return: void 8 ****************************************************/ 9 void print(int array[]) 10 { 11 int i; 12 13 for(i = 0; i < 8; i++) 14 { 15 printf("%d ", array[i]); 16 } 17 } 18 /**************************************************** 19 **function: quicksort 20 **description: detach the array into two parts 21 **intput: the array, low, high 22 **return: the position of the key 23 ****************************************************/ 24 int quicksort(int a[], int low, int high) 25 { 26 int key = a[low]; 27 28 while(low < high) 29 { 30 while(low < high && a[high] >= key) high--; 31 a[low] = a[high]; /* exchange the value*/ 32 while(low < high && a[low] < key) low++; 33 a[high] = a[low]; /* exchange the value*/ 34 } 35 a[low] = key; /* the last value*/ 36 return low; 37 } 38 /**************************************************** 39 **function: qsort 40 **description: sort the array 41 **intput: the array, low, high 42 **return: void 43 ****************************************************/ 44 void qsort(int a[], int low, int high) 45 { 46 int pivot; 47 if (low < high) 48 { 49 pivot = quicksort(a, low, high); 50 qsort(a, low, pivot-1); /*deal the first part */ 51 qsort(a, pivot+1, high); /*deal the second part */ 52 } 53 } 54 /* test the code */ 55 void qsorttest(int a[], int low, int high) 56 { 57 qsort(a, low, high); 58 print(a); 59 } 60 61 int main(int argc, char *argv[]) 62 { 63 int array[] = {11, 34, 23, 7, 21, 13, 9, 81}; 64 qsorttest(array, 0, 7); 65 return (0); 66 } 67 68 /**********************end file****************************/

         但上述快速算法的实现略显繁琐,先整合为一个函数,代码如下:

     1 /*快速排序的算法实现2*/
     2 void quicksort(int a[], int left, int right)
     3 {
     4     int low = left;
     5     int high = right;
     6     int key = a[low];
     7 
     8     if (left < right)
     9     {
    10         while (low < high)
    11         {
    12             while (low<high && a[high]>key)
    13                 high--;
    14             a[low] = a[high];
    15 
    16             while (low<high && a[low]<key)
    17                 low++;
    18             a[high] = a[low];
    19         }
    20         a[low] = key;
    21         quicksort(a, left, low);
    22         quicksort(a, low+1, right);
    23     }
    24     
    25 }

    代码二传入了三个参数,也可以只传入两个参数;

     1 /*快速排序的算法实现三*/
     2 void quicksort(int a[], int len)
     3 {
     4     int low = 0;
     5     int high = len-1;
     6     int key = a[low];
     7 
     8     if(len-1 > 0)
     9     {
    10         while(low < high)
    11         {
    12             while(low<high && a[high]>key)
    13                 high--;
    14             a[low] = a[high];
    15 
    16             while(low<high && a[low]<key)
    17                 low++;
    18             a[high] = a[low];
    19         }
    20         a[low] = key;
    21         quicksort(a, low);
    22         quicksort(a+low+1, len-low-1);
    23     }
    24 }

    上述三中算法实现代码原理都一样,仅供参考交流。

  • 相关阅读:
    洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk 题解 结构体排序
    信息学竞赛中C语言的输入输出
    Python 关键字参数和可变参数
    大白话讲解神经网络算法,原理如此简单!
    idea debug flink1.12 sqlClient 源码
    Flink SQL如何保证分topic有序
    Flink 1.12.0 SQL Connector支持 Oracle
    Flink实战之Flink SQL connector支持并行度配置
    java程序中执行脚本时具有的是那个用户的权限呢?
    clickhouse日期等函数的使用
  • 原文地址:https://www.cnblogs.com/philospy/p/4020418.html
Copyright © 2011-2022 走看看