Binary search is an efficient algorithm for finding a key in sorted data. It runs in O(log n) time.
0
votes
2answers
57 views
C++ binary search tree with templates
I implemented a binary search tree with methods of insert, search, size and print using the << operator. All the methods works with ...
7
votes
4answers
1k views
Bisection method
Totally agree lack of { } on if else is bad practice. That is just how I roll and I can't do it at work.
Use bisection to get to the index of a target value of a sorted array in O(sqrt(array....
3
votes
1answer
83 views
Find kth largest element in the union of two sorted array
Problem statement:
Find \$kth\$ largest element in the union of two sorted array.
My introduction of the algorithm
I spent a few hours to review two algorithms, Leetcode 4:Median of Two Sorted Arrays ...
1
vote
1answer
67 views
Binary search in R
Again playing around in R with a Edx CS50 homework.
Performance relative to %in% is above my expectation. But I still wonder ...
2
votes
3answers
172 views
Binary Search Algorithm in JavaScript
I've written this implementation of a binary search algorithm, and wanted to know if it was efficient and where I can improve.
...
1
vote
1answer
59 views
Find a missing element in a sorted list
I've made a program to find the missing element (if there's one) in a sequential list:
Program criteria:
Returns -1 if a list is sequential, otherwise the ...
5
votes
3answers
131 views
Tree abstraction
This code provides a Tree abstraction.
I would like to finalize the code structure only in tree directory, before starting ...
2
votes
1answer
59 views
Benchmark switch, binary search and if-else
Just for curiosity I have done some benchmark for testing the best performance between switch, binary-search, and if-else statement.
here is the code :
...
0
votes
0answers
46 views
Ordered array item insertion based on binary method
Array is sorted. I'm asking more about algorithm related stuff. I know that by naming conventions is not good here. Also error handling is not exists yet.
...
4
votes
1answer
167 views
5
votes
0answers
112 views
Red-black tree appears to be slower than std::multimap
I've written a red-black tree in C as an exercise. The tree works and it is not bad, but it is about 10% slower than std::multimap from libstdc++ which I'm ...
3
votes
1answer
104 views
Binary search in C++
I was wondering if I have any hidden bugs or traps in my code I wrote for an exercise.
...
2
votes
2answers
45 views
Inserting into a binary search tree in C
I am in the process of refactoring this simple binary search tree code responsible for adding new values:
...
0
votes
1answer
58 views
Binary search on string in alphabetical order
The code for a binary search on an ordered string by alphabetical orders seems to work. I am wondering if there is a better way to write this.
...
1
vote
1answer
56 views
Find valid triples for a sorted list of integers (part 2)
Have some ideas to improve code from this discussion (Find valid triples for a sorted list of integers), and post new code in a new post.
The new idea is, trying to remember where low bound searched ...
0
votes
0answers
48 views
Data structure that supports binary search and has near constant insertion and deletion times
I'm trying to create a data structure that has the following properties:
must be able to perform a binary search based on a on-the-fly computed value
ideally must have an \$O(1)\$ insert and delete ...
5
votes
1answer
46 views
“Guess My Number” with Racket
I am using the book "Realm of Racket" which was written by the people behind the Racket and Dr. Racket projects. It is a great book based on games, similar to the famous "Land of Lisp".
The first ...
4
votes
1answer
121 views
Find valid triples for a sorted list of integers
I'm working on a problem in which I have an input array, sorted positive unique integers, and have to try to find all possible triples \$(x,y,z)\$ which satisfy \$x+y>z\$ and \$x<y<z\$. For ...
4
votes
2answers
81 views
Find max element in increasing-decreasing array
Question: Find the largest in a list that list is stored as two sections, one in ascending order and the other in descending order. Elements are distinct.
E.g: Input ...
2
votes
2answers
204 views
1
vote
1answer
71 views
Binary search for random access iterators in C++
I wrote a generic binary search routine in C++ for ranges that are specified by random access iterators. In case the user inputs a range with non-random access iterators, an exception will be thrown. ...
6
votes
2answers
91 views
Binary search a 2D array for multiple matching criteria fields
Here is what I came up with for a binary search of an array. It allows for searching one or many columns to match. It assumes that array being searched is sorted with the same sort priorities as the ...
3
votes
1answer
55 views
Search in sorted matrix NxM
The problem (from Cracking the Coding book):
Given an NxM matrix in which each row and each column is sorted in ascending order, write a method to find an element.
I thought of a modified version ...
3
votes
3answers
205 views
Binary search of an integer array
I'm a beginner. This is my implementation of binary search algorithm in Python. Please review my code and give your valuable inputs for further improvements on overall coding style, rare cases etc.
<...
0
votes
1answer
34 views
Implementation of the binary search algorithm [closed]
This is my first implementation the binary search algorithm. I have tested it and so far it is okay. But I need an experienced eye to review the code and recommend best practices.
...
3
votes
3answers
71 views
Binary search that gets ALL matching results
I am implementing a binary search to retrieve all matches of a specified value. Instead of a regular binary search that would just return the first match, I want to get that first match AND all ...
3
votes
2answers
88 views
Making a sorted linked list into a balanced binary search tree
My code works, but I need help on revising it to make it shorter and simple if possible on my main.cpp part ONLY. The other files is to make the program compile and work out if you want to see the ...
3
votes
2answers
166 views
Calculate the path sum of a binary tree
I want to calculate all sum of all possible paths from root to leaf. Each node has value of an integer number (decimal).
In the following example:
...
4
votes
3answers
114 views
Finding the frequency of an element in a sorted list of integers
Input: Array of sorted integers and an element to check the frequency for
Output: Frequency of the element
...
9
votes
3answers
816 views
Binary search in C#
The challenge requires the program to receive data from the console and use binary search to find if a term is contained in a set of other terms. I've unit-tested the solution below, but according to ...
2
votes
1answer
112 views
Deletion of a node in a binary search tree
I am looking to see if my implementation of the removal/deletion method in a binary search tree is sufficient, readable, and runs in \$O(\log n)\$ time. It's been awhile since I've written a data ...
3
votes
1answer
141 views
Binary search tree class in Java
I am trying to create a binary search tree class in Java and am wondering how I did. I am fairly confident on most of the methods, but fear I may have messed up the delete method. I know I am ...
-5
votes
1answer
82 views
Maximum clique using binary matrix adjacency [closed]
I'm solve the maximum clique problem. I'm using binary matrix adjacency to represent the graph. But when I test using the DIMACS-benchmark, my answer is far more different compared to the answer given....
4
votes
3answers
409 views
Find median of two sorted arrays
Problem statement
There are two sorted arrays nums1 and nums2 of size m and ...
1
vote
3answers
552 views
Priority Queue (Binary Search Tree) Implementation
I have made a Priority Queue implemented as a Binary Search Tree. I would appreciate comments related to optimization and proper functionality, but any comments/critiques are welcome.
PQ.h:
...
0
votes
0answers
111 views
Binary search tree and breadth first search
I have written code to implement Binary Search and Breadth First Search in JS. Please review my code. I want to ensure Im following the best practices.
Node.js
...
2
votes
3answers
103 views
Minimum element in a sorted rotated array
A sorted array [0,1,2,3,4,5] when rotated n times (3 times in this case) becomes [3,4,5,0,1,2], meaning elements in the front move to the end. The code below finds the minimum element in this array, ...
0
votes
0answers
20 views
Alphanumerica binary search in node.js
I have some rather large arrays with hash values in them that looks like this:
ZjFhOTZjYzgtMmZhMy00YjdkLThmMDgtNjI4MjYxNTc3MGRlXw==ntyxntmzmzkw
I've done a binary search, using the node.js binary-...
5
votes
2answers
87 views
Recursive binary search implementation in Python
I have this (recursive) binary search implementation in Python, and I want you to criticize every aspect of it. It is a naive implementation, (drilling the basics and stuff), but, I want you to ...
0
votes
1answer
59 views
Couting numbers greater than given - using binary search
It's my first exercise involving the well-known binary search. The problem goes as follows: we are given a sequence of n numbers and we have to answer to t questions, each of which is: “How many ...
3
votes
1answer
103 views
Python 2 binary search
I wanted to take a stab at implementing a binary search, and here's what I came up with. Is there anything I could have done better, from a code clarity perspective, or code optimization perspective? ...
-1
votes
1answer
59 views
Finding the minimum from a sorted rotated array with duplicates
Is this an efficient way of finding the minimum using binary search?
...
2
votes
2answers
159 views
Template Binary Search Tree
This is a simple binary search tree implementation written with C++'s templates feature I wrote to learn C++. What improvements would make this cleaner and better as far as C++ practices go, and are ...
2
votes
1answer
83 views
Find the number of quadraples (a,b,c,d) from 4 respective arrays such that a+b+c+d=0
Given four lists of numbers, calculate how many ways there are to
choose one element from each to get a sum of zero.
The input is given on stdin, first the ...
-3
votes
3answers
282 views
Binary search algorithm
Would this recursive solution's speed be comparable to a non-recursive solution?
...
3
votes
0answers
2k views
Breadth and Depth First Search in Ruby
I was tasked with building three individual methods; one to create a Binary Search Tree (BST), one to carry out a Breadth First Search (BFS), and one to carry out a Depth First Search (DFS).
Create ...
2
votes
3answers
70 views
Locating a sequence in a sorted array
I read the following question: Searching an element in a sorted array and I thought that I could give it a try in Python.
Given a sorted list of integers and an integer. Return the (index) bounds of ...
3
votes
1answer
103 views
Binary and linear search methods for a number-guessing game
This is a code written is Pascal (Delphi). It asks the user to think of a number between min_ and max_ and then guesses the ...
2
votes
1answer
100 views
Binary search function
I have written this function to perform a binary search on an array, given begin/end indices and a value to look for in the supplied array:
...
3
votes
2answers
78 views
Binary search attempt
I've heard about the binary search in an intro class I took a couple months ago, remembered how it worked and read this article, so I attempted to write my first binary search in Python. It works, but ...