intfindthesecond(int *arr,int size) { int max=0; // max int second=0; // second int i=0; for(i=1;i<size;i++){ if(arr[i]>max){ second=max; max=arr[i]; } elseif(arr[i]<max&&arr[i]>second) second=arr[i]; } return second; }
// from the QuickSort intAdjustArray(int *arr,int left, int right){ int i = left,j=right; int x = arr[left]; while (i<j) { while (i<j&&arr[j]>=x) j--; if (i<j) arr[i++]=arr[j]; while (i<j&&arr[i]<x) i++; if (i<j) arr[j--]=arr[i]; } arr[i]=x; return i;// 返回基准数位置 }
// find the n'nd min number intFindnMinNum(int *arr,int left,int right,int n){ int index = AdjustArray(arr,left,right); // the index to divide if (index-left+1 == n) return arr[index]; if (index-left+1 > n) return FindnMinNum(arr,left,index-1,n); else return FindnMinNum(arr,index+1,right,n-(index-left+1)); }
// find the n'nd max number intFindnMaxNum(int *arr,int left,int right,int n){ int index = AdjustArray(arr,left,right); if (right-index+1 == n) return arr[index]; if (right-index+1 < n) return FindnMaxNum(arr,index+1,right,n); else return FindnMaxNum(arr,left,index-1,n-(right-index+1)); }