Category "algorithm"

Why the insertion sort is slower than selection sort?

The code for selection sort in Java: public class Selection { public static void sort(Comparable[] a) { int n = a.length; for (int i = 0; i

Finding union of 2 sorted arrays (with duplicates)

I'm trying to find the union of 2 sorted arrays (with duplicates) but I feel I'm not coming up with the most elegant code (what I have works btw, I just feel I

Python Connected Components edges list

I use these algorithms in python for finding connected components from edges. components = [] def connected_components(pairs): for a, b in pairs:

Neighboring gray-level dependence matrix (NGLDM) in MATLAB

I would like to calculate a couple of texture features (namely: small/ large number emphasis, number non-uniformity, second moment and entropy). Those can be co

Bilinear interpolation with non-aligned input points

I have a non-grid-aligned set of input values associated with grid-aligned output values. Given a new input value I want to find the output:     &nbs

Find number of int pair in an array that all values between the pair are smaller than or equal to the both

Given an array of integer, find the number of int pair, so that for each pair a_i, a_j, the number between them, i.e. a_(i+1)...a_(j-1) are all smaller than or

Covering segments by points

I did search and looked at these below links but it didn't help . Point covering problem Segments poked (covered) with points - any tricky test cases? Need eff

CountNonDivisible - Codility training task

I'm traning on codility now. Some tasks I can solve by myself, but with some tasks have problems. Difficulty of this task is <**>. It's medium, but I stal

Python 3: Insertion Sort comparisons counter

I need to add a counter of total comparisons for my Insertion Sort program but I don't know why I'm getting a total of 0 comparisons! I know that the compariso

convert cyclic to acyclic graph

I want to convert a cyclic graph into an acyclic graph. Is there an pseudocode that can do this? I did try to search but most of them returned where mathematica

Insertion sort error occurring in a C program

I am probably missing something simple here, but I was looking over some simple algorithms in C and have not been able to get the insertion sort in the code bel

java codility Frog-River-One

I have been trying to solve a Java exercise on a Codility web page. Below is the link to the mentioned exercise and my solution. https://codility.com/demo/res

Does printing a string really take O(n)?

I was doing an example from Cracking the Coding Interview and I read that executing System.out.println(prefix); (where prefix is a String) would take "O(n) time

How are the pheromone rules applied in the ant colony system?

I'm reviewing the paper of Dorigo & Gambardella (1997) on the ant colony system (ACS). There are two pheromone updating rules: local updating and global upd

(Python) Heapsort min heap implementation for Non Increasing Order Sorting. What am I doing wrong?

def min_heapify(A,k, n): left = 2*k +1 right = 2*k +2 n=int(len(A)) if left < n and A[left] < A[k]: smallest = left else:

kattis problem ABC with python3. Works fine in local compiler, but when on submission getting partially right

I am trying the kattis problem ABC (https://open.kattis.com/problems/abc). It works fine on my local compiler, but when I submit, I can't pass all the cases. Ca

How to improve Merge Sort speed in python

Yes, it is homework, but I ended up doing it in Java just to get it done, but now the python implementation is bothering me. I'm pretty sure I've implemented it

What's the difference between big O and big Omega?

Big Omega is supposed to be the opposite of Big O, but they can always have the same value, because by definition Big O means: g(x) so that cg(x) is bigger or

Zig-zag scan an N x N array

I have a simple array. The array length always has a square root of an integer. So 16, 25, 36 etc. $array = array('1', '2', '3', '4' ... '25'); What I do, is

Zig-zag scan an N x N array

I have a simple array. The array length always has a square root of an integer. So 16, 25, 36 etc. $array = array('1', '2', '3', '4' ... '25'); What I do, is