赞
踩
- int findCount(int a[],int len ,int key)
- {
- int i,count = 0;
- for(i=0;i<len;i++)
- {
- if(key==a[i])
- count++;
- }
- return count;
- }
- int BinarySort(int a[],int len, int key, bool isLeft)
- {
- int left = 0, right = len -1;
- int last = 0 ; // 记录下标
- while(left<=right)
- {
- int mid = (left + right)/2;
- if(a[mid]<key)
- {
- left = mid + 1;
- }
- else if(a[mid]>key)
- {
- right = mid -1;
- }
- else
- {
- last = mid ;
- if(isLeft) // 该值在mid左边还有时
- {
- right = mid - 1;
- }
- else
- {
- left = mid + 1;
- }
- }
- }
- // if (last>0)
- // return last;
- // else
- // return -1;
- return last>0?last:-1;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。