Skip to main content
deleted 336 characters in body
Source Link

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

After the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item.

You could add a boolean flag for each inner loop to check

Also, yourYour algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}, so links might be split up. You could check whether the pair at b comes before the pair at a, but with circular links being legal, you need to check for the case where the whole list is one long circular chain, which could result in infinite loops.

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

After the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item.

You could add a boolean flag for each inner loop to check

Also, your algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}, so links might be split up. You could check whether the pair at b comes before the pair at a, but with circular links being legal, you need to check for the case where the whole list is one long circular chain, which could result in infinite loops.

Your algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}, so links might be split up. You could check whether the pair at b comes before the pair at a, but with circular links being legal, you need to check for the case where the whole list is one long circular chain, which could result in infinite loops.

added 301 characters in body
Source Link

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

Two problems: AfterAfter the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item. Also

You could add a boolean flag for each inner loop to check

Also, your algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}, so links might be split up. You could check whether the pair at b comes before the pair at a, but with circular links being legal, you need to check for the case where the whole list is one long circular chain, which could result in infinite loops.

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

Two problems: After the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item. Also, your algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}.

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

After the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item.

You could add a boolean flag for each inner loop to check

Also, your algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}, so links might be split up. You could check whether the pair at b comes before the pair at a, but with circular links being legal, you need to check for the case where the whole list is one long circular chain, which could result in infinite loops.

deleted 305 characters in body
Source Link

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

AfterTwo problems: After the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item. Also, your algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

One potential fix would be:The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}.

int a = 0;
int b = 0;
int temp;
bool foundSwap;

while (a < list.Count) {
    b = a + 2;
    foundSwap = false;
    while (b < list.Count) {
        if (list[b] == list[a + 1]) {
            temp = list[b];
            list.RemoveAt(b);
            list.Insert(a + 2, temp);

            temp = list[b + 1];
            list.RemoveAt(b + 1);
            list.Insert(a + 3, temp);

            a += 2;
            b = a;
            foundSwap = true;
        }
        b += 2;
    }
    if (!foundSwap)
        a += 2;
}

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

After the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item

One potential fix would be:

int a = 0;
int b = 0;
int temp;
bool foundSwap;

while (a < list.Count) {
    b = a + 2;
    foundSwap = false;
    while (b < list.Count) {
        if (list[b] == list[a + 1]) {
            temp = list[b];
            list.RemoveAt(b);
            list.Insert(a + 2, temp);

            temp = list[b + 1];
            list.RemoveAt(b + 1);
            list.Insert(a + 3, temp);

            a += 2;
            b = a;
            foundSwap = true;
        }
        b += 2;
    }
    if (!foundSwap)
        a += 2;
}

There are a few assumptions to my answer based on the example you gave:

  • Each pair has no more than one match on each side (ie, [0,1 1,2 1,3] is not allowed)
  • There is no circular link (ie, [0,1 1,2 2,0] is not allowed)

Your algorithm currently does not guarantee that the first pair and the last pair are in the correct location

Given that circular links are allowed, there is no "correct" first and last pair.

Two problems: After the last successful swap of a chain when there is no next match, a is incremented twice (after the swap and at the end of the outer loop) causing it to skip the next item. Also, your algorithm only appends links to a chain to the first encountered pair in the chain, but it does not ensure a is the first item in the chain.

The result is that an example like { 1,2, 0,1, 2,3} will sort to {1,2, 2,3, 0,1} instead of {0,1, 1,2, 2,3}.

added 98 characters in body
Source Link
Loading
Source Link
Loading