Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with my code - circularly sorted array does not show any results

Tags:

arrays

php

sorted

I had an interview today and the person asked me this question:

How do you find easily an item in a circularly sorted array

Since I didn't know the answer, I tried to find a solution. Here's what I have:

Thanks

<?php


function searchincircularsorterlist($a, $len, $num) {
    $start=0;
    $end=$len-1;
    $mid = 0;
    while($start<$end) {
        $mid=$start+$end/2;
        if ($num == $a[$mid]) {
            return $num;
        }
        if($num<$a[$mid]) {
            if($num<$a[$start] && $a[$start]<=$a[$start+1])
            $start=$mid++;
            else
            $end=$mid--;
            }
        else {
            if($num>$a[$end] && $a[$end-1]<=$a[end])
            $end=$mid--;
            else
            $start=$mid++;
        }
    }
    if ($start == $end && $num == $a[$start]) {
        return $num;
    }
    return -1; 
}

$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));

I am trying to work with a circularly sorted array but for some reason it does not work. What's wrong with my code?

like image 560
Gino Sullivan Avatar asked Nov 02 '11 01:11

Gino Sullivan


People also ask

What is circularly sorted array?

Circularly sorted arrays are arrays that are sorted in ascending or descending order and then rotated by a number of steps.

What is the time complexity to check if an array is sorted or not?

The average and worst-case complexity for your algorithm to check if an array is unsorted is O(N) . But the best-case complexity is O(1) .

Is sorted array LeetCode?

Check if Array Is Sorted and Rotated - LeetCode. Given an array nums , return true if the array was originally sorted in non-decreasing order, then rotated some number of positions (including zero). Otherwise, return false . There may be duplicates in the original array.


1 Answers

1) learn priority of operations. You should have: $mid=($start+$end)/2; which you ended up dividing $end by 2 and then $start - the result. This is why you got an infinite loop.

2) use: $start=$mid+1; and not $start=$mid++; that will help reducing the number of loops

<?php

function searchincircularsorterlist($a, $len, $num) {
    $start=0;
    $end=$len-1;
    $mid = 0;
    while($start<$end) {
        $mid=($start+$end)/2;
        if ($num == $a[$mid]) {
            return $num;
        }
        if($num<$a[$mid]) {
            if($num<$a[$start] && $a[$start]<=$a[$start+1])
            $start=$mid+1;
            else
            $end=$mid-1;
            }
        else {
            if($num>$a[$end] && $a[$end-1]<=$a[end])
            $end=$mid-1;
            else
            $start=$mid+1;
        }
    }
    if ($start == $end && $num == $a[$start]) {
        return $num;
    }
    return -1; 
}

$array = array(7,8,9,0,1,2,3,4,5,6);
var_dump(searchincircularsorterlist($array,sizeof($array),4));
like image 92
Book Of Zeus Avatar answered Sep 30 '22 00:09

Book Of Zeus