Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Searching algorithms #190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Algorithms/Intro.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Contribute by writing algorithms in whichever Programming Language you want.
Contribute by writing algorithms in whichever Programming Language you want.
158 changes: 79 additions & 79 deletions C++/FibonacciSearch.cpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
// Cpp program for Fibonacci Search
#include<bits/stdc++.h>
using namespace std ;
// 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;
cout<<"Found at index: "<<fibMonaccianSearch(arr, x, n);
return 0;
}
// Cpp program for Fibonacci Search

#include<bits/stdc++.h>
using namespace std ;

// 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;
cout<<"Found at index: "<<fibMonaccianSearch(arr, x, n);
return 0;
}
4 changes: 2 additions & 2 deletions C++/Intro.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Write C++ programs and participate in #HacktoberFest2020 by contributing to open-source.
You can create new branch for specific projects.
Write C++ programs and participate in #HacktoberFest2020 by contributing to open-source.
You can create new branch for specific projects.
42 changes: 21 additions & 21 deletions C++/Questions using Recursion/Combination_formula.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#include <iostream>
using namespace std;
int C(int n,int r)
{
if(r==0 || n==r)
return 1;
else
return C(n-1,r-1) + C(n-1,r);
}
int main() {
int n;
int r;
cout<<"Combination Formula :\n nCr = n!/(r!*(n-r)!)\n";
cout<<"Enter the value of n : ";
cin>>n;
cout<<"enter the value of r : ";
cin>>r;
cout<<"\n"<<n<<"C"<<r<<" : "<<C(n,r);
return 0;
}
#include <iostream>
using namespace std;

int C(int n,int r)
{
if(r==0 || n==r)
return 1;
else
return C(n-1,r-1) + C(n-1,r);
}
int main() {
int n;
int r;
cout<<"Combination Formula :\n nCr = n!/(r!*(n-r)!)\n";
cout<<"Enter the value of n : ";
cin>>n;
cout<<"enter the value of r : ";
cin>>r;
cout<<"\n"<<n<<"C"<<r<<" : "<<C(n,r);
return 0;
}
110 changes: 55 additions & 55 deletions C++/bubblesort.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
// Optimized implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0)
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
// Driver program to test above functions
int main()
{
IOS;
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}
// Optimized implementation of Bubble sort
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0)

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}

// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}

// Driver program to test above functions
int main()
{
IOS;
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout<<"Sorted array: \n";
printArray(arr, n);
return 0;
}
4 changes: 2 additions & 2 deletions C/Intro.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Write C programs and participate in #HacktoberFest2020 by contributing to open-source.
You can create new branch for specific projects.
Write C programs and participate in #HacktoberFest2020 by contributing to open-source.
You can create new branch for specific projects.
Loading