|
| 1 | +/* |
| 2 | + MIT License |
| 3 | +
|
| 4 | +Copyright (c) [2019] [Med Ezzeddine Macherki] |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | + |
| 26 | +#include "insertSort.h" |
| 27 | +#include "lsd.h" |
| 28 | + |
| 29 | + |
| 30 | +/* DONE |
| 31 | + * more effecient where the size of data is greater then 1e4 |
| 32 | + * |
| 33 | + * |
| 34 | + */ |
| 35 | +int p_int_odd (unsigned *l, unsigned *r, int k){ |
| 36 | + |
| 37 | + if(r-l<128){ |
| 38 | + unsigned tmp[r-l]; |
| 39 | + unsigned *it=tmp; |
| 40 | + for(unsigned *i=l;i!=r;i++) *it++=*i; |
| 41 | + sort_insert((int*)tmp,r-l); |
| 42 | + return tmp[k]; |
| 43 | + |
| 44 | + |
| 45 | + } |
| 46 | + if(r-l<0x10000){ |
| 47 | + unsigned tmp[r-l]; |
| 48 | + unsigned *it=tmp; |
| 49 | + for(unsigned *i=l;i!=r;i++) *it++=*i; |
| 50 | + olsd(tmp,tmp+(r-l)); |
| 51 | + return tmp[k]; |
| 52 | + |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + int count[0x10000]={0}; |
| 58 | + for(unsigned *i=l;i!=r;i++) count[(*i-INT_MIN) >>16]++; |
| 59 | + unsigned f=0;int pos=k+1; |
| 60 | + while(count[f]<pos) pos-=count[f++]; |
| 61 | + |
| 62 | + for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0 |
| 63 | + for(unsigned *i=l;i!=r;i++) |
| 64 | + if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++; |
| 65 | + unsigned b=0; |
| 66 | + while(count[b]<pos) pos-=count[b++]; |
| 67 | + return ((f<<16)+b)+INT_MIN; |
| 68 | + |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +double p_int_pair (unsigned *l, unsigned *r, int k){ |
| 73 | + if(r-l<128){ |
| 74 | + unsigned tmp[r-l]; |
| 75 | + unsigned *it=tmp; |
| 76 | + for(unsigned *i=l;i!=r;i++) *it++=*i; |
| 77 | + sort_insert((int*)tmp,r-l); |
| 78 | + return ((double)tmp[k]+(double)tmp[k-1])/(double) 2; |
| 79 | + |
| 80 | + |
| 81 | + } |
| 82 | + if(r-l<0x10000){ |
| 83 | + unsigned tmp[r-l]; |
| 84 | + unsigned *it=tmp; |
| 85 | + for(unsigned *i=l;i!=r;i++) *it++=*i; |
| 86 | + olsd(tmp,tmp+(r-l)); |
| 87 | + return ((double)tmp[k]+(double)tmp[k-1])/(double) 2; |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + int count[0x10000]={0}; |
| 93 | + for(unsigned *i=l;i!=r;i++) count[(*i-INT_MIN) >>16]++; |
| 94 | + unsigned f=0;int pos=k; |
| 95 | + while(count[f]<pos) pos-=count[f++]; |
| 96 | + unsigned f1=0; int pos1=k+1; |
| 97 | + while(count[f1]<pos1) pos1-=count[f1++]; |
| 98 | + |
| 99 | + if(f==f1){ |
| 100 | + |
| 101 | + for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0 |
| 102 | + for(unsigned *i=l;i!=r;i++) |
| 103 | + if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++; |
| 104 | + |
| 105 | + unsigned b=0; |
| 106 | + while(count[b]<pos) pos-=count[b++]; |
| 107 | + unsigned b1=0; |
| 108 | + while(count[b1]<pos1) pos1-=count[b1++]; |
| 109 | + |
| 110 | + int v1=(f<<16)+b+INT_MIN; |
| 111 | + int v2=(f<<16)+b1+INT_MIN; |
| 112 | + |
| 113 | + return ((double)v1+(double)v2)/(double)2; // double convertion is needed to avoid overflow |
| 114 | + } |
| 115 | + |
| 116 | + for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0 |
| 117 | + for(unsigned *i=l;i!=r;i++) |
| 118 | + if((*i-INT_MIN)>>16==f)count[(*i-INT_MIN)& 0xffff]++; |
| 119 | + |
| 120 | + unsigned b=0; |
| 121 | + while(count[b]<pos) pos-=count[b++]; |
| 122 | + |
| 123 | + for(int i=0;i<0x10000;i++)count[i]=0; // reset count to 0 |
| 124 | + for(unsigned *i=l;i!=r;i++) |
| 125 | + if((*i-INT_MIN)>>16==f1)count[(*i-INT_MIN)& 0xffff]++; |
| 126 | + |
| 127 | + |
| 128 | + unsigned b1=0; |
| 129 | + while(count[b1]<pos1) pos1-=count[b1++]; |
| 130 | + int v1=(f<<16)+b+INT_MIN; |
| 131 | + int v2=(f1<<16)+b1+INT_MIN; |
| 132 | + return ((double)v1+(double)v2)/(double)2; |
| 133 | + |
| 134 | + |
| 135 | +} |
| 136 | + |
0 commit comments