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

Add Cocktail Sort in C++ in Sorting and Fibonacci Search in C in searching #256

Open
wants to merge 3 commits into
base: master
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
1 change: 1 addition & 0 deletions Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
* [Muskan](https://github.com/Muskan-goyal6)
* [Tarannum](https://github.com/giTan7)
* [HCamberos](https://github.com/HCamberos)
* [Priyal Gupta](https://github.com/priyal18)
75 changes: 75 additions & 0 deletions Search Algorithms/C-CPP/Fibonacci_Search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// C program for Fibonacci Search
#include <stdio.h>

// 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;
}
54 changes: 54 additions & 0 deletions Sorting Algorithms/C-CPP/cocktail-sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <bits/stdc++.h>
using namespace std;

void CocktailSort(int a[], int n)
{
bool swapped = true;
int start = 0;
int end = n - 1;

while (swapped) {
swapped = false;

for (int i = start; i < end; ++i) {
if (a[i] > a[i + 1]) {
swap(a[i], a[i + 1]);
swapped = true;
}
}

if (!swapped)
break;

swapped = false;

--end;

for (int i = end - 1; i >= start; --i) {
if (a[i] > a[i + 1]) {
swap(a[i], a[i + 1]);
swapped = true;
}
}

++start;
}
}


void printArray(int a[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}

int main()
{
int arr[] = { 5, 1, 4, 2, 8, 0, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
CocktailSort(arr, n);
printf("Sorted array :\n");
printArray(arr, n);
return 0;
}