function compare(f, d) {
var e = {
relocation: [],
insert: [],
length: d.length
}, c = d + ", " + f ,
b = f.slice();
if (c = c.match(/([^,]\S*[^,])(?=,[\s\S]*\s[\s\S]*\1)/g)) {
c.reverse();
for (var a = 0; a < c.length; a++) {
var g = ("" + b).slice(0, ("" + b).lastIndexOf(c[a]) + 1).split(",").length - 1,
h = c[a].split(",").length,
k = b.splice(g, h);
e.relocation.push([g, h]);
b = k.concat(b)
}
}
for (a = 0; a < d.length; a++) d[a] != b[a] && (b.splice(a, 0, d[a]), e.insert.push([a, d[a]]));
return e
};
function conversion(b, c) {
for (var a = 0; a < c.relocation.length; a++) {
var d = c.relocation[a];
b = b.splice(d[0], d[1]).concat(b)
}
for (a = 0; a < c.insert.length; a++) d = c.insert[a], b.splice(d[0], 0, d[1]);
b.length = c.length;
return b
};
var oldArray = [1, 2, 3, 4, 5];
var newArray = [11, 3, 4, 3, 11, 21];
var changes = compare(oldArray, newArray);
var len = changes.relocation.length + changes.insert.length + 1;//кол-во затраченных операций в conversion
alert(newArray +"\n"+conversion(oldArray, changes)+"\n"+len)
var oldArray = [4, 50, 6, 7, 8, 9,1, 2, 3, 50 ];
var newArray = [1, 2, 4, 50, 6, 7, 8, 9,50];
var changes = compare(oldArray, newArray);
var len = changes.relocation.length + changes.insert.length + 1;
alert(newArray +"\n"+conversion(oldArray, changes)+"\n"+len)
var oldArray = [1, 2, 3, 4, 5];
var newArray = [5, 4, 3, 2, 1];
var changes = compare(oldArray, newArray);
var len = changes.relocation.length + changes.insert.length + 1;
alert(newArray +"\n"+conversion(oldArray, changes)+"\n"+len);
var oldArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var newArray = [1, 2, 3, 5, 34, 32, 4, 45];
var changes = compare(oldArray, newArray);
var len = changes.relocation.length + changes.insert.length + 1;
alert(newArray +"\n"+conversion(oldArray, changes)+"\n"+len);
var oldArray = [1, 2, 3, 4, 5];
var newArray = [1, 2, 3, 5, 4, 45];
var changes = compare(oldArray, newArray);
var len = changes.relocation.length + changes.insert.length + 1;
alert(newArray +"\n"+conversion(oldArray, changes)+"\n"+len);