如何将数组中的排序数组合并到第三个排序数组

时间:2016-10-11 23:12:58

标签: javascript arrays sorting merge

标题可能令人困惑并原谅我,因为我还是初学者。这是学校硬件分配的一部分,虽然我不是直接寻找答案,但我需要向正确的方向推进,或者任何帮助。所以问题就是......

我需要根据类中的代码创建一个简单的程序,将多个已排序的数组合并为一个排序的数组,以输出到屏幕。这些数组在"测试用例中被硬编码"取消注释并验证。指导方向如下

  

使用在类中开发的合并程序(参见附件)作为基础,创建一个将0(零)合并到n个数组的版本。   提示:   在合并之前对数组进行排序   使用shift方法删除数组中的第一项(arrayName.shift())   创建一个数组数组---> var x = []; var y = []; var z = [x,y];

编辑:我已经通过电子邮件向教师发送了一个基​​于@cpugourou给出的答案的解决方案,他的答复是他不会接受这个解决方案。作业的重点是手动合并这些数组。

以下是最初的"合并计划"我们在课堂上开发的

"use strict";   // reduces chance for error

/* 
    there are 2 arrays here and this program will merge them.
    Your job is to merge zero or more arrays. Test cases include:
    1. arrays with no elements: x = [], y = [], z = [], ...
    2. 2 arrays - one with elements and one without: x = [], y = [200, 39,1]
    3. 4 arrays: x = [5, 1, 0], y = [], z = [78, 3], w = [4, 34]
*/
var x = [ 12, 5, 1], y = [ 13, 2, 3], z = [];   // x and y are input and, after processing, z has the merged arrays
var ix = 0, iy = 0, iz = 0;         // indexes to the next element

// The following sorts the arrays in ascending sequence
x.sort(function(a, b){return a - b});
y.sort(function(a, b){return a - b});

while (ix < x.length || iy < y.length){     // while arrays x or y have elements

if (ix < x.length && iy < y.length){    // if both have elements, choose the lowest element

    if (x[ix] <= y[iy]){                // is the current element in array x less than the current element in y?
        z[iz] = x[ix];                  // choose x
        iz++;                           // point to the next space in z
        ix++;                           // ditto x
    } else{                             // choose y
        z[iz] = y[iy];                  
        iz++;                           // next
        iy++;                           // next
    }

} else if (ix < x.length){              // if only one has elements is it x?
    for (ix; ix < x.length; ix++){      // if so, move the rest of x to z
        z[iz] = x[ix];                  // copy current element in x
        iz++;                           // next z ... no need to increment x because the for loop does it
    }

} else if (iy < y.length){              // if only y has elements
    for (iy; iy < y.length; iy++){      // move the rest of y to z
        z[iz] = y[iy];                  // copy
        iz++;                           // increment z's pointer
    }
}
}

// display the resulting merged elements in the web page
var result = "<ul>";
for (var i in z){
    result += "<li>" + z[i] + "</li>"
}
result += "</ul>"
document.getElementById("placeAnswerHere").innerHTML=result;

/*
Things to think about:
. This code is hard wired to merge only 2 arrays
. You will need to create an array containing all arrays to be merged. e.g. q = [x, y, z, w, and more]
. Your loop will need to find the smallest element in any of the arrays in q and move it to z (the resultant array)
. There is a method shift() to strip the first element from an array (e.g. x.shift(); removes the first element in array x)
. This is like making a pile of cafeteria trays from a variable numbers of tray stacks.
. Remember to sort all arrays first
*/

所以我的新问题变成了,如何在不写一堆意大利面条代码的情况下实现这一目标?

2 个答案:

答案 0 :(得分:1)

我能想到的最快和最短的(增加了非重复加值):

var x = [5, 1, 0], y = [2, 5, 0], z = [78, 3, 1], w = [4, 34];

function sortNumber(a,b) {
    return a - b;
}
function uniq(a) {
   return Array.from(new Set(a));
}

var d = uniq(x.concat(y,z,w).sort(sortNumber));
/* [0, 1, 2, 3, 4, 5, 34, 78] */

答案 1 :(得分:0)

尽管cpugourou的答案有效,但它似乎与陈述的HW分配不匹配。至于HW分配,我不清楚的一个问题是如何创建一个通用的数组数组(比如for(i = 0; i&lt; n; i ++)aa [i] = new Array(...)),这似乎与HW问题声明冲突,即数组是硬编码测试用例。

忽略如何创建硬编码数组的通用数组的问题,一旦你有一个排序数组的数组,HW赋值似乎要求你使用数组数组实现n路合并。这意味着查找数组数组中的哪个数组具有最小元素并将该元素移动到输出数组(附加到输出数组,从输入数组中删除)。当n个阵列中的一个被清空时,该阵列将从阵列数组中删除,并继续进行n-1路合并。重复此操作,直到只剩下1个数组,其中最后一个数组的其余部分只是复制(或移动)到输出数组。

n路合并的常见优化是使用堆,但我怀疑你是否应该使用堆进行此HW分配。