Show / Hide Table of Contents

Class BinarySearch

Implements Binary search algorithm for finding a specific value in a sorted list.

Inheritance
System.Object
BinarySearch
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: AlgorithmsAndDataStructures.Algorithms.Search
Assembly: AlgorithmsAndDataStructures.dll
Syntax
public class BinarySearch

Methods

| Improve this Doc View Source

Search<T>(List<T>, T, Int32, Int32)

Searches in a sorted list of any comparable type, and returns the index of the key using binary search, and -1 if it is not found.

Declaration
[Algorithm(AlgorithmType.Search, "BinarySearch", Assumptions = "List is sorted with an ascending order.")]
[SpaceComplexity("O(1)", false, InPlace = true)]
[TimeComplexity(Case.Best, "O(1)")]
[TimeComplexity(Case.Worst, "O(Log(n))")]
[TimeComplexity(Case.Average, "O(Log(n))")]
public static int Search<T>(List<T> sortedList, T key, int startIndex, int endIndex)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.List<T> sortedList

A sorted list of any comparable type.

T key

The value that is being searched for.

System.Int32 startIndex

The lowest (left-most) index of the list - inclusive.

System.Int32 endIndex

The highest (right-most) index of the list - inclusive.

Returns
Type Description
System.Int32

The index of the key in the list, and -1 if it is absent from the array.

Type Parameters
Name Description
T
  • Improve this Doc
  • View Source
Back to top Generated by DocFX