Sort array with difference
Hello everybody,
I have a question to ask, what is the efficient way to sort array according to difference of elements?
Let's say I have array/vector
a = [0 2 3 6 8]; //which is sorted and start from 0.
But I want to sort by neighbor difference
abs(0-2) = 2 //difference between first and second elements in vector
abs(2-3) = 1 //difference between second and third elements in vector
abs(3-6) = 3
abs(6-8) = 2
Now sorted output:
abs(2-3) = 1
abs(0-2) = 2
abs(6-8) = 2
abs(3-6) = 3
make new vector from sorted:
a = [0 1 3 5 8] now their difference is accordance of sorted early vector.
If there is anything is not clear, please let me know I will do my best to explain more in details.
Any idea appreciated!
Thanks!