leetcode-剑指40-OK

address

// 低效率算法
int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize){
////排序算法
    int partition(int *mm,int low, int high){
        int pivot = mm[low];
        while (low <high){
            while(low <high && mm[high] >=pivot)   high--;
            mm[low] = mm[high];
            while(low <high && mm[low] <= pivot)   low++;
            mm[high] = mm[low];
        }
        mm[low] = pivot;
        return low;
    }
    void paixu(int *mm, int start, int end){
        int pivot = partition(mm,start,end);
        if (start < pivot-1) paixu(mm, start, pivot-1);
        if (end > pivot+1) paixu(mm, pivot+1, end);
    }
//////////////////
    returnSize[0] = k;
    if(k == 0)
        return arr;
    int *a = (int *) malloc(sizeof(int) * k);
    int i = 0;
    int j;
    for(i = 0; i< k; i++)
        a[i] = arr[i];
    paixu(a,0,k-1);
    for(i = k; i<arrSize; i++){
        if(arr[i]<a[k-1]){  //找到位置插进去
            for(j = k-2; j >=0;j--){
                if(arr[i]<a[j]){
                    a[j+1] = a[j];
                }else{
                    break;
                }
            }
            a[j+1] = arr[i];
        }
    }
    return a;
}

// 直接快排,效率还行
int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize){
////排序算法
    int partition(int *mm,int low, int high){
        int pivot = mm[low];
        while (low <high){
            while(low <high && mm[high] >=pivot)   high--;
            mm[low] = mm[high];
            while(low <high && mm[low] <= pivot)   low++;
            mm[high] = mm[low];
        }
        mm[low] = pivot;
        return low;
    }
    void paixu(int *mm, int start, int end){
        int pivot = partition(mm,start,end);
        if (start < pivot-1) paixu(mm, start, pivot-1);
        if (end > pivot+1) paixu(mm, pivot+1, end);
    }
//////////////////
    returnSize[0] = k;
    if(k == 0)
        return arr;
    paixu(arr,0,arrSize-1);
    return arr;
}
// 使用堆排序
// 每次把最小的推到堆顶,搞k次
// 效率比较高
int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize){
    returnSize[0] = k;
    if(k == 0)
        return arr;
    int mid[k+1];
    for(int i = 0; i<k; i++)
        mid[i+1] = arr[i];
// 堆调整以及建堆的函数
    void head_adjust(int arr[], int k, int size_adjust){
        arr[0] = arr[k];
        int i;
        for (i=2*k ; i <= size_adjust ; i*=2){
            if (i+1<= size_adjust && arr[i] <arr[i+1])
                i +=1;
            if (arr[0]>= arr[i])
                break;
            else{
                arr[k] = arr[i];
                k = i;
            }
        }
        arr[k] = arr[0];
    }

    void build_max_heap(int *arr, int size){
        for (int i= size/2; i>0; i--){
            head_adjust(arr, i, size);
        }
    }
//--------------------------------------------------
    build_max_heap(mid,k);
    for(int i =k; i<arrSize; i++){
        if(arr[i]<mid[1]){
            mid[1] = arr[i];
            head_adjust(mid,1,k);
        }
    }
    for(int i = 0; i<k; i++)
        arr[i] = mid[i+1];
    return arr;
}
posted @ 2021-01-24 03:57  RougeBW  阅读(41)  评论(0编辑  收藏  举报