algorithm

Converting a decimal number into binary

I am currently reading Charles Petzold's book 'Code'. In it he explains how to convert a decimal number into binary using the following template: [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] ÷128 ÷64 ÷32 ÷16 ÷8 ÷4 ÷2 ÷1 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] In th...

StackOverflow's Popularity algorithm in MySQL

How would you write SO's Popularity algorithm in MySQL? The algorithm is detailed here: http://stackoverflow.com/questions/32397/popularity-algorithm. thanks! ...

Examples of Algorithms which has O(1), O(n log n) and O(log n) complexities.

What are some algorithms which we use daily that has O(1), O(n log n) and O(log n) complexities? ...

How can I modify my Shunting-Yard Algorithm so it accepts unary operators?

I've been working on implementing the Shunting-Yard Algorithm in JavaScript for class. Here is my work so far: var userInput = prompt("Enter in a mathematical expression:"); var postFix = InfixToPostfix(userInput); var result = EvaluateExpression(postFix); document.write("Infix: " + userInput + "<br/>"); document.write("Postfix (RPN):...

Modifying a convex hull to exclude unwanted points

Hi folks, In my C#-Silverlight 3 program, I have a set of points. Those points can be of a different color, green, red or blue. Then I create a convex hull for the different points: A hull for green, a hull for red and a hull for blue. Now it can happen, that within the hull of each color are points from another color, like red points w...

Linked list interview question

This may be the old, but I couldn't think of an answer. Say, there are two lists of different lengths merge at one point; how do we know where the merging point is? We don't know the length and we should parse each list only once. ...

Algorithm - Searching all loops in a network topology

I am given a network defined by nodes and links. I have to search all loops in the network. No coordinates will be given for the nodes. Is there any existing algorithm or library that can do this. Or can you please give me some idea how I can approach this problem? I am programming in .NET. I draw a diagram to illustrate what I need h...

merge heaps algorithm

is there an efficient algorithm for merging 2 max-heaps (stored as an array)? ...

JavaScript - find date of next time change (Standard or Daylight)

Here's a timely question. The rules in North America* for time change are: the first Sunday in November, offset changes to Standard (-1 hour) the second Sunday in March, offset changes to Daylight (your normal offset from GMT) Consider a function in JavaScript that takes in a Date parameter, and should determine whether the argument ...

Looking for a multidimensional optimization algorithm

Problem description There are different categories which contain an arbitrary amount of elements. There are three different attributes A, B and C. Each element does have an other distribution of these attributes. This distribution is expressed through a positive integer value. For example, element 1 has the attributes A: 42 B: 1337 C: ...

Various ways of implementing bubble sort?

How should I implement the classic bubble sort algorithm? I'd particularly like to see a C++ implementation, but any language (even pseudocode) would be helpful. P.S. Not a homework. ...

linear towers of hanoi

I have a question on the linear Towers of Hanoi. I implemented it in C++ but am trying to do the same using the tail recursive or iterative method. I am having trouble with my algorithm. This code snippet shows transferring blocks from the middle tower to the end tower. #include <stdlib.h> #include <stdio.h> using namespace std; //i...

What is the most elegant way of shell sorting (comb / diminishing increment sorting) in C#?

Hi SO, Is there a better way to shell sort using C#? // array of integers to hold values private int[] a = new int[100]; // number of elements in array private int count; // Shell Sort Algorithm public void sortArray() { int i, j, increment, temp; increment = 3; while( increment > 0 ) { for( i=0; i < count; i++ ) {...

Most prevalent substring of length X

Hi! I have a string s and I want to search the most prevalent substring of length X in s, overlap are admited. For example: s="aoaoa" and X=3, return "aoa" (which appear 2 times in s). Is it an algorithm that founds this substring in O(n)? ...

Designing a custom random class

Hi, I know C# has the Random class and probably a few classes in LINQ to do this, but if I was to write my own code to randomly select an item from a collection without using any built in .NET objects, how would this be done? I can't seem to nail the logic required for this - how would I tell the system when to stop an iteration and se...

Human inspector vs. programmer (was: Algorithm to detect if any circles overlap)

Problem This question actually came up at work today. We are planning an experiment where we will show a series of maps to users. Each map has 31 symbols on it. Each symbol also has a label. We noticed that, in a few cases, the labels overlapped, which rendered one of the labels unreadable. We ended up identifying the problem symbols t...

Algorithm for Finding Good, Reliable Players

I've the following players, each value corresponds to a result in percentage of right answers in a given game. $players = array ( 'A' => array(0, 0, 0, 0), 'B' => array(50, 50, 0, 0), 'C' => array(50, 50, 50, 50), 'D' => array(75, 90, 100, 25), 'E' => array(50, 50, 50, 50), 'F' => array(100, 100, 0, 0), 'G' =...

Scheduling algorithm/problem

hey Stackoverflowians, I'm bored, and this problem just haunted me again. Back at university, I used to always wonder how they schedule exams. The ability to schedule 10k student to do exams in 2 weeks and guarantee that no student will have an exam in two consecutive periods. I'm assuming some form of heuristics be applied. I'm bored ...

Convert Byte String to Int in Scheme

I have code like this to convert hex into byte string (define (word->bin s) (let ((n (string->number s))) (bytes (bitwise-and (arithmetic-shift n -24) #xFF) (bitwise-and (arithmetic-shift n -16) #xFF) (bitwise-and (arithmetic-shift n -8) #xFF) (bitwise-and n #xFF)))) (word->bin "#x10000002") I'm thinking of a similar...

Help understanding Sieve of Eratosthenes implementation

This is boring, I know, but I need a little help understanding an implementation of the Sieve of Eratosthenes. It's the solution to this Programming Praxis problem. (define (primes n) (let* ((max-index (quotient (- n 3) 2)) (v (make-vector (+ 1 max-index) #t))) (let loop ((i 0) (ps '(2))) (let ((p (+ i i 3)) (startj...