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
39 views

Quicksort using pointers

As an exercise, I've written quicksort algorithm in C using pointers. Please comment and help me find the cases where it breaks (if any). void qsort(int *, int, int); void swap(int *, int *); void ...
2
votes
0answers
62 views

ways to make my program shorter?

I've made a program that goes through your download directory (or any other directory you just have to change the path) and finds the files that matches your query then asks you to rename or remove ...
0
votes
0answers
30 views

Name of this algorithm (Create binary image) [migrated]

Some days ago I saw in this site the algorithm below to convert color images to binary images and I used it in my application (with some small changes). However, I need to know what is the name of the ...
2
votes
0answers
32 views

Ruby Branch and Bound Algorithm implementation?

I am relatively new to Ruby and am trying to get a handle on making my code more Ruby-esque. At the same time I am experimenting with the branch and bound algorithm to solve a problem that I outline ...
1
vote
2answers
51 views

Time limit of a c program while calculating factorial of numbers in c

I am solving a problem on calculation of factorial and the challenge is as follows! You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t<=100, ...
-1
votes
0answers
48 views

Euclid's Algorithm in Python [closed]

I am learning about primes and modulus arithmetic in math class so I decided to try to implement Euclid's algorithm in Python. I can't figure out why this doesn't work, but the Python interpreter does ...
0
votes
0answers
13 views

Suggestions for data extraction Data in fortran [migrated]

I use F95/90 and IBM compiler. I am trying to extract the numerical values from block and write in a file. I am facing a strange error in the output which I cannot understand. Every time I execute the ...
3
votes
1answer
96 views

Project Euler 407: Is there any more optimal way to solve this idempotent equation (modulo n ring)?

Project Euler problem 407: If we calculate a2 mod 6 for 0 ≤ a ≤ 5 we get: 0, 1, 4, 3, 4, 1. The largest value of a such that a2 mod 6 = a is 4. Let's call M(n) the largest value of a < n ...
-1
votes
0answers
45 views

CodeReview:Miller Rabin Primality Test algorithm implementation in C and python [closed]

I have written a python code implementing Miller Rabin Primality Test The code finds the largest prime less than the input. Here is a code snippet of primality testing algorithm def ...
1
vote
1answer
63 views

Create palindrome by rearranging letters of a word

Inspired by a recent question that caught my interest, I wrote a function in Python 3.3 to rearrange the letters of a given string to create a (any!) palindrome: Count the occurrences of each letter ...
0
votes
1answer
90 views

Deep copying objects with circular references

