Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 785787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:56:06+00:00 2026-05-14T20:56:06+00:00

I have a sorted list which is rotated and would like to do a

  • 0

I have a sorted list which is rotated and would like to do a binary search on that list to find the minimum element.

Lets suppose initial list is {1,2,3,4,5,6,7,8}
rotated list can be like {5,6,7,8,1,2,3,4}

Normal binary search doesn’t work in this case. Any idea how to do this.

— Edit

I have one another condition. What if the list is not sorted??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:56:07+00:00Added an answer on May 14, 2026 at 8:56 pm

    A slight modification on the binary search algorithm is all you need; here’s the solution in complete runnable Java (see Serg’s answer for Delphi implementation, and tkr’s answer for visual explanation of the algorithm).

    import java.util.*;
    public class BinarySearch {
        static int findMinimum(Integer[] arr) {
            int low = 0;
            int high = arr.length - 1;
            while (arr[low] > arr[high]) {
                int mid = (low + high) >>> 1;
                if (arr[mid] > arr[high]) {
                    low = mid + 1;
                } else {
                    high = mid;
                }
            }
            return low;
        }
        public static void main(String[] args) {
            Integer[] arr = { 1, 2, 3, 4, 5, 6, 7 };
            // must be in sorted order, allowing rotation, and contain no duplicates
    
            for (int i = 0; i < arr.length; i++) {
                System.out.print(Arrays.toString(arr));
                int minIndex = findMinimum(arr);
                System.out.println(" Min is " + arr[minIndex] + " at " + minIndex);
                Collections.rotate(Arrays.asList(arr), 1);
            }
        }
    }
    

    This prints:

    [1, 2, 3, 4, 5, 6, 7] Min is 1 at 0
    [7, 1, 2, 3, 4, 5, 6] Min is 1 at 1
    [6, 7, 1, 2, 3, 4, 5] Min is 1 at 2
    [5, 6, 7, 1, 2, 3, 4] Min is 1 at 3
    [4, 5, 6, 7, 1, 2, 3] Min is 1 at 4
    [3, 4, 5, 6, 7, 1, 2] Min is 1 at 5
    [2, 3, 4, 5, 6, 7, 1] Min is 1 at 6
    

    See also

    • Java Collections.rotate() with an array doesn’t work
      • Explains why Integer[] instead of int[]
    • Google Research Blog: Nearly All Binary Searches and Mergesorts are Broken
      • Explains why >>> 1 instead of / 2

    On duplicates

    Note that duplicates makes it impossible to do this in O(log N). Consider the following bit array consisting of many 1, and one 0:

      (sorted)
      01111111111111111111111111111111111111111111111111111111111111111
      ^
    
      (rotated)
      11111111111111111111111111111111111111111111101111111111111111111
                                                   ^
    
      (rotated)
      11111111111111101111111111111111111111111111111111111111111111111
                     ^
    

    This array can be rotated in N ways, and locating the 0 in O(log N) is impossible, since there’s no way to tell if it’s in the left or right side of the “middle”.


    I have one another condition. What if the list is not sorted??

    Then, unless you want to sort it first and proceed from there, you’ll have to do a linear search to find the minimum.

    See also

    • Wikipedia | Selection algorithm | Linear minimum/maximum algorithms
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like that: The rows are sorted by CLNDR_DATE
I have a list of strings which has been sorted by a specific comparison
I currently have a long list which is being sorted using a lambda function
I have a ListBox to which I bound a sorted list of strings. I
I have two lists of objects. Each list is already sorted by a property
I have a table of data sorted by date, from which a user can
I have potentially large files that need to be sorted by 1-n keys. Some
I have a column containing items that can be sorted by the user: DOC_ID
I have a sorted list of inputs: let x = [2; 4; 6; 8;
I'm working in c#. I have a sorted List of structures. The structure has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.