zoukankan      html  css  js  c++  java
  • quick sort O(logn)

    #include <stdio.h>
    
    int partition(int* p, int low, int high);
    void Qsort(int* p, int low, int high);
    void quickOrder(int* p, int len);
    int main()
    {
            int array[10];
            int len;
            len = input(array);
    
            quickOrder(array, len);
            for(int i =0; array[i] != '\0'; i++) {
                    printf("number is:%d\r\n", array[i]);
            }
            return 0;
    }
    int input(int* array)
    {
            int n;
            int i = 0;
            while(1) {
                    scanf("%d", &n);
                    if(n == -1) break;
                    *array = n;
                    array++;
                    i++;
            }
    
            return i;
    }
    
    int partition(int* p, int low, int high)
    {
            int pivotkey = *(p+low);
            while(low < high) {
                    while(low < high && *(p+high) >= pivotkey)  --high;
                    *(p+low) = *(p+high);
                    while(low < high && *(p+low) <= pivotkey) ++low;
                    *(p+high) = *(p+low);
    
            }
            *(p+low) = pivotkey;
            return low;
    
    }
    void Qsort(int* p, int low, int high)
    {
            if(low < high) {
                    int pivotloc =  partition(p, low, high);
                    Qsort(p, low, pivotloc-1);
                    Qsort(p, pivotloc+1,high);
            }
    }
    void quickOrder(int* p, int len)
    {
            Qsort(p, 0, len-1);
    }
  • 相关阅读:
    Python 学习目录
    Django目录
    SQLAlchemy
    Flask之Sqlalchemy
    Websocket
    Mongodb
    虚拟环境
    Github
    LINUX
    内存管理和垃圾回收机制
  • 原文地址:https://www.cnblogs.com/mysqlinternal/p/2955850.html
Copyright © 2011-2022 走看看