0

I am working through some general algorithm questions and I came upon a java solution that uses the following code to sort a collection of edges. Each edge is an x position and a height.

Collections.sort(edges, new Comparator<Edge>() {
    public int compare(Edge a, Edge b) {
        if (a.x != b.x)
            return Integer.compare(a.x, b.x);

        if (a.isStart && b.isStart) {
            return Integer.compare(b.height, a.height);
        }

        if (!a.isStart && !b.isStart) {
            return Integer.compare(a.height, b.height);
        }

        return a.isStart ? -1 : 1;
    }

I would like to do something similar in python, but I'm not sure where to start. At first I thought I could just add a key function, but I don't see how I would do that in this case. My current quick and dirty implementation is far from efficient. I represent each edge as a list, [x, height] and I represent "start edges" as a positive value and "end edges" as a negative value (that isn't too relevant here).

def sort_edges(edges):
    edges.sort(key=lambda x: x[0])
    for i in range(len(edges)-1):
        edge_one = edges[i]
        edge_two = edges[i+1]

        # Continue if edges are not on same x
        if edge_one[0] != edge_two[0]:
            continue
        # Compare height if both are start edges
        if edge_one[1] > 0 and edge_two[1] > 0:
            if edge_two[1] > edge_one[1]:
                temp = edge_one
                edges[i] = edge_two
                edges[i+1] = temp
        # Compare height if both are end edges
        if edge_one[1] < 0 and edge_two[1] < 0:
            if edge_two[1] > edge_one[1]:
                temp = edge_one
                edges[i] = edge_two
                edges[i+1] = temp
        # Start edges come first
        if edge_two[1] > 0 and edge_one[1] < 0:
            temp = edge_one
            edges[i] = edge_two
            edges[i+1] = temp


edges = [[1, 3], [2, 5], [5, -3], [4, -5]]

old = None
while old != edges:
    old = copy(edges)
    sort_edges(edges)
martineau
  • 119,623
  • 25
  • 170
  • 301
Shatnerz
  • 2,353
  • 3
  • 27
  • 43
  • I am having a hard time finding a question here. What is it that is not working here? – Tammo Heeren Oct 17 '16 at 05:04
  • How can one extend the sort function in python to include your own rules? For instance in this case, I want to sort by the first index, and then handle a few special cases when the values at the first indices are equal. As far as I know, I can't do this with a key function. This code is working, but it is horribly inefficient and I shouldn't need to rewriter a sorting algorithm. – Shatnerz Oct 17 '16 at 05:07
  • The sort function also features a `cmp` keyword. This will take a function with two attributes. Essentially two values to compare. Put your own function there. You should return either -1 (for x < y), 0 (for x==y), or 1 (for x > y). – Tammo Heeren Oct 17 '16 at 05:09
  • See [here](https://wiki.python.org/moin/HowTo/Sorting#The_Old_Way_Using_the_cmp_Parameter) for some more explanation. You may need to implement it differently depending on the python version you use. – Tammo Heeren Oct 17 '16 at 05:13
  • Is there a reason the cmp keyword became deprecated? That makes me hesitant to use. – Shatnerz Oct 17 '16 at 05:15
  • Yes. Detailed on that link I provided. There is also a way to implement it with `key`. – Tammo Heeren Oct 17 '16 at 05:16

0 Answers0