<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Ytl's Java Blog

    厚積而薄發---每一天都是一個全新的開始
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Algorithm to merge sorted arrays

    Posted on 2011-05-06 16:55 ytl 閱讀(420) 評論(0)  編輯  收藏 所屬分類: Algorithms and programming concepts

    Algorithm to merge sorted arrays

    In the article we present an algorithm for merging two sorted arrays. One can learn how to operate with several arrays and master read/write indices. Also, the algorithm has certain applications in practice, for instance in merge sort.

    Merge algorithm

    Assume, that both arrays are sorted in ascending order and we want resulting array to maintain the same order. Algorithm to merge two arrays A[0..m-1] and B[0..n-1] into an array C[0..m+n-1] is as following:

    1. Introduce read-indices ij to traverse arrays A and B, accordingly. Introduce write-index k to store position of the first free cell in the resulting array. By default i = j = k = 0.
    2. At each step: if both indices are in range (i < m and j < n), choose minimum of (A[i], B[j]) and write it toC[k]. Otherwise go to step 4.
    3. Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.
    4. Copy the rest values from the array, which index is still in range, to the resulting array.

    Enhancements

    Algorithm could be enhanced in many ways. For instance, it is reasonable to check, if A[m - 1] < B[0] orB[n - 1] < A[0]. In any of those cases, there is no need to do more comparisons. Algorithm could just copy source arrays in the resulting one in the right order. More complicated enhancements may include searching for interleaving parts and run merge algorithm for them only. It could save up much time, when sizes of merged arrays differ in scores of times.

    Complexity analysis

    Merge algorithm's time complexity is O(n + m). Additionally, it requires O(n + m) additional space to store resulting array.

    Code snippets

    Java implementation

    // size of C array must be equal or greater than

    // sum of A and B arrays' sizes

    public void merge(int[] A, int[] B, int[] C) {

          int i,j,k ;

          i = 0;

          j=0;

          k=0;

          m = A.length;

          n = B.length;

          while(i < m && j < n){

              if(A[i]<= B[j]){

                  C[k] = A[i];

                  i++;

              }else{

                  C[k] = B[j];

                  j++;

           }

           k++;

           while(i<m){

             C[k] = A[i]

             i++;

             k++;

          }

          while(j<n){

             C[k] = B[j] 

             j++;

              k++;

     }


    Python  implementation

    def merege(left,right):

        result = []

        i,j = 0

       while i< len(left) and j < len(right):

            if left[i]<= right[j]:

                result.append(left[i])

                i = i + 1

            else:

                result.append(right[j])

                j = j + 1

        while i< len(left):

               result.append(left[i])

               i = i + 1

        while j< len(right):

               result.append(right[j])

               j = j + 1

        return result

      
    MergSort:

    import operator

    def mergeSort(L, compare = operator.lt):
         if len(L) < 2:
              return L[:]
         else:
              middle = int(len(L)/2)
              left = mergeSort(L[:middle], compare)
              right= mergeSort(L[middle:], compare)
              return merge(left, right, compare)

    def merge(left, right, compare):
         result = []
         i, j = 0, 0

         while i < len(left) and j < len(right):
              if compare(left[i], right[j]):
                   result.append(left[i])
                   i += 1
              else:
                    result.append(right[j])
                    j += 1
         while i < len(left):
              result.append(left[i])
              i += 1
         while j < len(right):
              result.append(right[j])
              j += 1
         return result
                   

    主站蜘蛛池模板: www.亚洲精品.com| 57pao一国产成视频永久免费| 成年18网站免费视频网站| 亚洲最大免费视频网| 99久久综合精品免费| 亚洲ts人妖网站| 免费黄色一级毛片| 国产亚洲精品欧洲在线观看| 免费国产成人高清在线观看麻豆| 亚洲av无码一区二区三区四区| 日韩免费三级电影| 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 热99RE久久精品这里都是精品免费 | 亚洲a∨无码精品色午夜| 国产成人免费全部网站| 一级中文字幕免费乱码专区| 亚洲欧洲日产国码无码久久99| 成人免费区一区二区三区| 亚洲综合无码一区二区| 特级做A爰片毛片免费69| 亚洲高清乱码午夜电影网| 亚洲精品国产V片在线观看| 手机看片国产免费永久| 亚洲精品在线电影| 黄网址在线永久免费观看| 乱爱性全过程免费视频| 亚洲网址在线观看你懂的| 黄在线观看www免费看| 日韩在线视精品在亚洲| 国产亚洲免费的视频看| 中文字幕av无码无卡免费| 亚洲a∨无码一区二区| 国产成人精品日本亚洲| 中文毛片无遮挡高潮免费| 美国毛片亚洲社区在线观看| 亚洲中文字幕在线乱码| 91免费国产自产地址入| 精品在线免费视频| 久久亚洲精品中文字幕| 免费看男女下面日出水视频| 中文字幕免费观看视频|