Skip to main content
Commonmark migration
Source Link

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQcode-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1
Tweeted twitter.com/StackCodeGolf/status/665604501571805184
added 114 characters in body
Source Link
A T
  • 157
  • 7

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1

Binary search is an algorithm that looks for an element in a sorted data-structure by constantly dividing the search space in half.

###Rosetta Code

A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. It is the classic example of a "divide and conquer" algorithm.

###Wikipedia

In computer science, a binary search or half-interval search algorithm finds the position of a target value within a sorted array. The binary search algorithm can be classified as a dichotomic divide-and-conquer search algorithm and executes in logarithmic time.


Smallest # of bytes wins

Clarifications

  • Example of calling the function/lambda/macro should be included, but don't count those chars!
  • Output should be > -1 if found, and <=-1 otherwise
  • import binary_search (20 bytes) DOES NOT count!
  • Implementation should run in O(log n), as algorithm dictates

Also see the code-golf StackExchange FAQ.

Example

> binary_search([1,3,5,6,7,8,8,10], 5);
4
> binary_search([1,3,5,6,7,8,8,10], 543);
-1
deleted 8 characters in body
Source Link
A T
  • 157
  • 7
Loading
added 58 characters in body
Source Link
A T
  • 157
  • 7
Loading
Post Reopened by Alex A.
added 624 characters in body
Source Link
A T
  • 157
  • 7
Loading
Post Closed as "Needs details or clarity" by Alex A.
Source Link
A T
  • 157
  • 7
Loading