`
tan-chao
  • 浏览: 2462 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Write a code to merge N sorted array

 
阅读更多

N     ERRORN = 1:    A1N = 2:    merge A1 & A2;
{
N > 2:
    merge A1 & A2 -> A12;
    merge A12 & A3 -> A13;
    ...
    merge A1(N-1) & AN.
}

recursive way is simple for thinking. but this will loop for N-1 times, and the remove operations will cost a lot of waste.

another thinking about {} part:
while (N>1):
    getMaxTail(A1...AN)
    A[index--] = Max
    if AX.len == 0:
        N - 1

1. while loop break condition
2. tail condtion (plus one sorted array?)
3. N is large number



http://www.shahuwang.com/?p=327



[code="python"]'''
MergeSortedArrays.py
Write a code to merge N sorted array
'''

def mergeTwoSortedArrays(a, b):
print 'start mergeTwoSortedArrays'

aIndex = a.__len__() - 1
bIndex = b.__len__() - 1
index = aIndex + bIndex + 2
r = [0]*index
index-=1

while aIndex >= 0 and bIndex >=0:
if a[aIndex] > b[bIndex]:
r[index] = a[aIndex]
index-=1
aIndex-=1
else:
r[index] = b[bIndex]
index-=1
bIndex-=1

while aIndex >= 0:
r[index] = a[aIndex]
index -= 1
aIndex -= 1

while bIndex >= 0:
r[index] = b[bIndex]
index -= 1
bIndex -= 1

print r

if __name__ == '__main__':
print 'hello MergeSortedArrays'
a = [1,2,3]
print a
b = [1, 4, 6]
print b
mergeTwoSortedArrays(a, b)

 

分享到:
评论

相关推荐

    Merge Sorted Array合并排序数组leetcode

    Merge Sorted Array 合并 排序 数组 leetcode

    LeetCode Merge 2 Sorted Lists解决方案

    LeetCode Merge 2 Sorted Lists解决方案

    mergeksorted.cs

    Merge k Sorted Lists 给定k个有序数组并将其合并成1个有序数组, 代码采用C#编写

    php数组合并array_merge()函数使用注意事项

    1.array_merge()合并 例子 $array = array('a'=>'bb'); $array2 = array('b'=>'cc'); $array3 = array_merge($array,$array2); 输出结果为 Array ( [a] => bb [b] => cc ) 上面因为都是数组就没有问题了,假如我们...

    C#例子代码 A0710_Merge

    C#例子代码 A0710_MergeC#例子代码 A0710_MergeC#例子代码 A0710_MergeC#例子代码 A0710_MergeC#例子代码 A0710_MergeC#例子代码 A0710_MergeC#例子代码 A0710_MergeC#例子代码 A0710_MergeC#例子代码 A0710_MergeC#...

    C#例子代码 A0711_Merge

    C#例子代码 A0711_MergeC#例子代码 A0711_MergeC#例子代码 A0711_MergeC#例子代码 A0711_MergeC#例子代码 A0711_MergeC#例子代码 A0711_MergeC#例子代码 A0711_MergeC#例子代码 A0711_MergeC#例子代码 A0711_MergeC#...

    文本比对工具-配合CodeMirror使用

    文本比对js-配合 CodeMirror.MergeView方法使用

    GridctrlV2.24 Demo And SourceCode(With Merge and Unmerge)

    GridctrlV2.24 SourceCode, Merge Unmerge 含合并与拆分功能,当前GridCtrlV2.27中不含该功能,可自行移植

    重新认识php array_merge函数

    规范:array array_merge(array $array1 [, array $…]) 说明: 1. 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。 2. 如果输入的数组中有相同的字符串键名,则该键名后面的值将覆盖前一...

    解析php中array_merge与array+array的区别

    本篇文章是对php中array_merge与array+array的区别进行了详细的分析介绍,需要的朋友参考下

    PHP中array_merge和array相加的区别分析

    //如果键名为字符,且键名相同,array_merge()后面数组元素值会覆盖前面数组元素值print_r(array_merge($arr1,$arr2)); //Array ( [a] => JAVA )//如果键名为字符,且键名相同,数组相加会将最先出现的值作为结果...

    merge-sort code

    merge-sort code

    Araxis Merge Professional 2012.4162 (x86/x64).part1

    Use it to compare and merge source code, web pages, XML and other text files with native application performance. Directly open and compare the text from Microsoft Office (Word and Excel), ...

    cpp-算法精粹

    Convert Sorted Array to Binary Search Tree Convert Sorted List to Binary Search Tree LCA of BST Kth Smallest Element in a BST 二叉树的递归 Minimum Depth of Binary Tree Maximum Depth of Binary Tree ...

    Coding Interview In Java

    20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations II 571 236 Permutation Sequence 573 237 Generate ...

    PHP合并数组函数array_merge用法分析

    array array_merge(array array1,array array2[,array...]); 注: 在合并时,如果输入的数组中有相同的字符串键名,则后面的值将覆盖前面的值;如果数组包含数字键名,后面的值不会覆盖原来的值,而是附加到后面 ...

    Araxis Merge v6.5/Araxis Merge 2017比对工具

    Araxis Merge是一款比对软件工具,不仅支持单个文件的比对,而且支持对两个文件夹的所有文件进行对比,并实时修改。用了蛮久感觉很好用,所以分享给大家。 里面给大家内嵌了Araxis Merge v6.5和Araxis Merge 2017两...

    Araxis Merge Professional 2012.4162 (x86/x64).part2

    Use it to compare and merge source code, web pages, XML and other text files with native application performance. Directly open and compare the text from Microsoft Office (Word and Excel), ...

    php通过array_merge()函数合并关联和非关联数组的方法

    本文实例讲述了php通过array_merge()函数合并关联和非...$result = array_merge((array)$beginning, (array)$end); print_r($result); ?> 输出结果如下: Array ( [0] => foo [1] => bar ) 希望本文所述对大家的ph

Global site tag (gtag.js) - Google Analytics