One-dimensional closest point crashes too much data

< H1 > include < iostream > < / H1 > < H1 > include < time.h > < / H1 > < H1 > include < stdlib.h > < / H1 > < H1 > include < math.h > < / H1 >

using namespace std;
/ / problem: partition function does not work effectively;
typedef double Type;
typedef struct Pair1 {

Type a;
Type b;
Type dis;

} Pair1;

typedef struct Pair2 {

Type x1;
Type y1;
Type x2;
Type y2;
Type dis;

} Pair2;
Type random_data ();
Pair1 closest_pair1 (Type s [], int pminint r);
void bubble_sort (Type a [], int preint r);
Type linear_select (Type a [], int precinct int k);
int linear_partition (Type a [], int low,int high,Type k);

int Partition (Type a [], int pcent int type k);
void swap_element (Type a [], int irect int j);
void show (Type a [], int n);
void quick_sort (Type a [], int low,int high);
int quick_partition (Type a [], int low,int high);
Type Min (Type s [], int pdint r);
Type Max (Type s [], int pcent int r);
/ / crash occurs when there is too much data
int main ()
{

srand(time(0));
int n=200;
Type s[n];
for(int i=0;i<n;iPP)
    s[i]=random_data();
//show(s,n);

Pair1 d=closest_pair1(s,0,n-1);
cout<<":"<<d.dis<<endl<<":"<<d.a<<""<<d.b<<endl;
return 0;    

}

Pair2 closest_Pair2 ()
{

}

Type linear_select (Type a [], int pcent int rjint k)
{


if(r-p<20)
{
    bubble_sort(a,p,r);
    return a[p+k-1];
}

int i,j;
for(i=0;i<=(r-p-4)/5;iPP)
{
    int s=p+5*i,t;
    t=s+4;
    bubble_sort(a,s,t);
    swap_element(a,p+i,s+2);//i a[p+i]; 
}

Type x=linear_select(a,p,p+(r-p-4)/5,(r-p+6)/10);// (r-p+6)/10 a[p,p+(r-p-4)/5]; 

i=linear_partition(a,p,r,x);// 
j=i-p+1;//+;
 
//;     
if(k==j)
    return a[j];
else 
{
    if(k<j) 
        return    linear_select(a,p,i-1,k);
    else
        return linear_select(a,i+1,r,k-j);
}

}

int linear_partition (Type a [], int low,int high,Type k)
{

swap_element(a,low,low+(high-low-4)/10);

Type pivotkey=a[low];
while(low<high)
{
    while(high>low&&a[high]>=pivotkey)
        high--;
    a[low]=a[high];
    while(high>low&&a[low]<=pivotkey)
        lowPP;
    a[high]=a[low];
}
a[low]=pivotkey;
return low;

}

void swap_element (Type a [], int ijint j)
{

Type tmp=a[i];
a[i]=a[j];
a[j]=tmp;

}
void bubble_sort (Type a [], int pjingint r)
{

int i,j;
Type tmp;
for(i=0;i<r-p;iPP)
    for(j=p;j<r-i;jPP)
        if(a[j]>a[j+1])
        {
            tmp=a[j];
            a[j]=a[j+1];
            a[j+1]=tmp;
        }    

}
void show (Type a [], int n)
{

for(int i=0;i<n;iPP)
    cout<<a[i]<<" ";
cout<<endl;

}
int Partition (Type a [], int low,int high,Type k)
{

int tmp=low;
while(a[tmp]!=k)
{
    if(a[tmp]==k)
        break;
    tmpPP;
}
cout<<"a["<<tmp<<"]="<<a[tmp]<<endl;
swap_element(a,tmp,low);

Type pivotkey=a[low];
while(low<high)
{
    while(high>low&&a[high]>=pivotkey)
        high--;
    a[low]=a[high];
    while(high>low&&a[low]<=pivotkey)
        lowPP;
    a[high]=a[low];
}
a[low]=pivotkey;
return low;

}

Pair1 closest_pair1 (Type s [], int pcent int r)
{

Pair1 d;
d.a=0;d.b=0;d.dis=65535;
if(r-p<=0)//; 
    return d;
if(r-p==1)//; 
{
    d.a=s[p];
    d.b=s[r];
    d.dis=fabs(s[p]-s[r]);
    return d;
}  
Type x=linear_select(s,p,r,(r-p)/2+1); 
int pivot_loc=Partition(s,p,r,x);//sxss1 s2; 

//cout<<"x="<<x<<"s["<<pivot_loc<<"]="<<s[pivot_loc]<<endl;

Pair1 d1,d2,d3;
d1=closest_pair1(s,p,pivot_loc);
d2=closest_pair1(s,pivot_loc+1,r);

Type min,max;
max=Max(s,p,pivot_loc);//s1; 
min=Min(s,pivot_loc+1,r);//s2;

//cout<<"max="<<x<<"min="<<min<<endl;
d3.a=min;
d3.b=max;
d3.dis=fabs(max-min);//d3s1 s2; 

if(d1.dis<d2.dis)
{
    if(d1.dis<d3.dis)
        return d1;
    else
        return d3;
}
else 
{ 
    if(d2.dis<d3.dis)
        return d2;
    else 
        return d3;
} 

}
Type random_data ()
{

Type num1=(Type) (rand()%101)/101;
int num2=rand()%10000+1;
return num1+num2;

}

Type Min (Type s [], int pcent int r)
{

Type num=s[p];
for(int i=p+1;i<=r;iPP)
    if(num>s[i])
        num=s[i];

/ / cout < < "min=" < < num < < endl;

return num;

}

Type Max (Type s [], int pcent int r)
{

Type num=s[p];
for(int i=p+1;i<=r;iPP)
    if(num<s[i])
        num=s[i];

/ / cout < < "max=" < < num < < endl;

return num;

}

C cPP
Mar.04,2021
Menu