From f1ac9f59977c92202de03a115576946df5b94ad9 Mon Sep 17 00:00:00 2001 From: Ayush Sharma Date: Thu, 24 Oct 2019 19:44:51 +0530 Subject: [PATCH] Added a code which search a number in an array through fibbonacci search. --- .mergeinversion.c.swp | Bin 0 -> 12288 bytes fibonaccisearch.c | 76 ++++++++++++++++++++++++++++++++++++++++++ mergeinversion.c | 0 3 files changed, 76 insertions(+) create mode 100644 .mergeinversion.c.swp create mode 100644 fibonaccisearch.c create mode 100644 mergeinversion.c diff --git a/.mergeinversion.c.swp b/.mergeinversion.c.swp new file mode 100644 index 0000000000000000000000000000000000000000..20b715eaf59d04b81497e77379c274f2b1c9659d GIT binary patch literal 12288 zcmeI2J#W)c6ozjZOG^<53GpFJN-IaUn-7Gjl(?(~LFqtQiU&8jX>Xf4k?lHdDkuVe z0f>#z|mc2OwSs#b|C7a;TX5=rr&59g=mhQYa=^W4iVTo}Ll{`|YsTd8+|^;Mhesh3rq4;B+25xl5fxfZxl?F0E2FL&z zAOmE843GgbKnBPF86X2>;0PM9tBef}GL|`x;_>%?`TPIx5ypOjFW^0B0te_o14H2F z3C4ba&)^Ms4IYC4%z`Um1pFCB9V#6Ghh;22V-CqgdTR_V-(Oq2FL&z zAOmE843L5U)xblhbPkvMYp$c@ONUVrra4U9J~98E4jI^jS8dnT^Eyvnm$6>Oc91a)&FJG;IKN?Got&;UTow0J zHq3l!qwJJ6G%eemRO$_9G5!eGaMLjgz0h=KCy_KA=9Jhb3*%R9;iy@bu+)xuq( zH|odJ?R`3u9ot$VS|MZQ*j0iQiK^sux;oobR8P?qp3N0|y=^kNTGUiN-A&=6H6 z%H5X%PAxrs&FlNlqqcLoIBVy{Zzmpc_+VBv%`)q}=Fbb9^@?dNi8@zBxrHRMtnPYO zEH3%;%XW5w9U4ru8OMRX@#sIdKJjK%JIOv%SAnh@(QneoXIc!V>jm> z2IN{ijCj^6o1Mn3-s{@CVQfrM6-ZOE%J?`=p#!H0BQj~6D6q`10(7qxv>;(W+#vyW zM^NSlW=lc1LzoPfV^mfv&f#gTzW5g6Tjh?~QY)1q{o)8SuP=i@Z|PCaPLQR?Wkxkv z`ikj=yDEAm<1Ld;6SlI?I~nanQQiH5rZcorGD}|ylNR&wfd<*Aa4Pk+DldC}RWv4+ Grr2LwiIF}4 literal 0 HcmV?d00001 diff --git a/fibonaccisearch.c b/fibonaccisearch.c new file mode 100644 index 0000000..14cbdbb --- /dev/null +++ b/fibonaccisearch.c @@ -0,0 +1,76 @@ +// C program for Fibonacci Search +#include + +// Utility function to find minimum of two elements +int min(int x, int y) { return (x<=y)? x : y; } + +/* Returns index of x if present, else returns -1 */ +int fibMonaccianSearch(int arr[], int x, int n) +{ + /* Initialize fibonacci numbers */ + int fibMMm2 = 0; // (m-2)'th Fibonacci No. + int fibMMm1 = 1; // (m-1)'th Fibonacci No. + int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci + + /* fibM is going to store the smallest Fibonacci + Number greater than or equal to n */ + while (fibM < n) + { + fibMMm2 = fibMMm1; + fibMMm1 = fibM; + fibM = fibMMm2 + fibMMm1; + } + + // Marks the eliminated range from front + int offset = -1; + + /* while there are elements to be inspected. Note that + we compare arr[fibMm2] with x. When fibM becomes 1, + fibMm2 becomes 0 */ + while (fibM > 1) + { + // Check if fibMm2 is a valid location + int i = min(offset+fibMMm2, n-1); + + /* If x is greater than the value at index fibMm2, + cut the subarray array from offset to i */ + if (arr[i] < x) + { + fibM = fibMMm1; + fibMMm1 = fibMMm2; + fibMMm2 = fibM - fibMMm1; + offset = i; + } + + /* If x is greater than the value at index fibMm2, + cut the subarray after i+1 */ + else if (arr[i] > x) + { + fibM = fibMMm2; + fibMMm1 = fibMMm1 - fibMMm2; + fibMMm2 = fibM - fibMMm1; + } + + /* element found. return index */ + else return i; + } + + /* comparing the last element with x */ + if(fibMMm1 && arr[offset+1]==x)return offset+1; + + /*element not found. return -1 */ + return -1; +} + +/* driver function */ +int main(void) +{ + int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, + 85, 90, 100}; + int n = sizeof(arr)/sizeof(arr[0]); + int x = 85; + printf("Found at index: %d", + fibMonaccianSearch(arr, x, n)); + return 0; +} + diff --git a/mergeinversion.c b/mergeinversion.c new file mode 100644 index 0000000..e69de29