Seg fault on binary search tree - big sorting

Trying to do this Big Sorting problem on Hackerrank. I know there are simpler ways to write the algorithm, but I am trying to brush up on my C knowledge, and as such wanted to challenge myself and write a Binary Search Tree for the problem. The bina...
more »

2017-09-16 20:09 (2) Answers

Rearranging an array of integers

I need to implement the following in both pseudocode and java. Input: an array of integers Output: Rearrange the array to have the following: suppose the first element in the original array has the value x In the new array, suppose that x is in p...
more »

2017-09-15 23:09 (3) Answers

how to sort array in ruby by multi-field

I have an array like: - {"name"=>“A”, "10"=>30, "2" =>40, "90"=>0} {"name"=>“B”, "10"=>20, "2" =>40, "90"=>0} {"name"=>“C”, "10"=>20, "2" =>40, "90"=>10} --- I suppose to f...
more »

2017-09-15 23:09 (2) Answers

Merge Sort in Java not working as expected. Trying to do it without a while loop

I've been working on this all day and have no idea why this isn't working. The sort is duplicating some elements and is sorting some of the array but I can't see what is causing this. Tried to trace it but couldn't follow it. import java.io.FileNot...
more »

2017-09-13 21:09 (1) Answers

Cut and Stack the Array

I have an array of int as input. The array will always be a length of 2^n. I want to cut the array in half and stack it. Repeat cutting and stacking until there is only one stack. For example: int[] array = {1,2,3,4,5,6,7,8} if we cut the array in ...
more »

2017-09-13 08:09 (2) Answers

Algorithm for nesting nesting sets

I have a large collection of sets, some of which are subsets of each others, like: [{1, 2, 3, 4}, {1, 2}, {1, 5}, {1, 2, 3, 4, 5}, {2, 6}] I'd like to take this collection and output a DAG of partial order of the subset relations {1, 2, 3, 4, 5} ...
more »

2017-09-13 05:09 (0) Answers

Sort array using two thirds algorithm

