# 数组中出现次数超过一半的数字

#include <iostream>
using namespace std;

/*

*/

int Partition(int *A,int low,int high)
{
int pivotkey=A[low];
while (low<high)
{
while (low<high&&A[high]>=pivotkey)
{
high--;
}
A[low]=A[high];
while (low<high&&A[low]<=pivotkey)
{
low++;
}
A[high]=A[low];
}
A[low]=pivotkey;
return low;
}

bool confirmNum(int *array,int number,int len)
{
int time=0,i;
for (i=0;i<len;i++)
{
if (array[i]==number)
{
time++;
}
}
if (time*2>len)
{
return true;
}
else
return false;
}

void MoreThanHalfArray(int *array,int len)
{
int middle=len/2;
int index,start=0,end=len-1;
index=Partition(array,start,end);
while (index!=middle)
{
if (index>middle)
{
end=index-1;
index=Partition(array,start,end);
}
else
{
start=index+1;
index=Partition(array,start,end);
}
}
int result=array[middle];
if (confirmNum(array,result,len))
{
cout<<"exsit this element is "<<result<<endl;
}
else
}

/*

*/

void MoreThanHalf(int *array,int len)
{
int i;
int result,time=0;
for (i=0;i<len;i++)
{
if (time==0)
{
result=array[i];
time=1;
}
else if (array[i]==result)
{
time++;
}
else
time--;
}
if (confirmNum(array,result,len))
{
cout<<"exsit this element is "<<result<<endl;
}
else
}