main.cpp
Upload User: ledyuyuhua
Upload Date: 2018-05-03
Package Size: 3k
Code Size: 9k
Development Platform:

Visual C++

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <math.h>
  5. static unsigned long jz,jsr=123456789;
  6. #define SHR3 (jz=jsr, jsr^=(jsr<<13), jsr^=(jsr>>17), jsr^=(jsr<<5),jz+jsr)
  7. #define UNI (.5 + (signed) SHR3*.2328306e-9)
  8. #define IUNI SHR3
  9. static long hz;
  10. static unsigned long iz, kn[128], ke[256];
  11. static float wn[128],fn[128], we[256],fe[256];
  12. #define RNOR (hz=SHR3, iz=hz&127, (fabs(hz)<kn[iz])? hz*wn[iz] : nfix())
  13. #define REXP (jz=SHR3, iz=jz&255, (    jz <ke[iz])? jz*we[iz] : efix())
  14. #define ranf() 
  15.   ((float)rand()/(1.0+(float)RAND_MAX)) // Uniform from interval [0,1)
  16. using namespace std;
  17. void make_array(int);
  18. int binary_search(int *,int,int);
  19. int exp_interpolation_search(int *,int,int);
  20. float gauss();
  21. float efix();
  22. void zigset(unsigned long);
  23. void quicksort(int *A,int m);
  24. void taxytaksinomisi(int *A,int p, int r);
  25. int diamerisi(int *A, int p, int r);
  26. int count=0;
  27. int main(){
  28. int size;
  29. cout << "Dose megethos pinakan";
  30. cin >> size;
  31. make_array(size);
  32. }
  33. void make_array(int size) {
  34.   int i,katanomi,search;
  35.   int found;
  36.   int x;
  37.   int *array=new int[size];
  38.   srand(time(0));
  39.   cout << "Diale3e katanomi:n1.Gaussian katanomin2.Omoiomorfin3.Ekthetikin";
  40.   cin >> katanomi;
  41.   if (katanomi==1)
  42.     for (i=0; i<size; i++)
  43.       array[i] = (gauss()+5)*1000;
  44.   else if (katanomi==2)
  45.     for (i=0; i<size; i++)
  46.       array[i] = (float)(rand()%1000000) +1 ;
  47.   else if (katanomi==3)
  48.     { zigset(86947731);
  49.       for (i=0; i<size; i++)
  50.         array[i] = efix()*100000;
  51.     }
  52.   //for (i=0; i<size; i++)
  53.     //cout << array[i] << " ";
  54.   cout << "n";
  55.   quicksort(array,size);
  56.   cout << "n";
  57.   for (i=0; i<size; i++)
  58.     cout << array[i] << " ";
  59.   cout << "n";
  60.   cout << "Dose to stoixeio gia euresin";
  61.   cin >> x;
  62.   cout <<"Diale3e searchn1.Binary Searchn2.Interpolation Searchn";
  63.   cin >> search;
  64.   if (search==1)
  65.    {  found = binary_search(array,x,size);
  66.         if (found!=0) 
  67.   { cout << "To stoixeio vre8ike sti 8esi " << found<<endl; 
  68.     cout << "O arithmos ton sigkriseon = "<<count<<endl; }
  69.         else cout << "To stoixeio den vre8ike";
  70.       cout << "n"; }
  71.   else if (search==2)
  72.    {
  73.   found = exp_interpolation_search(array,x,size);
  74.         if (found!=0) 
  75.   { cout << "To stoixeio vre8ike sti 8esi " << found<<endl; 
  76.     cout << "O arithmos ton sigkriseon = "<<count<<endl; }
  77.       else cout << "To stoixeio den vre8ike";
  78.       cout << "n"; }
  79. }
  80. void quicksort(int *A,int m) {
  81.      cout << "nquickn";
  82.      taxytaksinomisi(A,0,m-1);
  83. }
  84. void taxytaksinomisi(int *A,int p, int r) {
  85.       int q;
  86.       if (p<r) {
  87.           q=diamerisi(A, p, r);
  88.           taxytaksinomisi(A,p,q-1);
  89.           taxytaksinomisi(A,q+1,r);
  90.       }
  91. }
  92. int diamerisi(int *A, int p, int r) {
  93.       int x=A[r];
  94.       int i=p-1;
  95.       int t;
  96.       for(int j=p; j<r; j++) {
  97.           if (A[j]<=x) {
  98.              i++;
  99.              t=A[j];
  100.              A[j]=A[i];
  101.              A[i]=t;
  102.            }
  103.       }
  104.       t=A[r];
  105.       A[r]=A[i+1];
  106.       A[i+1]=t;
  107.       return i+1;
  108. }
  109. int binary_search(int *array,int x,int size) {
  110.   int left,right,i,mikos;
  111.   mikos=size;
  112.   left=0;
  113.   right=mikos;
  114.   while(mikos!=0) {
  115.     mikos=(right-left)/2;
  116. count++;
  117. if (x==array[left+mikos])
  118.   return left+mikos+1;
  119.     else 
  120.   if (x>array[left+mikos]) 
  121.     { count++; left=left+mikos; }
  122.   else 
  123.     { count++; right=right-mikos; }
  124.   }
  125.   return 0;
  126. }
  127. int exp_interpolation_search(int *array,int x,int size) {
  128.   int left,left2=0,right,right2,next,i=1,j,y,d=0,k,root_size,mikos;
  129.   bool out_of_bounds=false,out_of_bounds2=false,found=false;
  130.   //elegxos gia size<=3
  131.   if (size<=3)
  132.    { for (j=0; j<size; j++)
  133.    if (array[j]==x) 
  134.      { count++; return j+1; }
  135.      return 0; }
  136.   root_size=(int)sqrt((float)size);
  137.   int *array3 = new int[root_size];
  138.   //euresi next
  139.   left=0;
  140.   right=size-1;
  141.   if (x==array[right])
  142.     next=size;
  143.   else
  144.   next=size*((float)(x-array[left])/(array[right]-array[left]))+1;
  145.   
  146.   if ((next>size)||(next<=0))
  147.   return 0;
  148.   //euresi diastimatos pou vrisketai to x
  149.   cout << "next:" << next << "n";
  150.   count++;
  151.   if (x==array[next-1])
  152.     return next;
  153.   else
  154.     if (x>array[next-1]) {
  155.   if (next-1+i*root_size-1>=size)
  156.     { count++; i=1; out_of_bounds=true; }
  157.   else
  158.     { count++;
  159.           while (x>array[next-1+i*root_size-1]) {
  160.            i=2*i;   
  161.     count++;
  162.     if (next-1+i*root_size-1>=size) 
  163.   { out_of_bounds=true; break; }
  164.       }
  165.     }
  166.       right=next+i*root_size;
  167.   left=next+(i/2)*root_size; 
  168. }
  169. else {
  170.   if (next-1-i*root_size+1<0)
  171.     { count++; i=1; out_of_bounds2=true; }
  172.   else
  173.     { count++;
  174.       while (x<array[next-1-i*root_size+1]) {
  175.         i=2*i;
  176.     count++;
  177.             if (next-1-i*root_size+1<0)
  178.   { out_of_bounds2=true; break; }
  179.       }
  180.     }
  181.   right=next-(i/2)*root_size;
  182.   left=next+1-i*root_size;
  183.   //elegxos otan to alma einai megalitero tou size
  184.   if (out_of_bounds) {
  185. //euresi megistou ipodiastimatos pou vgainei out of bounds
  186.     for (j=1; j<(i/2); j++)
  187.   if ((left+j*root_size)>size) break;
  188. //euresi ean iparxei ipodiastima riza(n)
  189. for (k=1; k<j; k++)
  190.   if (x<array[left+k*root_size])
  191.     { count++; found=true; break; }
  192.     if (found)
  193.    left2=(k-1);
  194. else
  195.   { for (j=left-1+(k-1)*root_size; j<size; j++)
  196.       if (array[j]==x) 
  197.         return j+1; 
  198.     return 0; } 
  199.   }
  200.   //elegxos otan to alma einai mikrotero tou 0
  201.   else if (out_of_bounds2) {
  202.     //euresi elaxistou ipodiastimatos pou vgainei out of bounds
  203.     for (j=1; j<(i/2); j++)
  204.   if ((right-j*root_size<0)) break;
  205. //euresi ean iparxei ipodiastima riza(n)
  206. for (k=1; k<j; k++)
  207.   if (x>array[right-k*root_size])
  208.     { count++; found=true; break; }
  209.     if (found)
  210.   { cout<< k<<endl;
  211.     left2=0;
  212. left=right-k*root_size+1;
  213.   }
  214. else
  215.   { left=0;
  216. for (j=left; j<(right-(k-1)*root_size+1); j++)
  217.   if (array[j]==x) 
  218.   return j+1; 
  219.     return 0; }
  220.   }
  221.   //dimiourgia pinaka gia binary search
  222.   else {
  223.     mikos=i/2;
  224.     //otan to i!=1(gia diastimata pou mporoun na spasoun 
  225.     //             se riza(n) ipodiastimata)
  226.     if (i!=1) {
  227.       int *array2 = new int[mikos];
  228.       k=0;
  229.       for (j=0; j<mikos; j++)
  230.     { array2[j]=array[left-1+k*root_size];
  231.           k++; }
  232.       for (k=0; k<mikos; k++)
  233.     cout << array2[k]<< " ";
  234.       cout << "n";
  235.       //binary search(euresi ipodiastimatos riza(n))
  236.       left2=0;
  237.       right2=mikos;
  238.       while(mikos!=1) {
  239.         mikos=(right2-left2)/2;
  240.     count++;
  241.         if (x==array2[left2+mikos])
  242.       return (left2+mikos)*root_size;
  243.         else 
  244.       if (x>array2[left2+mikos]) 
  245.     left2=left2+mikos;
  246.       else right2=right2-mikos;
  247.   }
  248.   }  
  249.     //otan i=1(den mporei na spasei se pairetero ipodiastimata)
  250.     else
  251.       {left2=0; right2=0;}
  252.   }
  253.   //dimiourgia neou pinaka mege8ous riza(n) gia anadromiki klisi
  254.   for (j=0; j<root_size; j++)
  255.   array3[j]=array[left-1+left2*root_size+j];
  256.   for (j=0; j<root_size; j++)
  257.       cout << array3[j]<< " ";
  258.   
  259. y=exp_interpolation_search(array3,x,root_size); 
  260.   if (y==0)  
  261.     return 0;
  262.   else
  263.     if (i==1)
  264.   return left-1+y;
  265. else
  266.   return left-1+left2*root_size+y;
  267. }
  268. float gauss(){                     
  269.       float x1, x2, w, y1; 
  270.       static float y2; 
  271.       static bool y2next=false;
  272.       if (y2next==true){ y2next=false; 
  273.          y2 = y2*10000;
  274.          y2 = floor(y2 + 0.5); 
  275.          return y2/10000;      
  276.       return y2; }
  277.          do {
  278.                  x1 = 2.0 * ranf() - 1.0;
  279.                  x2 = 2.0 * ranf() - 1.0;
  280.                  w = x1 * x1 + x2 * x2;
  281.          } while ( w >= 1.0 );
  282.          w = sqrt( (-2.0 * (log(w)/log(exp((float)1))) ) / w );
  283.          y1 = x1 * w;
  284.          y2 = x2 * w;
  285.          y2next=true;
  286.            y1 = y1*10000;
  287.            y1 = floor(y1 + 0.5); 
  288.          return y1/10000;
  289. }
  290. float efix(void)
  291. { float x;
  292.  for(;;)
  293.   {  if(iz==0) return (7.69711-log(UNI));          /* iz==0 */
  294.      x=jz*we[iz]; if( fe[iz]+UNI*(fe[iz-1]-fe[iz]) < exp(-x) ) return (x);
  295.       /* initiate, try to exit for(;;) loop */
  296.    jz=SHR3;
  297.    iz=(jz&255);
  298.    if(jz<ke[iz]) return (jz*we[iz]);
  299.   }
  300. }
  301. void zigset(unsigned long jsrseed)
  302. {  const double m1 = 2147483648.0, m2 = 4294967296.;
  303.    double dn=3.442619855899,tn=dn,vn=9.91256303526217e-3, q;
  304.    double de=7.697117470131487, te=de, ve=3.949659822581572e-3;
  305.    int i;
  306.    jsr^=jsrseed;
  307. /* Set up tables for REXP */
  308.     q = ve/exp(-de);
  309.     ke[0]=(de/q)*m2;
  310.     ke[1]=0;
  311.     we[0]=q/m2;
  312.     we[255]=de/m2;
  313.     fe[0]=1.;
  314.     fe[255]=exp(-de);
  315.    for(i=254;i>=1;i--)
  316.   {de=-log(ve/de+exp(-de));
  317.    ke[i+1]= (de/te)*m2;
  318.    te=de;
  319.    fe[i]=exp(-de);
  320.    we[i]=de/m2;
  321.   }
  322. }