//Memory Time
//464K 47MS
#include<iostream>
using namespace std;
void quicksort(int *s, int left, int right)
{
int i = left, j = right , temp = s[left];
if (i > j)
{
return;
}
while (i != j)
{
while (temp<=s[j]&&j>i)
{
j--;
}
if (i<j)
{
s[i++] = s[j];
}
while (temp>=s[i]&&j>i)
{
i++;
}
if (i<j)
{
s[j--] = s[i];
}
}
s[i] = temp;
quicksort(s,left,i-1);
quicksort(s,i+1,right);
}
int find(int *s, int a)
{
int left,right,mid;
left = 0;
right = 99;
while (left<=right)
{
mid = left + (right - left)/2;
if (s[mid] < a)
{
left = mid + 1;
}
else if (s[mid] > a)
{
right = mid -1;
}
else
{
return mid;
}
}
return left-1;
}
int main()
{
int s[100];
for (int i = 0; i < 100; i++)
{
s[i] = rand();
}
quicksort(s,0,99);
for (int i = 0; i < 100; i++)
{
cout<<s[i]<<" ";
}
cout<<endl;
for (int i = 0; i < 100; i++)
{
cout<<find(s, s[i])<<" ";
}
}
前面输出排序结果,后面输出检索结果,应为0~99