-2

I have an initial array of Tiles ordered by [row,col] like this

[0,1] [0,2] [0,3]

[1,1] [1,2] [1,3]

i need to binary insert elements ordered by row and colum like this

  • binaryInsert([0,0])

  • binaryInsert([1,0])

and the expected result will be an array ordered like this

[0,0] [0,1] [0,2] [0,3]

[1,0] [1,1] [1,2] [1,3]

basically i need to be able to add rows and columns in every direction (top,right,left,down) but always ordered like the example below.

I can transform rows and cols to one value with this function

function coordToOrder(row,col,numCols){
 return col+initCols*row
}

var initCols = 5;

var order = coordToOrder(1,4,initCols);

alert(order)

but then if i need to insert a tile at order 10 and is already inserted i need to increment the following.

I'm tryng to use a binary solution instead the sort function because is faster.

Thanks for your help!

  • Try hiring a programmer – KooiInc Dec 19 '14 at 14:31
  • Somehow, that sounds like a homework. What have you tried so far? Show some code, point us to where you're blocked, and we'll be willing to help you. We can't just write your code for you. It's a POOR learning strategy anyway – Patrice Dec 19 '14 at 14:34
  • I mean like this http://machinesaredigging.com/2014/04/27/binary-insert-how-to-keep-an-array-sorted-as-you-insert-data-in-it/ but using two factor and yes i'm a beginner – Angelo Bosio Dec 19 '14 at 14:36
  • @mvw i have one array of objects that store Tiles values like rows and columns coord – Angelo Bosio Dec 19 '14 at 14:38
  • @AngeloBosio Your update quite changed the nature of the question to a performance issue. You need to provide more information on the size of the data and the needed operations to get a good performance advice. – mvw Dec 19 '14 at 16:09
  • @mvw you are right my question was initially not precise sorry for that i'll give you more information soon thanks again for your help. – Angelo Bosio Dec 19 '14 at 16:15

1 Answers1

1

Hint: Your implementation of any sorting procedure, including binary insert, will need to provide an order relation "<" on your tiles. As a first step use this comparison function, which you can use to sort your test data via the standard JavaScript sort.

Then try to implement your binary insert.

var a = [ [0,1], [0,2], [0,3], [1,1], [1,2], [1,3] ];

var cmp = function(a, b) { 
  if (a[0] == b[0]) {
    return a[1] - b[1]; 
  } 
  return a[0] - b[0]; 
};

Then for example:

a.push([0,0]);

gives

[[0, 1], [0, 2], [0, 3], [1, 1], [1, 2], [1, 3], [0, 0]]

then

a.sort(cmp);

gives

[[0, 0], [0, 1], [0, 2], [0, 3], [1, 1], [1, 2], [1, 3]]

further

a.push([1,0]);
a.sort(cmp);

gives

[[0, 0], [0, 1], [0, 2], [0, 3], [1, 0], [1, 1], [1, 2], [1, 3]]
mvw
  • 5,075
  • 1
  • 28
  • 34
  • Your function can be reduced to `var cmp = function { var f = 0 + ( a[0] == b[0] ); return a[f] - b[f]; };` but this isn't as fast as yours. Your answer is better for general purposes, my reduced version is better for minimification of code. – Ismael Miguel Dec 19 '14 at 15:18
  • Thanks for this answer but i've already tried with sort function and is to slow for my purpose. I think that will be a faster solution the binary insertion but i can't figure out how to 2 with 2 factor (rows,cols) – Angelo Bosio Dec 19 '14 at 15:44
  • @AngeloBosio a) I suggested you should use `sort()` only for testing. You will need `cmp()` for all diamonds in the the flowchart where a comparision is performed. b) I would not expect the result to be much faster, `sort()` is very likely to use some O(log n) sort method. – mvw Dec 19 '14 at 15:53
  • @mvw every time i add i tile i need to sort the entire array with sort function O(Nlog(N)), that cost much more than binary insertion but is just my personal opinion if someone can explain me more i'll appreciate – Angelo Bosio Dec 19 '14 at 16:02
  • @AngeloBosio No, you could do it batch-wise. In my example one would just push the two new entries and then do just one sort at the end. Your insertion will also force you to extend the array somewhere to insert the new elements. I am not sure if that extension / copying is much faster than that single sort. – mvw Dec 19 '14 at 16:05