Design an algorithm to determine if the given array is alrea
Design an algorithm to determine if the given array is already sorted or not. Analyze this algorithm for its worst-case input. Setup the summation and solve it.
Design an algorithm to determine if the given array is already sorted or not. Analyze this algorithm for its worst-case input. Setup the summation and solve it.
Solution
isSorted (A[ ], n)
{
flag=0;
i=0;
j=0;
while (i<n)
{
if (A[i] == A[i+l])
if (A[i] < A[i+l])
{
while ( i!=n)
{
if (A[i] > A[i+1])
{
flag=1
exit
}
i++;
}
}
flag =-l;
if (A[i] > A[i+l])
{
while (i!=n)
{
if (A[i] < A[i+l])
{
flag =l
exit
}
i++
}
}
}
return (flag)
}
If the second element is bigger than the first one then test is each successive element is grater than or equal to current element.
In case two the test is done if the array is sorted in the descending order. In third case test is done if first k elements have same value.

