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 files via upload #61

Open
wants to merge 4 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
66 changes: 66 additions & 0 deletions Search/Ternary_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
using namespace std;

#define s 12 //I gave space in the array as 12,but you can give the number you want.

int ternary_search (int v[],int n, int left, int right, int x);
int main()
{
int v[s];
short x;
for(int i = 1; i <= s; i++)
{
v[i-1] = i;
}
cout << "Enter number for research:\n";
cin >> x;

int left = s/3;
int right = (s/3)*2;

if(ternary_search(v,s,left-1,right-1,x) == -1)
{
cout<<"Number does not exist in array.\n";
}
else
{
cout<<"The index is:"<<ternary_search(v,s,left-1,right-1,x)<<"\n";
}
return 0;
}
int ternary_search (int v[],int n, int left, int right, int x)
{

if(left < 0 || right > n-1 || left > right)
{
return -1;
}
if(x == v[left])
{
return left;
}

if(x == v[right])
{
return right;
}

// Update the two index left and right if the element is not found.


if(x < v[left])
{
return ternary_search(v,n,left-1,right,x);
}

if (x > v[left] && x < v[right])
{

return ternary_search(v,n,left+1,right-1,x);
}

if(x > v[right])
{
return ternary_search(v,n,left,right+1,x);
}
}
42 changes: 42 additions & 0 deletions Sorting/Insertion_Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include<iostream>

using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i-1;

/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
int main() //MAIN BODY
{
int *a,n,i;
cout<<"Enter No OF Units : ";
cin>>n; // TAKING INPUT OF SIZE OF ARRAY
a=new int[n+1];
for(int i=1;i<=n;i++) //INPUT OF ARRAY
{
cin>>a[i];
}
insertionSort(a,n); //CALLING INSERTION SORT FUNCTION
cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<a[i]<<endl; //PRINTING OF MODIFIED ARRAY
}

delete a;
return 0;
}
66 changes: 66 additions & 0 deletions Ternary_Search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <iostream>
using namespace std;

#define s 12 //I gave space in the array as 12,but you can give the number you want.

int ternary_search (int v[],int n, int left, int right, int x);
int main()
{
int v[s];
short x;
for(int i = 1; i <= s; i++)
{
v[i-1] = i;
}
cout << "Enter number for research:\n";
cin >> x;

int left = s/3;
int right = (s/3)*2;

if(ternary_search(v,s,left-1,right-1,x) == -1)
{
cout<<"Number does not exist in array.\n";
}
else
{
cout<<"The index is:"<<ternary_search(v,s,left-1,right-1,x)<<"\n";
}
return 0;
}
int ternary_search (int v[],int n, int left, int right, int x)
{

if(left < 0 || right > n-1 || left > right)
{
return -1;
}
if(x == v[left])
{
return left;
}

if(x == v[right])
{
return right;
}

// Update the two index left and right if the element is not found.


if(x < v[left])
{
return ternary_search(v,n,left-1,right,x);
}

if (x > v[left] && x < v[right])
{

return ternary_search(v,n,left+1,right-1,x);
}

if(x > v[right])
{
return ternary_search(v,n,left,right+1,x);
}
}