algorithm

Which sorting algorithm is used by STL's list::sort()?

I have a list of random integers. I'm wondering which algorithm is used by the list::sort() method. E.g. in the following code: list<int> mylist; // ..insert a million values mylist.sort(); EDIT: See also this more specific question. ...

Which sorting algorithm is used by Microsoft's STL::list::sort()?

Note: I accidentally posted this question without specifying which STL implementation I was using, and I felt it can't really be updated since it would render most of its answers obsolete. So, the correct question goes - which sorting algorithm is used in the below code, assuming I'm using the STL library of Microsoft Visual C++?: list...

Synchronizing filesystem and cached data on program startup

I have a program that needs to retrieve some data about a set of files (that is, a directory and all files within it and sub directories of certain types). The data is (very) expensive to calculate, so rather than traversing the filesystem and calculating it on program startup, I keep a cache of the data in a SQLite database and use a Fi...

Iterative Cartesian Product in Java

Hi, I want to compute the cartesian product of an arbitrary number of nonempty sets in Java. I've wrote that iterative code... public static <T> List<Set<T>> cartesianProduct(List<Set<T>> list) { List<Iterator<T>> iterators = new ArrayList<Iterator<T>>(list.size()); List<T> elements = new ArrayList<T>(list.size()); List<Set<T>> toR...

Classify array of strings based on commonalities

I have huge list (200000) of strings (multi word). I want to group these strings based on comman array of word match among these strings. I cant think of a low computation time algorithm for this "AB 500" "Bus AB 500" "News CA" "News CA BLAH" My plan was a. Tokenize them to words. b. Create a global array tokens c. Compare those...

What is the best argorithm for finding the closest color in an array to another color?

I have a color (RGB) being read from a sensor. I also have a list of "known" colors, each paired with a string name. What would the best way (ie behave like a human choosing colors) to pull the name of the nearest color out of this list? I've tried a shortest cartesian distance with RGB, but that makes grey closer to green than to blac...

How implement Rollback feature ?

I want to create a C# application in which copy some files in two diffrent folders(already contains older version files) and also run sql scripts. During whole process if any exception generate i need to rollback all the changes. For sql scripts, transation can be used but how implement files copying process with rollback? ...

From an interview: Removing rows and columns in an n×n matrix to maximize the sum of remaining values

Given an n×n matrix of real numbers. You are allowed to erase any number (from 0 to n) of rows and any number (from 0 to n) of columns, and after that the sum of the remaining entries is computed. Come up with an algorithm which finds out which rows and columns to erase in order to maximize that sum. ...

How to easily know if a maze has a road from start to goal?

I implemented a maze using 0,1 array. The entry and goal is fixed in the maze. Entry always be 0,0 point of the maze. Goal always be m-1,n-1 point of the maze. I'm using breadth-first search algorithm for now, but the speed is not good enough. Especially for large maze (100*100 or so). Could someone help me on this algorithm? Here is my...

Algorithm and data structure learning resources for dynamic programming

Im learning dynamic programming now, and while I know the theory well, designing DP algorithms for new problems is still difficult. This is what i would really like now- A book or a website, which poses a problem which can be solved by dynamic programming. Also there is the solution with an explanation available, which i would like to ...

Logic for a file compare

I trying to write a programm for file compare. For example: file1 1 2 3 4 5 file2 1 2 @ 3 4 5 If I do it line by line, I get: 1 == 1; 2 == 2; 3 != @; 4 != 3; 5 != 4; != 5; But, the truth is that the only difference between the files is @. I want get something like this: 1 == 1; 2 == 2; != @; 3 == 3; 4 == 4; 5 == 5; Whic...

Looking for an implemenentation of binomial tree in c++

If anyone can point me into direction where i can find an easy to understand impl. of binomial tree, that would be very helpful. thx tree should look like in this article: http://software.intel.com/en-us/articles/high-performance-computing-with-binomial-option-pricing-part-1/ ...

Determining child interface closest to a class

Lets say I have a inheritance tree of interfaces: IParent > IChild > IGrandChild How would I: Find a class that implements IParent Determine the closest ancestor of the class that is also a child of IParent. For example: var myClass = FindImplementor<IParent>(); var myInterface = ClosestAncestor<IParent, myclass>(); I'm not look...

Clarification on FFT

I know there is something wrong with the following reasoning but I'm not sure what it is. The FFT: given two polynomials A = a_0 + a_1 x + a_2 x^2 + ... + a_n x^n and B = b_0 + b_1 x + b_2 x^2 + ... + b_n x^n you can compute the coefficients of the product AB = \sum _k = 0 ^ 2n ( \sum _ j = 0 ^ k (a_j b_{k-j}))x^k in O(n lo...

Is it correct to ask to solve an NP-complete problem on a job interview?

Today there was a question on SO, where the author was given an NP-complete problem during an interview and he obviously hadn't been told that it was one. What is the purpose of asking such questions? What behavior does the interviewer expect when asking such things? Proof? Useful heuristics? And is it even legitimate to ask one if ...

Blend mode on a transparent and semi transparent background.

In general, the "normal" blend mode equation looks like this: D = Sa * S + D * (1.0 - Sa) where D is destination color, Sa is source alpha and S is source color. Now, this works fine with fully opaque destination but I'd like to know how you would handle that with semi and fully transparent destination. When blending the source over...

Prime Factorization

I have recently been reading about the general use of prime factors within cryptography. Everywhere i read, it states that there is no 'PUBLISHED' algorithm which operates in polynomial time (as opposed to exponential time), to find the prime factors of a key. If an algorithm was discovered or published which did operate in polynomial t...

Distributed system design using only C

Hi, I have the work of implementing a distributed system of nodes (like p2p nodes) each of these nodes (lets say A,B,C and D) perform certain functions and need to interact with each other for various operations such as synchronize operations and other things like 15 A nodes interact with a group of 5 B nodes to get into the least loade...

improving search times

Hi guys, Just wondering if there is any tips on improving search times (full-text). How do large sites like stackoverflow, reddit, etc, implement their search functions? (Sorry for the vagueness - i am a newbie) ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone's keypad. i.e. for 1,0-> No Letter for 2-> A,B,C So for example, 1230 ADG BDG CDG AEG.... Whats the best solution in c/c++ to this problem? ...