Optimal bubble sorting algorithm for an array of arrays of numbers

前端 未结 3 1744
不知归路
不知归路 2021-02-01 03:59

Fix positive integers n and k.

Let A be an array of length n with A[i] an array of length k

3条回答
  •  时光说笑
    2021-02-01 04:43

    Here is an intuitive algorithm I thought of. It gives a constructive proof of the optimal solution I think.

    Here is the algorithm :

    I tried it for n= 4 5 6 7 9 and it gave the same results as the one from badawi:

    The idea is the following:

    1: chose one extreme value that is not at his final place ( 1 or n to start)

    2: find the extreme value which is the closest to his final position ( marked with an arrow in my example below)

    3: If it's among the largest elment,

    then move it to the other side and shifht all smallest element of the pair to the left

    otherwise

    move it to the otherside and shift all the largest element of each pair to the right .

    Note: shifting is equivqlent to "bubbling" this value with the smalles (resp largest) element of each pair.

    4: go back to step 2, but if you chose one of the large take one of the small and vice versa.

    It's pretty intuitive and it seems to work:

    Example n=5:

    11 22 33 44 55 
    ^
    |
    12 23 34 45 51 (4 moves) // shifted all larger numbers to the left
              ^
              |
    52 13 24 43 51 (3 moves) // shifted all smaller numbers to the right
       ^
       |
    52 34 24 35 11 (3 moves) // shifted all larger numbers to the left
              ^
              |
    55 24 34 32 11 (3 moves) // smaller to the right
       ^
       |
    55 44  33 22 11 (2 moves) // larger to left
    

    Total 15 moves.

    second example n=7:

    11 22 33 44 55 66 77 // 6 moves
     ^
    12 23 34 45 56 67 71 //5 moves
                    ^
    72 13 24 35 46 56 71 //5 moves
       ^
    72 34 25 36 46 57 11 // 4 moves
                    ^
    77 24 35 26 36 45 11 //4 moves
       ^
    77 45 36 26 35 42 11 //1 move
           ^       
    77 65 34 26 35 42 11 //2 moves
             ^
    77 65 34 56 34 22 11 //2 moves
              ^
    77 66 54 53 34 22 11 //1 move
              ^
    77 66 54 45 33 22 11 //1 move
              ^
    77 66 55 44 33 22 11
    

    total: 31

    Don't hesitate to ask me questions if i'm not clear.

    It's pretty easy to do it by hand. You can try it yourself with 6 or 7 or write an algorithm.

    I tried it with 6 it gave 23. , with 7 it gave 31 and with 9 it gave 53 , it takes one minute to calculate it by hand without computing anything

    Why this solution is optimal :

    Each time you move one large element to the opposite side, you move all the smallest one of the pair to the left.

    So moving all the large element will not make you lose any move for the moving all smallest one.

    You always move you element in "the right direction"

    Moreover you for moving the extreme elements you make the minimum number of moves. (this is because the algorithm takes the extreme value closest to his last position that no move is lost)

    The reasonning is the same for small element.

    This algorithm gives you optimal moves since it doesn't make any unnecessary move.

    Hope I didn't make any mistake .

    It proves that badawi results were optimal as you expected.

提交回复
热议问题