Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort an array so the difference of elements a[i]-a[i+1]<=a[i+1]-a[i+2]

My mind is blown since I began, last week, trying to sort an array of N elements by condition: the difference between 2 elements being always less or equal to the next 2 elements. For example:

Α[4] = { 10, 2, 7, 4}

It is possible to rearrange that array this way:

  • {2, 7, 10, 4} because (2 - ­7 = ­-5) < (7 - ­10 = -­3) < (10 - ­4 = 6)

  • {4, 10, 7, 2} because (4 - ­10 = -­6) < (10 - ­7 = ­3) < (7 - ­2 = 5)

One solution I considered was just shuffling the array and checking each time if it agreed with the conditions, an efficient method for a small number of elements, but time consuming or even impossible for a larger number of elements.

Another was trying to move elements around the array with loops, hoping again to meet the requirements, but again this method is very time consuming and also sometimes not possible.

Trying to find an algorithm doesn't seem to have any result but there must be something.

Thank you very much in advance.

like image 449
Giorgis3 Avatar asked Apr 15 '16 14:04

Giorgis3


2 Answers

I normally don't just provide code, but this question intrigued me, so here's a brute-force solution, that might get you started.

The concept will always be slow because the individual elements in the list to be sorted are not independent of each other, so they cannot be sorted using traditional O(N log N) algorithms. However, the differences can be sorted that way, which simplifies checking for a solution, and permutations could be checked in parallel to speed up the processing.

import os,sys
import itertools

def is_diff_sorted(qa):
  diffs = [qa[i] - qa[i+1] for i in range(len(qa)-1)]
  for i in range(len(diffs)-1):
    if diffs[i] > diffs[i+1]:
      return False
  return True

a = [2,4,7,10]
#a = [1,4,6,7,20]
a.sort()
for perm in itertools.permutations(a):
  if is_diff_sorted(perm):
    print "Solution:",str(a)
    break
like image 120
Matt Jordan Avatar answered Nov 07 '22 13:11

Matt Jordan


This condition is related to differentiation. The (negative) difference between neighbouring elements has to be steady or increasing with increasing index. Multiply the condition by -1 and you get

 a[i+1]-a[i] => a[i+2]-a[i+1] 

or

0 => (a[i+2]-a[i+1])- (a[i+1]-a[i])

So the 2nd derivative has to be 0 or negative, which is the same as having the first derivative stay the same or changing downwards, like e.g. portions of the upper half of a circle. That does not means that the first derivative itself has to start out positive or negative, just that it never change upward.

The problem algorithmically is that it can't be a simple sort, since you never compare just 2 elements of the list, you'll have to compare three at a time (i,i+1,i+2).

So the only thing you know apart from random permutations is given in Klas` answer (values first rising if at all, then falling if at all), but his is not a sufficient condition since you can have a positive 2nd derivative in his two sets (rising/falling).

So is there a solution much faster than the random shuffle? I can only think of the following argument (similar to Klas' answer). For a given vector the solution is more likely if you separate the data into a 1st segment that is rising or steady (not falling) and a 2nd that is falling or steady (not rising) and neither is empty. Likely an argument could be made that the two segments should have approximately equal size. The rising segment should have the data that are closer together and the falling segment should contain data that are further apart. So one could start with the mean, and look for data that are close to it, move them to the first set,then look for more widely spaced data and move them to the 2nd set. So a histogram might help.

[4 7 10 2] --> diff [ 3 3 -8] --> 2diff [ 0 -11]

like image 40
roadrunner66 Avatar answered Nov 07 '22 12:11

roadrunner66