风萧28

快排

1.python快排

def get_pivot(li,left,right):
    mid = (left+right)//2
    if li[left] > li[right]:
        li[left],li[right] = li[right],li[left]
    if li[left] > li[mid]:
        li[left],li[mid] = li[mid],li[left]
    if li[mid] > li[right]:
        li[mid],li[right] = li[right],li[mid]
    li[left],li[mid] = li[mid],li[left]
    return li[left]
def partition(li,left,right):
    #tmp = get_pivot(li,left,right)
    tmp = li[left]
    while left <= right:
        while left <= right and tmp < li[right]:
            right -= 1
        li[left] = li[right]
        while left <= right and tmp > li[left]:
            left += 1
        li[right] = li[left]
    li[left] = tmp
    return left
def _quick_sort(li,left,right):
    if left < right:
        mid = partition(li,left,right)
        _quick_sort(li,left,mid)
        _quick_sort(li,mid+1,right)
def quick_sort(li):
    left = 0
    right = len(li)-1
    _quick_sort(li,left,right)
    
def sort_test():
    import random, time,sys
    sys.setrecursionlimit(20000)
    li = []
    for i in range(2000):
        li.append(i)
    random.shuffle(li)
    start = time.time()
    for i in range(1000):
        quick_sort(li)
    print(time.time()-start)
    #print(li)
sort_test()
#336.07243514060974
#7.571660757064819

2.C语言快排

#include <stdio.h> 
#include <stdlib.h>
#include <time.h>

void swap(int *m, int *n){
    int tmp = *m;
    *m = *n;
    *n = tmp;
}

void shuffle(int *array, int n)
{
    srand((int)time(NULL));
    int i;
    for (i=0;i<n;i++){
        int tmp = rand()%n;
        swap(&array[0],&array[tmp]);
    }
}
int get_pivot(int *array, int left, int right){
    int mid = (left+right)/2;
    if (array[left] > array[right]){
        swap(&array[left],&array[right]);
    }
    if (array[left] > array[mid]){
        swap(&array[left],&array[mid]);
    }
    if (array[mid] > array[right]){
        swap(&array[mid],&array[right]);
    }
    swap(&array[left],&array[mid]);
    return array[left];
}


int partition(int *array, int left, int right){
    int tmp = array[left];
    //int tmp = get_pivot(array,left,right);
    while (left <= right){
        while (left <= right && tmp < array[right]){
            right--;
        }
        array[left] = array[right];
        while (left <= right && tmp > array[left]){
            left++;
        }
        array[right] = array[left];
    }
    array[left] = tmp;
    return left;
}

void _quick_sort(int *array,int left,int right){
    if (left < right){
        int mid = partition(array,left,right);
        _quick_sort(array, left, mid);
        _quick_sort(array, mid+1, right);
    }
}
void quick_sort(int *array, int n){
    int left = 0;
    int right = n-1;
    _quick_sort(array,left,right);
}

int main(){
    int array[2000] = {0};
    int size = sizeof(array)/sizeof(array[0]);
    int i,j,k;
    for (i=0;i<size;i++){
        array[i] = i;
    }
    shuffle(array, size);
    clock_t start,end;
    start = clock();
    for (k=0;k<1000;k++){        
        quick_sort(array,size);
    }
    end = clock();
    printf("%f\n",start);
    printf("%f\n",end);
    printf("%f",(float)(end-start)/CLK_TCK);

    for (j=0;j<size;j++){
    //    printf("%d  ",array[j]);
    }
}
//0.153
//5.903

运行时间:python是C的50-60倍

posted on 2019-06-17 21:25  小流江海  阅读(260)  评论(0编辑  收藏  举报

导航