Basically I am attempting a HW assignment for questions that a frequently asked in interviews. I am not sure if I am having problems with the coding or actually understanding what they question is asking for. Basically I am asked to find an element in a sorted array and that is it. The element I am assuming is the pivot point? I am more so needing help with the understanding than the coding itself but advice on either is welcomed. Keep in mind this is entry level C work I am doing so any advanced explanations will most likely be over my understanding. Here is what I have so far.

#include <stdio.h>

#define size 5

int main()
{
int array[size] = {4, 8, 0, 1, 3}, pivot, i, j, high, low, mid;
high=4;
low=0;

while ( low <= high )
{
    mid=(high + low)/2;
    if (array[low] < array[mid])
        low = mid+1;
    else if (array[mid] < array[high])
        high = mid-1;
    else
        printf("%d is pivot", mid);

Recommended Answers

All 3 Replies

Basically I am asked to find an element in a sorted array and that is it

If this is the question, then your code, which is basically binary search will work fine. Of course as its an interview question, they want to see some innovation or knowledge from your side. Perhaps another algorithm which is efficient than binary search. See this, its an quick search algorithm. You can google, to find more search algorithm.

The element I am assuming is the pivot point?

No, it's not a pivot point. It's just a number you need to find.

Basically I am asked to find an element in a sorted array and that is it.

What element? You don't seem to be looking for anything. You need a target number to find in the array

int array[size] = {4, 8, 0, 1, 3}

I thought you said sorted array...

It's asking you to search for a value in the array.

Could you post the text of the question, exactly as it was provided to you? That way we'll be able to address any issues with your understanding of it, which is where your uncertainty appears to stem from.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.