I came across an uncommon sorting algorithm. The algorithm sorts the first 2/3rd of an array then next 2/3rd of an array and again sorts the first 2/3rd of an array recursively. fun sort3(a: int list): int list = case a of nil => nil | [x...
more »

2017-09-12 00:09 (0) Answers

Insert an element into an array sorted according to frequency, then sort the array by frequency again

So I was asked asked to write an O(n) function, insertRanked(int[] list, int item), to insert an element into an array sorted by frequency (I have written a boolean function to check if int[] list is sorted by frequency). After inserting the element ...
more »

2017-09-11 21:09 (2) Answers

Best Data Structure for fast retrival, update, and keeping ordering

The problem is as follows I need to keep track of url + click count. I need to be able to update url quickly with click count when user click on that url. I need to be able to retrieve the top 10 click count URL quickly. NOTE: Assuming you cannot...
more »

2017-09-11 19:09 (5) Answers

How to sort by best match?

I have a query where I want results to match as close to the conditions as possible. For example If I have a condition "a" = 500, then the returned results should be sorted such that I'll get 500, 499, 501, 498, 502 and so on..expanding from the pro...
more »

2017-09-11 16:09 (2) Answers

(merge sort) is log from n log n on base 2?

I've learned in class that merge sort is O(n · log(n)), but I'm not clear on whether it's O(n · log2(n)) or O(n · log10(n)). I've read on Stack Overflow (at Big O notation Log Base 2 or Log Base 10) that "it does not ...
more »

2017-09-10 18:09 (3) Answers

Counting sort in C implemented according to pseudocode but doesn't run properly

I have implemented counting sort according to its pseudocode ( that's written on the blackboard in this video explanation ) but for some mysterious reason, it doesn't seem to sort properly. For the test input: 10 9 8 7 6 5 4 3 2 1 It gives: 3 4 5 6...
more »

2017-09-10 13:09 (1) Answers

Determine number of leading zeros in a floating point number

How can I calculate how many zeros come after the decimal point but before the first non-zero in a floating point number. Examples: 0 -> 0 1 -> 0 1.0 -> 0 1.1 -> 0 1.01 -> 1 1.00003456 ->4 Intuitively I assume there is a math fun...
more »

2017-09-10 03:09 (4) Answers

Changing this Quicksort implementation's comparator from <= to < causes infinite recursion. Why?

I'm studying the Quicksort implementation below (from Cracking the Coding Interview). In the partition method, there are two "left <= right" predicates (in its first while-statement and last if-statement). When left == right, swapping elements at...
more »

2017-09-09 23:09 (1) Answers

java.lang.IllegalArgumentException: Comparison method violates its general contract when I'm implementing sort() in Graham Scan Algo

private static void sort() { Arrays.sort(points, 1, 100, new Comparator<P>() { @Override public int compare(P p1, P p2) { double cp = cross_product(points[0], p1, p2); return cp > 0 ? -1 : 1; ...
more »

2017-09-09 19:09 (0) Answers

Haskell Recursion Schemes: Traverse two structures simultaneously

I'm trying to write Robinson's unification algorithm using recursion schemes. The unification algorithm takes two types and spits a result. A type is a: data TypeF a = TypeApplication a a | TypeVariable Name deriving (Read,Show,Eq,Functo...
more »

2017-09-09 13:09 (1) Answers

Big-O and Omega Notations

I was reading this question Big-O notation's definition. But I have less than 50 reputation to comment, so I hope someone help me. My question is about this sentence: There are many algorithms for which there is no single function g such tha...
more »

2017-09-09 12:09 (2) Answers

Algorithm to split people into groups with most diversity per group

I'd like an algorithm to put people into groups for an upcoming conference. There's lots of people going, from different regions, departments, genders etc, and they want to split people up as much as possible so get diversity in each group. So is th...
more »

2017-09-08 13:09 (2) Answers

Code refactoring, how to disintegrate two two static functions without making parent functions non static

Context : We are refectoring the code in order to move to micro services. We've multiple products(A, B, C and some common code for A,B,C in monolithic service). now we creating new sandbox for common code. Problem : User.java Class U...
more »

2017-09-07 17:09 (1) Answers

Is the actual performance difference between these two O(n^2) algorithms coming from cache/memory access?

I wrote two methods to sort a list of numbers, they have the same time complexity: O(n^2), but the actually running time is 3 times difference ( the second method uses 3 times as much time as the first one ). My guess is the difference comes from th...
more »

2017-09-07 13:09 (1) Answers

Infinite loop in iterative implementation of QuickSort?

I'm trying to implement an iterative QuickSort using Lomuto's Partitiong method, for that reason I'm trying to implement a stack that holds a pair of indexes defining sub-array to be partitioned, using an array of struct with two fields: iBeg, iEnd a...
more »

2017-09-06 22:09 (1) Answers

Optimize recursive string manipulation function with JavaScript

Problem I was given this problem in my Algorithms class today: Given function maxSubstring(s, t), where s is a string and t is a substring of s, find the maximum number of iterations you can delete either the first or last occurrence of substrin...
more »

2017-09-06 21:09 (1) Answers

Haskell - how to avoid messing pure with IO

I am implementing some algorithm on haskell. This algorithm requires generating some data. I have a function of an algorithm which takes generation function as a parameter. For example, algorithm is just multiplying input data by n: algo :: a ->...
more »

2017-09-06 17:09 (2) Answers

Fibonacci Sequence using Cache

I am trying to make use of a cache to improve the performance of my Fibonacci method. However, it is still taking a lot of time to compute even fibonacci(40). import java.util.Scanner; public class FibWithCache { public static void main(String ...
more »

2017-09-06 13:09 (2) Answers

recursion in my quicksort

I'm currently trying to implement quicksort on an array in javascript. I have the overall layout but for some reason the recursion isn't working. It seems to have worked for the 2nd iteration of the code but after that, it seems to just mess up. Not ...
more »

2017-09-06 05:09 (2) Answers

Find a day of week for given first day in month

How could be calculated a day of the week if we know the day number of the first day in month? Lets say we have 1..7 days in a week I want to get number of the 4th day in the month if the 1st = 5 (Friday) then result should be 1 (Monday). 1st - 5 F...
more »

2017-09-05 23:09 (1) Answers

Random numbers external sort

I need to write a program that generates N random numbers and write them to binary file in descending order. It should be done without using any of sorting algorithms that use main memory. This is what I've done so far: #include <iostream> #i...
more »

2017-09-05 17:09 (3) Answers

Best way to sort a data structure of assorted objects by one sort variable?

Given a list/array of objects containing some other objects, I'm looking to sort by using one of the parameters. For example, if I have a multi-dimensional array of ints and I want to sort by their midpoint. The way I'm doing it is to create an anon...
more »

2017-09-05 09:09 (2) Answers

Maximum OR values in all subsets of array

Given an array of n integers. Find the minimum length of the subset with the maximum OR value among all subsets of the given array. How to go about solving this?? ...
more »

2017-09-04 17:09 (1) Answers

std::list sort algorithm runtime

I have a list of elements that is almost in the correct order and the elements are just off by a relatively small amount of places compared to their correct position (e.g. no element that is supposed to be in the front of the list is in the end). &l...
more »

2017-09-04 16:09 (4) Answers

When will the LRU algorithm be miss 100%?

I know these two ways to make LRU algorithm miss 100%. Cyclic accesses to a data-set that is marginally larger than the cache size. Arbitrary bursts of accesses to an infrequently accessed data-set that pollutes the cache by replaceing the mor...
more »

2017-09-03 16:09 (1) Answers

How to obtain the probability distribution of a sum of dependent discrete random variables more efficiently

I hope you are well. I was wondering if you could help me with the question provided in the attached link, please. Below the link I attach an R-code that solves the problem recursively for particular values of the parameters of the distributions invo...
more »

2017-09-03 04:09 (0) Answers

Why this is a bad bubble sort algorithm?

I started studying Data Structures and algorithms, and tried to implement Bubble sort: def BubbleSort(list): for a in range(len(list)): for b in range(len(list)):# I could start this loop from 1 if list[a]< list[b]: # to avoi...
more »

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

Would it be efficient to sort with indexes

So I was thinking of a new sorting algorithm that might be efficient but I am not too sure about that. 1) Imagine we have an array a of only positive numbers. 2) We go through the array and find the biggest number n. 3) We create a new array ...
more »

2017-09-02 19:09 (2) Answers

Get subgraph of shortest path between n nodes

I have an unweighted graph and I want to get a subgraph that has just the nodes and edges that contain the shortest paths between n known nodes. In this case 3 nodes (11, 29, & 13 are the names). Question How can I get a subgraph of shortest p...
more »

2017-09-01 15:09 (0) Answers