The k-th smallest number bfprt algorithm and the improvement of fast sorting

On the k-th smallest number O(N)

Improve the fast sorting idea O(N) In the worst case, the complexity is O(n^2) (when the sorting is close to order)

public static int findk(int[] nums,int m,int n ,int k)
	{
    
    
		int temp=nums[m];
		int i=m;
		int j=n;
		
		while(i<j)
		{
    
    
			while(i<j&&nums[j]>=temp)
			{
    
    
				j--;
			}
			while(i<j&&nums[i]<=temp)
			{
    
    
				i++;
			}
			if(i<j)
			{
    
    
				int t=nums[i];
				nums[i]=nums[j];
				nums[j]=t;
			}
		}
		nums[m]=nums[j];
		nums[j]=temp;
		
		if(j+1==k)
		{
    
    
			return nums[j];
			
		}
		else if(j+1>k){
    
    
			return findk(nums, m, j-1, k);
		}
		else {
    
    
			return findk(nums, j+1, n, k-j-1);
		}
	}

	
	public static void main(String[] args) {
    
    
		int[] nums=new int[]{
    
    8,3,6,9,2};
		System.out.println(findk(nums, 0, nums.length-1, 4));
		
 	}

The bfprt algorithm (the initials of the five MIT big cows) has a complexity of O(n) in the worst case.

  1. Randomly choose a number m (the difference between bfprt and the above is here,bfprt chooses a number carefully and eliminates as many numbers as possible
  2. <m put on the left m >m put on the right
  3. Hit return, if miss, choose left and right to continue recursive

How to choose the number

  1. First press the array5The numbers are grouped as a group, and the last less than 5 is ignored. Sort each group of numbers (such as insertion sort) to find the median. O(N)
  2. Move all the medians from the previous step to the front of the array, and recurse on these mediansCall the BFPRT algorithm to find their median.

If you need to get the median of m=[3 , 2, 3 ],
find the second
smallest number bfprt(m, 2)

  1. Use the median obtained in the previous step as the pivot of the division to divide the entire array.
public static int bfprt_choose(int[] nums,int m,int n)
	{
    
    
		int l=nums.length%5==0?nums.length/5:nums.length/5+1;
		int[] q=new int[l];
		int u=0;
		for(int i=m;i<=n;i++)
		{
    
    
			if(i+4<=n)
			{
    
    
				q[u]=numsort(nums,i,i+4);
				u+=1;
				i=i+5;
								
			}
			else {
    
    
				q[u]=numsort(nums,i,n);
			
			}
		}
		return findk(q, 0, q.length-1, q.length/2);
	}
	

Where is the best choice?

Fixed off

N 10 (median) + 2 ∗ N 10 = 3 N 10 numbers\frac{N}{10}(median)+2*\frac{N}{10}=\frac{3N}{10 } Number 10N( Middle position number )+210N=103 NA number

The worst complexity is O(N)

T ( N ) = O ( N ) + T ( N / 5 ) + O ( 3 N / 10 ) T ( N ) = O ( N ) T(N)=O(N)+T(N/5)+O(3N/10) T(N)=O(N) T(N)=O ( N )+T(N/5)+O(3N/10)T(N)=O ( N )

master official

T(N) = aT(N/b) + O(N^d)

b: the sample size of the
sub-process a: the number of calculations of the sub-process
O(N^d): the time complexity of sub-result merging
programs that satisfy the above formula can calculate the time complexity according to the master formula:

log(b,a)> d: the time complexity is O(N^log(b,a))
log(b,a) = d: the time complexity is O(N^d * logN)
log(b,a ) <d: The time complexity is O(N^d)

Algorithms create an era

Write the time complexity expression directly, you can deduce the final time expression of the algorithm, and see how this algorithm is worth studying.

Guess you like

Origin blog.csdn.net/hch977/article/details/112312855