Reverse lookup for a sorted merged list

Given k sorted lists with comparable elements, where no two elements are equal, we could merge them into a new sorted list L by various algorithms. My question is, given the position of an element in this hypothetical list L, what is an efficient (be...
more »

2017-01-11 17:01 (1) Answers

Sorting Optimization

I'm currently following an algorithms class and thus decided it would be good practice to implement a few of the sorting algorithms and compare them. I implemented merge sort and quick sort and then compared their run time, along with the std::sort: ...
more »

2017-01-10 23:01 (2) Answers

Caching time series aggregates of diffs

Is it possible to precalculate (cache) aggregates (min/max/avg) of values which are a difference of two signals? I have several channels (e.g. 50), with one or more measurements each second, and I can easily store precalculated 1-minute or 15-minute...
more »

2017-01-08 20:01 (1) Answers

Binary tree - not able to add values

In the below program, I am trying to populate a BST with the values present in an array arr. The program seems to run in a loop for a long time and then I get a segmentation fault. Can somebody please explain what I am missing here? struct node { ...
more »

2017-01-07 19:01 (1) Answers

Sort an array of integers into odd, then even

I have a problem I found on an algorithm forum elsewhere. I have an array with random numbers (for example, [5, 2, 7, 9, 2, 3, 8, 4]) which should be returned to me sorted by odd then even. The order of the odds/evens is not important so in the exam...
more »

2017-01-07 09:01 (10) Answers

Why Is This Factorial Algorithm Not Accurate

Sorry I feel stupid asking this and am prepared to lose half of my points asking this but why does this algorithm not work? It works up to a point. After the number 13 the factorials are a little off. For instance the numbers do not entirely match in...
more »

2017-01-06 17:01 (5) Answers

Implementing merge sort in Java: Only zeroes

I'm trying to implement some sorting algorithms in Java working on int-arrays as an educational process. I currently try to wrap my head around merge sort. Yesterday I got pretty far, with a result of an array of the correct size, but only containing...
more »

2017-01-06 14:01 (3) Answers

Merge sort failing to work in N logN

I have written a merge sort algorithm, but it fails to sort in N log N. The code for sorting an array list is: void merge(int start, int mid, int end) { int i,j,q; for (i=start; i<=end; i++) l[i]=list[i]; i=start; j=mid+1...
more »

2017-01-05 14:01 (2) Answers

std::sort algorithms memory usage

I'm wondering whether the standard libraries sorting algorithms (e.g. std::sort) are using the heap memory for sorting. Is there any reliable source how to find out what kind (heap, stack) and how much of temporary memory is used by a sorting algor...
more »

2017-01-05 00:01 (1) Answers

Average face - algorithm

Quesion and code is at the end I hope this question belongs here and not to the TCS's stack. I am trying to go through algorithms in Turk and Pentland's "Eigenfaces for Recognition". On page 74 one can read (last paragraph of left column): Let t...
more »

2017-01-03 22:01 (1) Answers

Wagner Fischer algorithm + display steps

I made an implementation of Wagner Fischer algorithm in java with input cost, but I want to display all steps. I search but can't find any idea.After a long time I tried to keep each transformation in matrix alongside cost and to go through back to f...
more »

2017-01-02 21:01 (2) Answers

Normalize array values in natural order PHP

I have faced with the problem, I need to normalize/sort in natural order values in array after some item has been removed. Consider following example. Initial array { [313]=> int(2) [303]=> int(1) [295]=> int(3) [290]=> int(4) } Sort...
more »

2017-01-02 15:01 (1) Answers

What is the space complexity of this code?

int f(int n) { if (n <= 1) { return 1; } return f(n - 1) + f(n - 1); } I know that the time complexity is O(2^n) and I understand why. But I don't understand why the space complexity is O(n). I was told that it's be...
more »

2016-12-31 22:12 (3) Answers

Sort lines, part of a closed shape

I have an array of structures, each structure element holds the endpoints of lines in 3D space. struct POINTS { float xStart; float yStart; float zStart; float xEnd; float yEnd; float zEnd; }; Combining these lines will create a...
more »

2016-12-30 12:12 (1) Answers

Iterating through LinkedList in O(n)

I think I have this understanding right, but wanted to double check. Say we are iterating through a LinkedList like this: for(int i = 0 ; i < lst.size() ; i++) { System.out.println(lst.get(i)); } lst.get() runs in O(n) time, and we're callin...
more »

2016-12-28 15:12 (1) Answers

Divide and conquer trominos algorithm in C

This is the classic divide and conquer problem. We have a 2^n * 2^n board and we want to fill it with L shaped cubes. We know there is one block on the board that we can't assign a cube. This problem is also known as tromino problem (somewhat). Prob...
more »

2016-12-28 13:12 (1) Answers

Sort array with goal to minimize sequence repeat

Ok so I have an array of websocket client connections. Imagine that this array contains connections to several different machines. Imagine that each different letter (1,2,3, etc) represents a different host. It might look like this: const conns = [1...
more »

2016-12-28 05:12 (1) Answers

C: realloc works on Linux, but not on Windows

this is my first question on Stack Overflow, sorry if it's not well written. I have a little problem. I wrote a program in C (I'm currently learning C, I am a newbie, my first language, don't say I should've learnt Python, please, because I'm doing j...
more »

2016-12-27 18:12 (1) Answers