c# 4.0 - I'm looking for a particular insertion algorithm but I can't think of the name -


basically have method take set of numbers selected value , reorder them ascending keeping track of new selected value.

so {21, 2, 9, 13} selectedval = 9 becomes { 1, 2, 3, 4 } selectedval = 2

what need able swap 2 numbers , keep order intact. if want swap 2 4 need 3->2 , 4->3 other way around swapping 3 1 1->2 , 2->3.

i'm sure there proven algorithm , create own fun tomorrow i'd fastest solution use in actual application.

the name of algorithm or useful links appreciated. using c# 4.0 if matters @ all.

edit - came own solution believe works on paper i'd still reply if knows better solution.

assuming ordered list starts @ 1 in case...

if (newpos < currpos)    (int = newpos-1; < currpos-1; i++)    {        array[i] += 1;        array[currpos-1] = newpos;    }      else if (newpos > currpos)    (int = newpos-1; >= currpos-1; i--)    {        array[i] -= 1;        array[currpos-1] = newpos;    }   else // nothing since trying swap same place 


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -