• C语言实现快排


    #include <stdio.h>
    
    void swap(int *pa, int *pb)
    {
        int t = *pa;
        *pa = *pb;
        *pb = t;
    }
    
    int partion(int *array, int begin, int end)
    {
        if (array == NULL || begin < 0 || end < 0)
            return -1; 
        int pivot = array[end];
        int small = begin -1; 
        for(int i = begin; i < end; i++ ){
            if(array[i] <= pivot) {
                ++small;
                if(small != i){ 
                    swap(&array[small], &array[i]);
                }   
            }   
        }   
        swap(&array[++small], &array[end]);
        return small;
    }
    
    void qsort(int array[], int begin, int end)
    {
        if (end == begin)
            return;
        int index = partion(array, begin, end);
        if (index == -1) 
            return;
        if (index > begin)
            qsort(array, begin, index - 1); 
        if (end > index)
            qsort(array, index + 1, end);
    }
    
    void display(int a[], int n)
    {
        for(int i = 0; i < n; i ++) 
            printf("%d ", a[i]);
        printf("
    ");
    }
    
    int main()
    {
        int a[] = {5, 4, 3, 2,6};
        int n = sizeof(a)/sizeof(a[0]);
        display(a, n); 
        qsort(a, 0, n -1);
        display(a, n); 
    }
    

      

  • 相关阅读:
    约瑟夫问题
    再谈Bellman-Ford
    Uva 11478 Halum操作
    Uva 11090 在环中
    Bellman-Ford
    Uva 10537 过路费
    Uva 10917
    LA 3713 宇航员分组
    2-SAT
    LA 3211 飞机调度
  • 原文地址:https://www.cnblogs.com/moxiaopeng/p/4849848.html
Copyright © 2020-2023  润新知