Yasin - 10 months ago 47

Python Question

What's the best way of sorting a python list in an unordered fashion according to the median of value positions in the list?

Suppose:

`a = [1, 3 ,6, 7, 10, 12, 17]`

I'm looking for this:

`a = [1, 17, 7, 3, 12, 6, 10]`

Meaning, the list now looks like

`[start, end, mid, first_half_mid, second_half_mid, ...]`

edit: To clarify further, I'm looking for a way to keep bisecting the list until it covers the whole range!

Answer

**UPDATE:** consider left element as a middle if the length of the list is an even numer

```
def f(a):
# take left middle element for even-length lists
mid = len(a)//2 if len(a)%2 else len(a)//2-1
# take len(a)//2 as a middle element
#mid = len(a)//2
if len(a) <= 2:
return a
elif(len(a) == 3):
return a[[0,-1,mid]]
else:
return np.append(a[[0,-1,mid]], f(np.delete(a, [0,len(a)-1,mid])))
```

Output:

```
In [153]: f(a)
Out[153]: array([ 1, 17, 7, 3, 12, 6, 10])
```

**OLD answer:**

here is one of many possible solutions:

```
import numpy as np
def f(a):
if len(a) <= 2:
return a
elif(len(a) == 3):
return a[[0,-1,len(a)//2]]
else:
return np.append(a[[0,-1,len(a)//2]], f(np.delete(a, [0,len(a)-1,len(a)//2])))
a = np.array([1, 3 ,6, 7, 10, 12, 17])
In [114]: a
Out[114]: array([ 1, 3, 6, 7, 10, 12, 17])
In [115]: f(a)
Out[115]: array([ 1, 17, 7, 3, 12, 10, 6])
```

PS about last two numbers in the result list/array - the question is what is the middle index for the 4-elements list?

What is the middle element for `[3,6,10,12]`

list? In my solution it would be `10`

(index: `4//2 == 2`

)