Leftovers algorithm note-BFPRT algorithm

Keywords: less

Solve the problem: Find the k th smallest number in an unordered array

Fast Solution:

  1. Randomly select a number and use the Dutch flag problem to divide the data into less than or equal to more than three pieces
  2. Calculate the length with a well-differentiated array to see if it hit or not, or in which block, continue dividing in the determined block and return to 1.
    For this solution, if the worst case is O(N^2), in general, O (N) is the best one to calculate using the master formula.The final mathematical expectation is O(N).

Violent solutions:

Arrange the entire array in an ordered array and find the k th element.

BFPRT algorithm:

The BFPRT algorithm is different from the fast-queuing algorithm only when the partition value is selected, all the others are the same.
BFPRT algorithm:

  1. Grouping (assuming every five groups, with fewer than five remaining groups)
  2. Sorting within each group after grouping, non-sorting across groups, sorting by five numbers, requires a total time complexity of O(N).
  3. Take out the median of each group to form a new array, where the new array length is N/5 (the last less than five can take the median or the median)
  4. Calls the BFPRT algorithm, in which case the recursive process no longer looks for k items, but selects the median in the middle
  5. Here's how to sort the Dutch flag using the num above
Time complexity of each step:

The first step is complexity O(1),
The second step is complexity O(n),
The third complexity, O(n),
Step 4 T(N/5), find p
Step 5 O(N),
The sixth step determines whether to go right or left/right.
At this point the left and right sizes can be estimated, not unknown.

At this point, you can estimate at least how many numbers are larger or smaller than p.
From the calculation, you can know that N/5 people are selected for the first sorting in a three-time order, and then sorted. When N/10 is larger than the value, and the value larger than the median is added to the original array, so N/10+2N/10=3N/10 at this time, so at most 7N/10 is larger or smaller than p.

code implementation

//Get the k th smallest value using the BFPRT method
public static int getMinKthByBFPRT(int[] arr,int k ){
	int[] copyArr = copyArray(arr);
	//Get the k-1 position in the array where the value is the smallest K value
	return bfprt(copyArr,0,copyArr.length-1,K-1);
}
//bfprt method body
public static int bfprt(int[] arr,int begin,int end,int i){
	if(begin == end){
		return arr[begin];
	}
	//Median of Median
	int pivot = medianOfMedians(arr,begin,end);
	//Divide after median of second round
	int[] privotRange = partition(arr,begin,end,pivot);
	//If the exact i position equals the equal part, it returns
	if(i>= pivotRange[0] && i <= pivotRange[1]){
		return arr[i];
		//i Less than the starting position of the sort
	}else if(i<pivotRange[0]){
		return bgprt(arr,begin,pivotRange[0]-1,i);
	}else{
		//i greater than the end position
		return bfprt(arr,pivotRange[1]+1,end,i);
	}
}
	
public static int medianOfMedians(int[] arr,int begin,int end){
	int num = end - begin +1;
	int offset = num % 5 == 0 ? 0:1;
	int[] marr = new int[num/5+offest];
	for(int i = 0;i<mArr.length;i++){
		int beginI = begin + i*5;
		int endI = beginI +4;
		mArr[i] = getMedian(arr,beginI,Math.min(end,endI));
	}
	return bfprt(mArr,0,mArr.length-1,mArr.length/2);
}

//partition is the issue of realizing the Dutch flag by dividing the three categories greater than or equal to
public static int[] partition(int[] arr,int begin,int end,int privotValue){
	int small = begin -1;
	int cur = begin;
	int big = end+1;
	while(cur != big){
		if(arr[cur]< pivotValue){
			swap(arr,++small,cur++);
		}else if(arr[cur]>pivotValue){
			swap(arr,cur,--big);
		}else{
			cur++;
		}
	}
	//range returns two values, the first being the starting position and the second the ending position of the sort
	int[] range = new int[2];
	range[0] = samll+1;
	range[1] = big-1;
	return range;
}
		
		
20 original articles published. 6% praised. 413 visits
Private letter follow

Posted by imtaqi on Thu, 06 Feb 2020 20:56:49 -0800