mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-24 10:33:10 +08:00
style: format code
This commit is contained in:
@@ -25,7 +25,7 @@ using namespace std;
|
||||
#define MAX 10000000
|
||||
|
||||
int N = 21;
|
||||
int A[MAX] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,10};
|
||||
int A[MAX] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 10};
|
||||
|
||||
/*
|
||||
* get_input function is to receive input from standard IO
|
||||
@@ -35,82 +35,94 @@ void get_input()
|
||||
// TODO: Get input from STDIO or write input to memory as done above.
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* This is the iterative method of the ternary search which returns the index of the element.
|
||||
*/
|
||||
int it_ternary_search(int left, int right, int A[],int target)
|
||||
int it_ternary_search(int left, int right, int A[], int target)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
if (left < right)
|
||||
{
|
||||
if(left<right)
|
||||
{
|
||||
if(right-left < absolutePrecision)
|
||||
{
|
||||
for(int i=left;i<=right;i++)
|
||||
if(A[i] == target) return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int oneThird = (left+right)/3+1;
|
||||
int twoThird = (left+right)*2/3+1;
|
||||
|
||||
if(A[oneThird] == target) return oneThird;
|
||||
else if(A[twoThird] == target) return twoThird;
|
||||
|
||||
else if(target > A[twoThird]) left = twoThird+1;
|
||||
else if(target < A[oneThird]) right = oneThird-1;
|
||||
|
||||
else left = oneThird+1, right = twoThird-1;
|
||||
}
|
||||
else return -1;
|
||||
if (right - left < absolutePrecision)
|
||||
{
|
||||
for (int i = left; i <= right; i++)
|
||||
if (A[i] == target)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int oneThird = (left + right) / 3 + 1;
|
||||
int twoThird = (left + right) * 2 / 3 + 1;
|
||||
|
||||
if (A[oneThird] == target)
|
||||
return oneThird;
|
||||
else if (A[twoThird] == target)
|
||||
return twoThird;
|
||||
|
||||
else if (target > A[twoThird])
|
||||
left = twoThird + 1;
|
||||
else if (target < A[oneThird])
|
||||
right = oneThird - 1;
|
||||
|
||||
else
|
||||
left = oneThird + 1, right = twoThird - 1;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This is the recursive method of the ternary search which returns the index of the element.
|
||||
*/
|
||||
int rec_ternary_search(int left, int right, int A[],int target)
|
||||
int rec_ternary_search(int left, int right, int A[], int target)
|
||||
{
|
||||
if(left<right)
|
||||
if (left < right)
|
||||
{
|
||||
if (right - left < absolutePrecision)
|
||||
{
|
||||
if(right-left < absolutePrecision)
|
||||
{
|
||||
for(int i=left;i<=right;i++)
|
||||
if(A[i] == target) return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int oneThird = (left+right)/3+1;
|
||||
int twoThird = (left+right)*2/3+1;
|
||||
for (int i = left; i <= right; i++)
|
||||
if (A[i] == target)
|
||||
return i;
|
||||
|
||||
if(A[oneThird] == target) return oneThird;
|
||||
if(A[twoThird] == target) return twoThird;
|
||||
|
||||
if(target < A[oneThird]) return rec_ternary_search(left, oneThird-1, A, target);
|
||||
if(target > A[twoThird]) return rec_ternary_search(twoThird+1, right, A, target);
|
||||
|
||||
return rec_ternary_search(oneThird+1, twoThird-1, A, target);
|
||||
return -1;
|
||||
}
|
||||
else return -1;
|
||||
|
||||
int oneThird = (left + right) / 3 + 1;
|
||||
int twoThird = (left + right) * 2 / 3 + 1;
|
||||
|
||||
if (A[oneThird] == target)
|
||||
return oneThird;
|
||||
if (A[twoThird] == target)
|
||||
return twoThird;
|
||||
|
||||
if (target < A[oneThird])
|
||||
return rec_ternary_search(left, oneThird - 1, A, target);
|
||||
if (target > A[twoThird])
|
||||
return rec_ternary_search(twoThird + 1, right, A, target);
|
||||
|
||||
return rec_ternary_search(oneThird + 1, twoThird - 1, A, target);
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* ternary_search is a template function
|
||||
* You could either use it_ternary_search or rec_ternary_search according to preference.
|
||||
*/
|
||||
void ternary_search(int N,int A[],int target)
|
||||
void ternary_search(int N, int A[], int target)
|
||||
{
|
||||
cout << it_ternary_search(0,N-1,A,target) << '\t';
|
||||
cout << rec_ternary_search(0,N-1,A,target) << '\t';
|
||||
cout << it_ternary_search(0, N - 1, A, target) << '\t';
|
||||
cout << rec_ternary_search(0, N - 1, A, target) << '\t';
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
get_input();
|
||||
ternary_search(N,A,_target);
|
||||
ternary_search(N, A, _target);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user