# Tag Info

5

For this precise case, (4*i+5*j+4*k+1) = 4*n+j+1. Since n is a constant you need to sort it by j. Or just use j for your outer loop (after n). Also, you can compute k directly from i and j for n in range(3,N+1): for j in range(1,n-1): for i in range(1,n-1): k = (n-i-j) if (1<=k && k<=n): ...

0

First of all, the product and category resources that can be accessed/viewed through your REST API do not have to mirror your database tables. In particular, the order of the products is not a property of a single product resource, but on the REST API it should be a property of the collection of products. This means that it should take only a single API call ...

3

To avoid updates to multiple rows, don't store the exact index in the database. Instead, store a value that is used for sorting only. When rearranging the order, give the inserted item a value halfway between the preceding and subsequent items. Using a floating point value will give you plenty of "empty slots" between items. For example, given three items ...

0

Big O time complexity of divide and conquer algorithms is not affected by what fraction you divide things into. This is because logs of different bases differ by only a constant factor. So the Big O time complexity in your case is the same as if you separate 50% out. So you should get the usual nLogn sort.

2

Are you sure you want to implement the Comparator interface, rather than defining some more suitable one of your own? For example, the randomized example you give cannot meet the transitivity requirement of Comparator, and of course can never be consistent with equals. Hence you might get some nasty surprises (including possible non-termination) if you ...

0

For your example, you could view each item in the array as an interval from the minimum possible value to the maximum possible value. Then you could randomly pick an interval, iterative reduce it to the intersection of what's left of it and any of the original intervals it still intersects, then quick sort using that interval as the pivot and considering ...

2

It's only preferable to modify the comparator if you can accept a number as your fuzzy result, and the computation only requires your left and right comparison values. public int compare(double val1, double val2) { int magic = andThenAMiracleOccurs(val1, val2); return magic; } What could be in andThenAMiracleOccurs? Almost anything. An ...

4

The key thing here is to avoid making the same comparison more than once. And there is one sort that really meets that criteria in a way that is doable for a human sorting their movie collection. Merge sort. With the merge sort, you recursively break down the size of the set to 2 (or 1). And then you sort each of those. Now, you take two sets of 2 (the ...

0

Inserting everything takes no comparisons, sorting it afterwards is in O(n*log(n)) so inserting and then sorting is in O(n * log(n)) Inserting one element into a sorted list so as to maintain a sorted list is logarithmic in the size of the list, so inserting n elements into that list is in O(n * log(n)) since the size of the list grows as you keep inserting ...

5

Insertion sort works best when you are inserting a new value into something that is already sorted. So what I would do is use Quicksort to sort the original dataset that you have, then when additional log entries come in, add them one by one into the already sorted set. With Quicksort being O(n*logn) and Insertion Sort being O(n) when used with an already ...

2

It looks like the ASCII values of the characters are used to sort relative to the actual numbers. After converting the characters to their decimal ASCII values, the sorted list looks something like this: [1, 2, 4, '-a', '3', '5', 'b', 'c'] [1, 2, 4, 45 97, 51, 53, 98, 99] Note that '-a' is sorted by the first character first, as is normal for an ...

5

This is a well-defined problem with a deterministic solution. You can think of each list as forming part of a directed acyclic graph: Then, constructing a merged order is simply a matter of using one of the well-known algorithms to find a topological sorting. The more similarities you have between the lists, the fewer valid topological sorts you will ...

1

If you have literally no comparison function, then the only sane option is to pick the same order each time: one = [1,2,3] two = [4,5,6] three = [7,8,9] merge(one, two, three) == [1,4,7,2,5,8,3,6,9] If there is some order, and the items can be compared for equality, but you can't encode the ordering explicitly, you can track how many times you've seen ...

Top 50 recent answers are included