Combine QuickSort and Median selection algorithm

前端 未结 4 716
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 15:18

I want to modify QuickSort (in Java) so that every time Partition is called, the median of the proportioned array is used as the pivot.

I have a median selection algorit

4条回答
  •  执念已碎
    2021-01-25 15:55

    You can use this ...

    int Select(int array[],int start, int end,int k){
    
    if(start==end){
        return start;
    }
    
    int x=array[end];
    int i=start-1;
    for(int j=start;j<=end-1;j++){
        if(array[j]k){
        return Select(array,start,i-1,k);
    }
    else{
        return Select(array,i+1,end,k);
    }
    

    }

    Select will partition array on kth smallest element in array;

提交回复
热议问题