sorting int array with only 3 elements

后端 未结 4 922
一个人的身影
一个人的身影 2020-12-09 11:56

I have this array:

int [] myarray =  {17, 6, 8};

What is the optimal way to sort this array, in pseudocode?

Thanks!

4条回答
  •  抹茶落季
    2020-12-09 12:28

    Slightly more efficient version than the unrolled bubble sort, not optimal, but still quite simple

    if (el1 > el2) Swap(el1, el2)
    if (el2 > el3) {
       Swap(el2, el3)
       if (el1 > el2) Swap(el1, el2)
    }
    

提交回复
热议问题