标签:int pivot turn pos floor aaa class sel bsp
int partition2(int[] arr,int Begin,int End,int Pivot) { int i = Begin; int j = End; while(i<j) { while( i<j && arr[j]>=Pivot)--j; while(i<j && arr[i]<= Pivot)++i; swap(arr[i],arr[j]); } //?÷è·partition±???£??úioíi×ó±?μ?êy??D?óúμèóúPivot if(arr[i]>Pivot) i = i-1; return i; } int Select(int[] arr,int Begin,int End, int k) { if(End-Begin+1<75) { sort(arr+Begin,arr+End+1); return arr[Begin+k-1]; } int count = End-Begin+1; int G5 = floor(count/5); for(int i =0;i<G5;++i) { sort(arr+Begin+5*i,arr+Begin+5*i+5); swap(arr[Begin+i],arr[Begin+5*i+2]); } //?ò?D??êy?Dμ??D??êy int x = Select(arr,Begin,Begin+G5-1,G5/2); int pos_x = partition2(arr, Begin, End ,x); int len = pos_x-Begin+1; if(k<=len) { return Select(arr,Begin,pos_x,k); } else { return Select(arr,pos_x+1,End,k-len); } }
标签:int pivot turn pos floor aaa class sel bsp
原文地址:https://www.cnblogs.com/SaraMoring/p/9792104.html