An algorithm is a precise plan (in the format of a sequence of steps) on how to solve a particular problem.

learn more… | top users | synonyms

3
votes
1answer
64 views

Determining average of integers using quicksort

I was attempting an online problem given on a programming website which asked us to tell us the number of integers in a given list which can be obtained as an average of any two other integers in that ...
2
votes
1answer
42 views

Binary HeapSort and Ternary HeapSort implementation

This is my take on binary and ternary heapsort implementation for a university assignment. The code works but I wonder if there are any mistakes or things to improve. ...
-1
votes
0answers
17 views
5
votes
1answer
56 views

Binary Search in Ruby

I want to write a search that raises an error if the list supplied is not sorted. ...
3
votes
1answer
64 views

Implementing a genetic algorithm to solve knapsack

I am trying to develop a genetic algorithm to solve knapsack problem(0-1). I am new to algorithm and programming as well. Here is my code and it works but I would like to know your suggestions of how ...
3
votes
2answers
118 views

Calculating sum of manhattan distances in a sliding puzzle

I would like some feed back on a method which calculates the sum of Manhattan distances for each tile in a sliding puzzle to its goal position in the goal puzzle. Here is the code: ...
6
votes
1answer
44 views

Tracking (uniform spacing) between 2D elements

The problem I am solving: I select multiple 2D SVG elements in an editor(the elements are in arbitrary positions on my canvas). I run the function ...
3
votes
0answers
25 views

BFS and DFS tree traversal

I posted some code as part of an answer to another question and I thought it would be a good idea to get that code reviewed also. Any comments are welcomed, but I am mostly annoyed by ...
1
vote
4answers
83 views

Find out max duplicate number between 1 to N numbers

How can I make this method better? ...
0
votes
1answer
37 views

Speeding up Mersenne prime generator

I am writing a program to calculate Mersenne primes. This code works, but it is so slow: ...
7
votes
4answers
282 views

Effectively calculate the result of geometric series

Given \$f(n) = 1 + x + x^2 + x^3 + \ldots + x^n\$ and the fact that computers take more time when multiplying two numbers than when adding, how can we work out the result with greater efficiency? ...
3
votes
1answer
32 views

Test if two nodes in binary search tree are cousins

Cousins are nodes at the same level of the tree, with different parents. They don't have to share the same grandparent. My solution depends on having a binary search tree with unique elements. This ...
3
votes
3answers
46 views

Hexadecimal to string without C++ standard library functions

For the purposes of this code review, I will use the standard library, but pretend that <cstddef> is <stddef.h>, ...
4
votes
1answer
35 views

Optimization of Barnes-Hut Multithread Insertion algorithm

I'm currently working on optimizing a Barnes-Hut implementation (connected to a previous post I did) and I need help to optimize it further. After some testing, the main problem appears to be in the ...
13
votes
1answer
107 views

Advanced and Detailed Minesweeper Probabilities

In an earlier question, I showed my code for calculating the mine probability for each and every field in a Minesweeper board. But it doesn't stop there. In that very same Minesweeper Analyze project, ...
4
votes
1answer
63 views

Functional Knapsack Problem in Python

This is the knapsack problem from rosettacode.org: A tourist wants to make a good trip at the weekend with his friends. They will go to the mountains to see the wonders of nature, so he needs to ...
4
votes
1answer
37 views

Graph (adjacency list) for BFS and DFS in Golang

I am trying to implement a Graph data structure in Golang and choose to use adjacency list representation. When starting to implement adjacency list, I have an idea ...
3
votes
2answers
128 views

Implementing a genetic algorithm to solve the Diophantine Equation

I am trying to implement a genetic algorithm to solve (Diophantine Equation). For instance, a + 2b + 3c + 4d = 90 where a, b, c, d are positive integers. After reading some books and following ...
5
votes
3answers
94 views

Finding the closest value in a collection in a game-server plugin implementation

I have an array of a instances of an enum type. A random double variable is initialized with a value between 0.... to 1, and should be used to get the closest ...
6
votes
2answers
94 views

Summator simulation

This is a reference implementation of a summation unit. The algorithm used is a most straightforward carry-propagation. If necessary, a test driving code could be provided. Few notes for the ...
5
votes
3answers
65 views

Bubble sorting in C

I've been into algorithms lately, so I wanted to apply a simple algorithm using a language of my choice, which was C in this case. I've implemented the bubblesort algorithm (for strings) in a simple ...
3
votes
2answers
101 views

Encryption-decryption method

My encryption algorithm I'm using this algorithm in order to encrypt notes users save on my site: ...
3
votes
1answer
46 views

Solving the Shortest Path problem (little bit of TSP, too)

Some background info: I'm working at a shipping company and the company's web developer has been fired a week ago. My boss knew that I had some knowledge in web development, so until we get a new ...
3
votes
1answer
32 views

Iterative Flood Fill implementation in C [closed]

I am trying to implement an iterative version of flood fill in C: ...
4
votes
1answer
51 views

Detecting connected acyclic through Tarjan strongly connected components variant

