algorithm

What's the best way to sort a two-dimensional matrix by values?

I have a two-dimensional array: function getMatrix(size) { var matrix = []; for (var i = 0; i < size; i++) { matrix[i] = new Array(size); } return matrix; }; It is filled with numeric values, so every existing matrix[i][j] is a Number. What is the best way to get a sequence of i and j pairs that will correspon...

Algorithm to find the simplest combination of integers that has not yet been used

I am looking for an algorithm for finding the simplest combination of integers from 0 to 5 (that is the one that consists of the fewest number of integers) that has not yet been used (the used combinations are in a list). The order does matter and the combinations should be returned in a list. For example, the list with the used number...

Find a median in parallel

If you have one huge amount of numbers and one hundred computers, How would you find the median of the numbers? ...

Algorithm to add new point to a polygon.

Greetings all, In the application I am developing I have polygons as show in the picture My data structure is double-linked list as follows. RzCurve { RzNode *head; }; RzNode{ double x; double y; RzNode *next; RzNode *prev; } I want to implement an algorithm which allows user to add a new Node by clic...

Getting all possible permutations from a list of numbers

I'm looking for an efficient way to achieve this: you have a list of numbers 1.....n (typically: 1..5 or 1..7 or so - reasonably small, but can vary from case to case) you need all combinations of all lengths for those numbers, e.g. all combinations of just one number ({1}, {2}, .... {n}), then all combinations of two distinct numbers ...

Brute-force sudoku solver: backtracking?

An implementation of a brute-force algorithm to solve Sudoku puzzles fails if a cell is discovered in which placing any of the digits 1-9 would be an illegal move. The implementation is written in C, with the board represented by a 9x9 array. The solver counts down from 9 until a legal number's reached, and if none can be reached, it ou...

What is a good algorithm for showing the most popular blog posts?

I'm planning on developing my own plugin for showing the most popular posts, as well as counting how many times a post has been read. But I need a good algorithm for figuring out the most popular blog post, and a way of counting the number of times a post has been viewed. A problem I see when it comes to counting the number of times a ...

Merge sorted arrays - Efficient solution

Goal here is to merge multiple arrays which are already sorted into a resultant array. I've written the following solution and wondering if there is a way to improve the solution /* Goal is to merge all sorted arrays */ void mergeAll(const vector< vector<int> >& listOfIntegers, vector<int>& result) { int totalNumbers = listOf...

Suppose I had a list in Python. What's the most pythonic and efficient way to randomize it by sections?

I have a list of X items. I want the first 10 to be randomized. Then, the 2nd 10 items to be randomized. Then the 3rd. What's the most efficient way to do this? ...

Array Division - What is the best way to divide two numbers stored in an array?

I have two arrays (dividend, divisor): dividend[] = {1,2,0,9,8,7,5,6,6}; divisor[] = {9,8}; I need the result (dividend/divisor) as: quotient[] = {1,2,3,4,5,6,7}; I did this using array subtraction: subtract divisor from dividend until dividend becomes 0 or less than divisor, each time incrementing quotient by 1, but it takes a...

tries data structure implementation......... Application - Dictionary............

Hi, Wanted to write a program to implement a dictionary of words using Tries Data Structure. Please tell me the structure for the implementation so that I could start the program, as i haven't got any article on internet, for Tries Implementation.. The confusion is, that when we search through the word, and we get through the word at...

Algorithm to stop flickering (clear box and re-populate) when adding text

Hey all, I was wondering if there's a some algorithm or resource that will look at the changes made from an AJAX request and not update the whole box (causing a flicker) but just add/remove the changes made. This is specifically used with the keypress command. A good example is stack overflows "preview" it doesn't flicker when you ad...

How can I improve this program to get 24 with 4 numbers?

24 points is small game. You must use + - * / to get the result 24 from 4 numbers. I wrote a Perl script to solve this problem. But I feel my code is too long and it looks like C. I hope someone can give me some advice. Thank you very much! Other language are possible, too, for example Python, Scala, F#, C++. my @test_arr = (10, 4...

Efficient ways to sort a deck of actual cards

I often have to sort decks of cards. These are "collector" cards numbered from 1 to 216 and there are doubles and missing numbers. I'm looking for sorting algorithms that work well with physical cards. Insertion sort seems fine because inserting a card does not require a shift of the subsequent cards like in a computer memory. However, ...

axis‐aligned rectangles intersection

I need an algorithm that takes an unsorted array of axis aligned rectangles and returns any pair of rectangles that overlaps Each rectangle has two variables, coordinates of the upper-left corner and the bottom-right corner ...

Python list, lookup object name, efficiency advice

Suppose I have the following object: class Foo(object): def __init__(self, name=None): self.name = name def __repr__(self): return self.name And a list containing multiple instances, such as: list = [Foo(name='alice'), Foo(name='bob'), Foo(name='charlie')] If I want to find an object with a given name, I could use the ...

Optimal algorithm to calculate the result of a continued fraction

A continued fraction is a series of divisions of this kind: depth 1 1+1/s depth 2 1+1/(1+1/s) depth 3 1+1/(1+1/(1+1/s)) . . . . . . . . . The depth is an integer, but s is a floating point number. What would be an optimal algorithm (performance-wise) to calculate th...

Interview: Find shortest path to few elements

There is a museum organized as NxN room. Some rooms are locked and inaccessible. Other rooms are open and some rooms have guards. Guards can only move north, south, east and west, only through open rooms and only within the museum. For each room, find the shortest distance to a guard. What is the time complexity of your algorithm? ...

Interview: lists intersection with limited memory

You are given two sets of integers, sizes M and N with M < N. Perform inner equal join on these two sets (i.e., find intersection of the two lists). How to perform it if both the lists are in files and the available memory is of size K < M < N ...

Permutation/Algorithm to Solve Conditional Fill Puzzle

I've been digging around to see if something similar has been done previously, but have not seen anything with the mirrored conditions. To make swallowing the problem a little easier to understand, I'm going to apply it in the context of filling a baseball team roster. The given roster structure is organized as such: C, 1B, 2B, 3B, SS...