dsh1956 2016-04-05 15:10
浏览 191

如何合并两个排序的二维数组而不改变它们的顺序

Say I have list of males sorted by their scores and list of females sorted by their scores as well(from highest to lowest). Now I want to have a list of:

Highest score male, Highest score female, 2nd score male, 2nd score female, 3rd score male, 3rd score female, etc...

(Note that, event if highest score female lower than 2nd score male, it is not important.)

How could I do that with php ?

Update as requested:

Array called: $arr_sort[$score][$gender]

Input array 1:

$arr_sort[20][male],$arr_sort[18][male],$arr_sort[17][male],$arr_sort[10][male],$arr_sort[9][male],$arr_sort[5][male],$arr_sort[1][male]

Input array 2:

$arr_sort[15][female],$arr_sort[14][female],$arr_sort[13][female]

expected result:

$arr_sort[20][male],$arr_sort[15][female],$arr_sort[18][male],$arr_sort[14][female],$arr_sort[17][male],$arr_sort[13][female],$arr_sort[10][male],$arr_sort[9][male],$arr_sort[5][male],$arr_sort[1][male]

Different sizes doesn't matter, just mixing to whatever length of each one, (In short, we need to get a result so that if we remove the "male" array from the result we will get the original "female" array and vice versa. )

  • 写回答

1条回答 默认 最新

  • dongzizhi9903 2016-04-05 15:58
    关注

    The input arrays are already sorted

    If you know how to iterate through one of them, you know how to iterate through the other array as well. Iterate over both (alternating).

    pseudo code:

    Iterator m = new Iterator(array1);
    Iterator f = new Iterator(array2);
    List array3 = [];
    
    while (m.hasNext() || f.hasNext() ) {
        if (male.hasNext())
            array3.add( m.next() );
        if (f.hasNext())
            array3.add( f.next() );
    }
    

    (EDIT) php example:

    $array3 = [];
    $mkeys = array_keys($array1);
    $fkeys = array_keys($array2);
    $nm = count($mkeys);
    $nf = count($fkeys);
    
    for ($i=0; ($i<$nm)||($i<$nf); ++$i) {
        if ($i<$nm) {
            //$array3[] = array($mkeys[$i], $array1[$mkeys[$i]]);
            $array3[$mkeys[$i]] = $array1[$mkeys[$i]];
        }
        if ($i<$nf) {
            //$array3[] = array($fkeys[$i], $array2[$fkeys[$i]]);
            $array3[$fkeys[$i]] = $array2[$fkeys[$i]];
        }
    }
    

    What the php code is doing is taking the key and the value and then adding it to an associative array, regardless of what that key and value are.

    Since the arrays are already sorted, all that needs to happen is to alternate adding their elements to a merge array as per the specification in the question

    Note that, event[sic] if highest score female lower than 2nd score male, it is not important

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?