I want to verify that a given directed graph is connected and acyclic (DAG). I have implemented a modification of the Tarjan's strongly connected components algorithm in imperative style (as the ...
10
votes
3answers
1k views

Dining Philosophers Algorithm

Please visit the http://en.wikipedia.org/wiki/Dining_philosophers_problem for algorithm discussion. I have written below java program to solve this problem by Arbitrator solution algorithm mentioned ...
3
votes
1answer
64 views

K-Mean with Numpy

I have implemented the K-Mean clustering Algorithm in Numpy: ...
3
votes
2answers
91 views

Game of Life generation procession algorithm

I've tried to make Game of Life in console and this is the algorithm I used to generate new generation: ...
2
votes
2answers
66 views

Ruby Mod 11 implementation

I wrote this Ruby Mod 11 (perhaps it's a bit generous to call it that) implementation to validate Brazilian CPF documents, which use a Mod 11 algorithm. CPF format is a 9 digit number followed by two ...
5
votes
4answers
125 views

NULL-eating / elimination version of fgets() in C

I've come up with the following routine to read data from a file stream, but I don't want any NULLs in the stream to terminate the string early. This function will ...
4
votes
0answers
47 views

Equivalent binary trees (structure & values)

I am following the Golang tour and I have been asked to build a couple of functions to manipulate binary trees. My code runs as expected, but I would like to know if I could further improve my code. ...
1
vote
2answers
92 views

Large arrays make runtime very slow

So I have the following code that takes the input of two arrays, and apply some queries to match elements from DBpediaClassesArray with elements from ...
4
votes
1answer
50 views

Zeckendorf numbers the Clojure way

I'm just getting into Clojure, and I wanted to make sure I am writing code the "Clojure way". The challenge I took on is Zeckendorf numbers (fairly trivial). ...
7
votes
1answer
102 views

Topological Sort and Graphing in Python 3

I wrote a program for DFS traversal, topological sort, find all vertices, and has_cycle methods. Can you please suggest more elegant and eloquent ways for this program? (Perhaps better ways to ...
5
votes
1answer
60 views

Summing up distinct elements in steps (follow up)

This is a "follow up" to an answer I gave on this question : Summing up distinct elements in steps Here are the OP's requirements : "My current task is to find a score from an array where the ...
6
votes
1answer
79 views

Check a collection of prices against an associated collection of minimum prices

I would like to find a more efficient way to solve this problem. My current implementation has 3 separate loops through the data. I'm creating an algorithm to check a collection of prices against an ...
0
votes
2answers
102 views

Josephus Problem in Java

Problem The Josephus Problem is a famous mathematical puzzle that goes back to ancient times. There are many stories to go with the puzzle. One is that Josephus was one of a group of Jews who ...
2
votes
2answers
23 views

Performance of RegExp vs Rune Loop

I was recently talking to someone about a function I wrote to mimic (very basically) the string interpolation function String.Format() found in the C# language. ...
5
votes
1answer
100 views

Groupby in NumPy

To avoid the XY problem, here is an example of what I need. Given the sorted integer input array [1 1 1 2 2 3], I would like to produce the following slices, ...
4
votes
1answer
39 views

Stable partition in Numpy

In essence, I need to do a stable partition: All elements of the 1D np.array a that are present in ...
8
votes
1answer
75 views

Bitmap problem performance boost

Problem statement: Input is a rectangular bitmap like this: 0001 0011 0110 The task is to find for each black (0) "pixel", the distance to the ...
2
votes
0answers
41 views

Circle Shadow Experiment

I'm trying to create a light system, with the midpoint algorithm and Bresenham Line Algorithm. I'm trying to redraw all the time (1 option) or redraw when I need to. When the circles are too big it ...
8
votes
5answers
494 views

Program for finding Fibonacci primes

I think it could have been designed with more object orientation. I don't like how one of my methods calls another from within the method, but I wasn't sure how to return the result because it is a ...
0
votes
1answer
78 views

Find number of inversions in a given array

Can you please give me a hint on how to improve performance of the following algorithm meant to find the number of inversions in a given array? ...
4
votes
2answers
136 views

Reverse Game HackerRank Ruby Solution

Problem Statement Akash and Akhil are playing a game. They have N balls numbered from 0 to N−1. Akhil asks Akash to reverse the position of the balls, i.e., to change the order ...
2
votes
2answers
51 views

Swap Kth node from beginning with Kth node from end in a Linked List

Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This code is attributed to geeksforgeeks. I'm ...
7
votes
2answers
58 views

External / File-based mergesort

I've implemented an external mergesort to sort a file consisting of Java int primitives, however it is horribly slow (fortunately it does at least work). Very little happens in the sort method: it ...
4
votes
0answers
26 views

Fowler–Noll–Vo hash function in Lua

I recently coded this FNV-1a hash function in Lua. Are there any apparent performance improvements that could be implemented? ...
2
votes
4answers
588 views

Sorting Algorithms

A group of basic sorting algos. Based on Algorithms, 4th Edition - Robert Sedgewick | Kevine Wayne. Just making sure all of my logic and everything is correct. ...
2
votes
2answers
58 views

Find a triplet from three linked lists with sum equal to a given number

Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are ...