Please help me improve my coding style.
/*
Author: Muhammad Awais
Blog: http://uetianblogger.blogspot.com/
Description:
Divide and Conquer Rule. Binary Search like algorithm
to find local peak.It may or may not find global peak
of the array.
Parameters:
A[]= Array of elements
i=starting point
j=ending point
Algorithms Class Analysis:
Theta(log(n))
BigO(log(n))
Omega(1)
*/
#include <stdio.h>
#include <stdlib.h>
int Peak1D(int A[],int i,int j)
{
//Middle point
int m=((i+j)/2);
//If it comes to either corner of the array
if (m==i || m==j)
{
return A[m];
}
//To find peak
else
{
if (A[m+1]>A[m])
{
Peak1D(A,m+1,j);
}
else if (A[m-1]>A[m])
{
Peak1D(A,i,m-1);
}
else return A[m];
}
}
***UPDATE**
Following is my updated code bases on suggestions. Please review this too.*
/*
Author: Muhammad Awais
Blog: http://uetianblogger.blogspot.com/
Date: 16/11/2016
Description:
Divide and Conquer Rule. Binary Search like algorithm
to find local peak.It may or may not find global peak
of the array.
Parameters:
A[]= Array of elements
starting_pt=starting point
ending_pt=ending point
Algorithms Class Analysis:
Theta(log(n))
BigO(log(n))
Omega(1)
*/
#include <stdio.h>
#include <stdlib.h>
int Peak1D(int A[],size_t starting_pt,size_t ending_pt)
{
//If length is zero
if ((sizeof(A)==0))
{
return 0;
}
//Middle point
int middle_pt=((starting_pt+ending_pt)/2);
//If it comes to either end of the array
if (middle_pt==starting_pt || middle_pt==ending_pt)
{
return A[middle_pt];
}
//To find local peak
if (A[middle_pt+1]>A[middle_pt])
{
Peak1D(A,middle_pt+1,ending_pt);
}
else if (A[middle_pt-1]>A[middle_pt])
{
Peak1D(A,starting_pt,middle_pt-1);
}
return A[middle_pt];
}
i
is the starting point, why not name the variablestartingPoint
instead? Same withj
andm
should bemiddle
. Arrays don't have "corners", they have bounds, the word "corner" makes me think matrix, not 1D array. The outerelse
doesn't need to be there and can be eliminated entirely. \$\endgroup\$