I've found a good example of how to do this in Java and how you can get the same behavior in c#, so I've made made an attempt at it: public class CircularReferenceOne { public int Identifier; ...
2
votes
1answer
69 views

General case of the 24-challenge problem

I'm working on an algorithm to solve the 24-challenge game. The basic idea is to combine positive integers using arithmetic operators to produce an expression that evaluates to exactly 24. For ...
3
votes
0answers
153 views

Connect Four AI (Minimax) in Clojure

I wrote a Connect Four game inlcuding a AI in Clojure and since I'm rather new to Clojure, some review would be highly appreciated. It can include everything, coding style, simplifications, etc. But ...
1
vote
2answers
79 views

Check if a Binary Tree <String> is aBinary Search Tree

i'm understanding the question but wanna make sure if my implementation is correct or not =| The Q is : Write a method that accepts as its argument a BinaryTree object and returns true if the ...
0
votes
0answers
76 views

Hanoi Towers - need help with the code

I made an algorithm solving Hanoi Tower puzzles, for n disks and m pegs. It uses lists as pegs, each list's element contains disks array - {0, 0, 0} means the peg is empty, {1, 2, 0} means the peg ...
-1
votes
1answer
35 views

C++: Last digit of an exponent of number - wrong answer [closed]

I'm trying to implement a simple program that takes base and exponent and output the last digit of a result of exponentiation, but online judge says that my program gives wrong answers. What could be ...
2
votes
1answer
133 views

Is this implementation of Kruskal's algorithm maintainable and efficient?

Update I've posted some updated code and included the definition of Vertex and Edge to try to answer as many questions as I could. To summarize what's changed: I've followed the advice here to ...
4
votes
2answers
102 views

Prime sieve: improve efficiency while keeping it reasonably simple?

public static void listPrimesThree(int maxNum){ long startTime = System.currentTimeMillis(); boolean[] booleanArray = new boolean[maxNum+1]; int root = (int)Math.sqrt(maxNum); for (int m = 2; m ...
0
votes
2answers
111 views

C++: Prime Number Generator algorithm optimization

I've implemented a simple program that finds all prime numbers between two integer inputs using Sieve of Eratosthenes, but online judge is still complaining that time limit is exceeding. Maybe I'm ...
0
votes
2answers
81 views

How to take the some elements of a list of random numbers and sort them?

I want to create a file consisting take rows of distinct numbers in ascending order. They are randomly taken from the first total integer. The file will be used to make an excerpt as discussed here. ...
1
vote
0answers
39 views

Can I make my render method more efficient?

I get great FPS with this as is, but I'm a performance freak and I don't know AffineTransform or the Java graphics libraries very well. I'm sure there is something I could be doing differently to make ...
6
votes
4answers
155 views

Readability and Performance of my Trie implementation

As an interesting exercise in Python 3.3, I implemented a Trie (also known as a Prefix Tree). Example usages Create from list of key-value-tuples mapping = (('she', 1), ('sells', 5), ('sea', 10), ...
2
votes
1answer
130 views

PHP - Alternated Caesar Cipher, code could be improved?

I've recently picked up PHP and due to having an IT-security class I tried out to code the first assignment in PHP. The algorithm I took upon is an alternation of the caesar cipher, instead of using a ...
3
votes
1answer
45 views

Finding missing items in an int list

Here is a problem I am trying to solve: trying to find missing photographs from a sequence of filenames. The problem boils down to: given an unsorted list of integers, return a sorted list of missing ...
1
vote
1answer
51 views

Queue implementation Doubly Linked List

Here is my implementation for a queue using doubly linked list: QUEUE-EMPTY if L.head == NIL return True else return False QUEUE(x): if L.head == NIL: x.prev = NIL L.head = x else ...
1
vote
0answers
77 views

new md5 algorithm

can you please try my algorithm and give a comment or feedback about it? thank you in advance.. :) this is in java language just copy and paste all of it and run in e.g. netbeans or eclipse thanks ...
4
votes
1answer
139 views

Correct implementation of a markov-chain?

I read about how markov-chains were handy at creating text-generators and wanted to give it a try in python. I'm not sure if this is the proper way to make a markov-chain. I've left comments in ...
1
vote
0answers
29 views

Is this code for getting the dependency tree of a file correct?

function dep_tree(graph,node,prev_deps){ return uniq(flatten(graph[node].map(function(child_node){ if (contains(prev_deps,child_node)) throw "Circular reference between ...
2
votes
1answer
62 views

Iterative Collatz with memoization

I'm trying to write efficient code for calculating the chain-length of each number. For example, 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1. It took 13 iterations ...
1
vote
3answers
159 views

More efficient way to retrieve first occurrence of every unique value from a csv column?

A large .csv file I was given has a large table of flight data. A function I wrote to help parse it iterates over the column of Flight ID's, and then returns a dictionary containing the index and ...
1
vote
1answer
79 views

Three pitchers with water problem?

I don't know what this problem is named, so i can't Google for a solution about it. Here's the image: There are three pitchers with capacities of 10, 7 and 3 quarts. We need to move the water from ...
0
votes
2answers
176 views

Recursive bubble sort in Java

I want to know if someone can suggest how I can remove the initialValue parameter of my method which sorts an array: public static void bubble(int[] array, int initialValue) { int aux; for ...
3
votes
2answers
185 views

Given an array find any three numbers which sum to zero

Looking for optimizations and cleaner, more pythonic ways of implementing the following code. #Given an array find any three numbers which sum to zero. import unittest def sum_to_zero(a): ...
1
vote
0answers
138 views

Density-based clustering of image keypoints

I have implemented the DBSCAN algorithm for clustering image keypoints, I'm using C++ and OpenCV, I have been following the pseudocode on the wiki page pretty strictly and its working but I get the ...
5
votes
2answers
212 views

Better way to write the following code?

Is there a better, more elegant solution to the following method? Boolean[] spades = new Boolean[10]; // 40 cards deck foreach (Card card in m_Cards.Where(card => (card.Suit == Suit.Spades))) ...
2
votes
1answer
96 views

Leap year algorithm?

I wrote two functions for determining leap years. ("Kabisat" means "leap year" in Indonesia.) def kabisat? y return false if y%4!=0 return true if y%100!=0 y%400==0 end ...
1
vote
0answers
93 views

How to optimize this algorithm? (solving systems of linear equations)

I am working on a programming challenge described here: http://www.enigmagroup.org/missions/programming/9/ Basically, you have a 6x6 table where the first 5 columns and rows are comprised of ...
3
votes
2answers
116 views

remove duplicates in a sorted array if duplicates are allowed at most twice

Given a sorted array, remove the duplicates in place such that each element appear at most twice and return the new length. Do not allocate extra space for another array, you must do this in place ...
0
votes
0answers
16 views

remove duplicates in a sorted array [duplicate]

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with ...
2
votes
1answer
112 views

Is this a good encryption algorithm?

I've never had a serious attempt at writing an encryption algorithm before, and haven't read much on the subject. I have tried to write an encryption algorithm (of sorts), I would just like to see if ...
3
votes
4answers
117 views

Finding unconnected sets

I have a list of boxes, and wish to group them into unconnected sets of overlapping boxes. (Note that two boxes A and B may not overlap each other, but if they are both overlapped by a box C, they ...
0
votes
1answer
53 views

Creating an Array of Linked Lists from a BST

Question Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (eg, if you have a tree with depth D, you’ll have D linked lists) Here is my ...
3
votes
1answer
82 views

Challenge: solution for this deep-iteration JavaScript function dilemma?

Trying to implement a deep-iteration function with some constraints, I've noticed the faster solution is iterating it twice! Any attempt to avoid this resulted in slower code. So, the challenge: can ...
4
votes
2answers
73 views

Using standard library to simplify pairwise iteration of container values

I came up with this code whilst answering this question. Is there a simpler way of doing this using standard library? I want to iterate over every object and do something with every other object. ...
4
votes
4answers
201 views

Find the 2 distinct highest values in a array

Good Night I want to know if there is a better way to improve my code to find two highest in a array, and these numbers need to be distinct. I don´t want to sort the values. Only run in the array ...
1
vote
2answers
79 views

Calculate difference of two ranges given

Write a method to compute the difference between two ranges. A range is defined by an integer low and an integer high. A - B (A “minus” B) is “everything in A that is not in B”. This is an interview ...
4
votes
2answers
111 views

Move object by one up or down algorithm in a custom order

Basically, I did an object (using hibernate) with a field called sorting_order. This field needs to be unique and I wish to swap two object by one. So one element has to be after or before the current ...
-1
votes
1answer
56 views

Project RGB with Switches [closed]

I am working on project in which I need to display different colors on RGB led. I am using pwm to drive different colors on LED. My Pic is PIC24FJ64GA004 with which I am working on now. Basic concept ...
4
votes
2answers
126 views

Merge sort in scala

I've implemented merge sort in scala: object Lunch { def doMergeSortExample() = { val values:Array[Int] = List(5,11,8,4,2).toArray sort(values) printArray(values) } def ...
2
votes
2answers
105 views

Need a suggestion for invert index structure in C++

I am trying to create an invert index structure in C++. An invert index, in this case, refers to image retrival system, where each invert index refers to a numbers of "Patch ID". (simply called "ID" ...

1 2 3 4 5 7