algorithm

Algorithm to return all combinations of k elements from n

I want to write a function that takes an array of letters as an argument and a number of those letters to select. Say you provide an array of 8 letters and want to select 3 letters from that. Then you should get: 8! / ((8 - 3)! * 3!) = 56 Arrays (or words) in return consisting of 3 letters each. ...

What is a good algorithm for compacting records in a blocked file?

Suppose you have a large file made up of a bunch of fixed size blocks. Each of these blocks contains some number of variable sized records. Each record must fit completely within a single block and then such records by definition are never larger than a full block. Over time, records are added to and deleted from these blocks as records ...

Decoding letters ('a' .. 'z') from a bit sequence without waste

I seek an algorithm that will let me represent an incoming sequence of bits as letters ('a' .. 'z' ), in a minimal matter such that the stream of bits can be regenerated from the letters, without ever holding the entire sequence in memory. That is, given an external bit source (each read returns a practically random bit), and user input...

Algorithm for implementing C# yield statement

I'd love to figure it out myself but I was wondering roughly what's the algorithm for converting a function with yield statements into a state machine for an enumerator? For example how does C# turn this: IEnumerator<string> strings(IEnumerable<string> args) { IEnumerator<string> enumerator2 = getAnotherEnumerator(); foreach(va...

image archive VS image strip

Hi, i've noticed that plenty of games / applications (very common on mobile builds) pack numerous images into an image strip. I figured that the advantages in this are making the program more tidy (file system - wise) and reducing (un)installation time. During the runtime of the application, the entire image strip is allocated and copi...

How can Google be so fast?

What are the technologies and programming decisions that make Google able to serve a query so fast? Every time I search something (one of the several times per day) it always amazes me how they serve the results in near or less than 1 second time. What sort of configuration and algorithms could they have in place that accomplishes this...

How do I sort a list of integers using only one additional integer variable?

How to sort list of values using only one variable? EDIT: according to @Igor's comment, I retitled the question. ...

Balancing a Binary Tree (AVL)

Ok, this is another one in the theory realm for the CS guys around. In the 90's I did fairly well in implementing BST's. The only thing I could bever get my head around was the intricacy of the algorithm to balance a Binary Tree (AVL). Can you guys help me on this? ...

Red eye reduction algorithm

I need to implement red eye reduction for an application I am working on. Googling mostly provides links to commercial end-user products. Do you know a good red eye reduction algorithm, which could be used in a GPL application? ...

How would you sort 1 million 32-bit integers in 2MB of RAM?

Please, provide code examples in a language of your choice. Update: No constraints set on external storage. Example: Integers are received/sent via network. There is a sufficient space on local disk for intermediate results. ...

Should one prefer STL algorithms over hand-rolled loops?

I seem to be seeing more 'for' loops over iterators in questions & answers here than I do for_each(), transform(), and the like. Scott Meyers suggests that stl algorithms are preferred, or at least he did in 2001. Of course, using them often means moving the loop body into a function or function object. Some may feel this is an unacce...

Optimal method to perform a best levenshtein match against Map in Java

Hello, I have a map in Java. I would like to compare a source string against all items in the map and return the best match based on a levenshtein ratio algorithm. I am wondering what the optimal way to perform this check on every element in the list would be. Thanks, Matt ...

Is there a built in method for converting radians to degrees?

I run into this occasionally and always forget how to do it. One of those things that pop up ever so often. Also, what's the formula to convert angles expressed in radians to degrees and back again? ...

best way to pick a random subset from a collection?

I have a set of objects in a Vector from which I'd like to select a random subset (e.g. 100 items coming back; pick 5 randomly). In my first (very hasty) pass I did an extremely simple and perhaps overly clever solution: Vector itemsVector = getItems(); Collections.shuffle(itemsVector); itemsVector.setSize(5); While this has the adva...

Could a truly random number be generated using pings to psuedo-randomly selected IP addresses?

The question posed came about during a 2nd Year Comp Science lecture while discussing the impossibility of generating numbers in a deterministic computational device. This was the only suggestion which didn't depend on non-commodity-class hardware. Subsequently nobody would put their reputation on the line to argue definitively for or ...

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7

What is simple solution What is effective solution to less minimum memory and(or) cpu speed? ...

Balanced Distribution Algorithm

Hello, I'm working on some code for a loosely coupled cluster. To achieve optimal performance during jobs, I have the cluster remap its data each time a child enters or exits. This will eventually be made optional, but for now it performs its data balancing by default. My balancing is basically just making sure that each child never has...

Algorithm to score similarness of sets of numbers

What is an algorithm to compare multiple sets of numbers against a target set to determine which ones are the most "similar"? One use of this algorithm would be to compare today's hourly weather forecast against historical weather recordings to find a day that had similar weather. The similarity of two sets is a bit subjective, so the ...

Writing a Domain Specific Language for selecting rows from a table

I'm writing a server that I expect to be run by many different people, not all of whom I will have direct contact with. The servers will communicate with each other in a cluster. Part of the server's functionality involves selecting a small subset of rows from a potentially very large table. The exact choice of what rows are selected wil...

How can I programmatically determine how to fit smaller boxes into a larger package?

Does anyone know of existing software or algorithms to calculate a package size for shipping multiple items? I have a bunch of items in our inventory database with length, width and height dimesions defined. Given these dimensions I need to calculate how many of the purchased items will fit into predefined box sizes. ...