Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach

This is a Java Program to find minimum element of a rotated sorted array. The following program uses a binary search approach to find the minimum element of a rotated sorted array. Time complexity is O (log n)

Here is the source code of the Java program to find minimum element of a rotated sorted array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 * Java Program to Find the Minimum element of a rotated 
 * sorted Array using Binary Search approach
 */
 
import java.util.Scanner;
 
public class MinimumElementInRotatedSortedArray
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of elements in array");
        int N = scan.nextInt();
        int[] arr = new int[ N ];
        /* Accept N elements */
        System.out.println("Enter "+ N +" elements of rotated sorted array");
        for (int i = 0; i < N; i++)
            arr[i] = scan.nextInt();
        System.out.println("Minimum element = "+ min(arr));
    }
    public static int min(int[] arr)
    {
        return min(arr, 0, arr.length - 1);
    }
    public static int min(int[] arr, int low, int high)
    {
        if (high < low)  
            return arr[0];        
        if (high == low) 
            return arr[low];
        /* Calculate mid position */
        int mid = (high + low)/2;
        if (mid < high && arr[mid + 1] < arr[mid])
           return arr[mid + 1];            
        if (mid > low && arr[mid] < arr[mid - 1])
           return arr[mid];
        /* recursively find min */   
        if (arr[high] > arr[mid])
           return min(arr, low, mid - 1);
        return min(arr, mid + 1, high);
    }
}
Enter number of elements in array
10
Enter 10 elements of rotated sorted array
59 78 83 99 24 29 35 49 53 56
Minimum element = 24
 
 
Enter number of elements in array
10
Enter 10 elements of rotated sorted array
14 23 34 56 61 67 75 81 90 99
Minimum element = 14
 
 
Enter number of elements in array
10
Enter 10 elements of rotated sorted array
2 3 4 5 6 7 8 9 10 1
Minimum element = 1

Related posts:

Implementing a Binary Tree in Java
Getting Started with Stream Processing with Spring Cloud Data Flow
Apache Camel with Spring Boot
Spring AMQP in Reactive Applications
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to implement Priority Queue
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Hướng dẫn Java Design Pattern – Object Pool
Quick Guide to Spring MVC with Velocity
Java Program to Implement HashTable API
Serialize Only Fields that meet a Custom Criteria with Jackson
Calling Stored Procedures from Spring Data JPA Repositories
Java Program to Represent Linear Equations in Matrix Form
Introduction to the Java ArrayDeque
Java Program to Implement Unrolled Linked List
Apache Tiles Integration with Spring MVC
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
A Quick Guide to Spring MVC Matrix Variables
Java Program to Implement Interpolation Search Algorithm
Sắp xếp trong Java 8
Introduction to Java 8 Streams
Extra Login Fields with Spring Security
Java Program to Implement Heap Sort Using Library Functions
Thao tác với tập tin và thư mục trong Java
Java Program to Implement Hash Tree
Transaction Propagation and Isolation in Spring @Transactional
Java Program to Implement the Bin Packing Algorithm
Java Program to Implement the Checksum Method for Small String Messages and Detect
Java Program to Solve Tower of Hanoi Problem using Stacks
Hướng dẫn Java Design Pattern – Chain of Responsibility