zoukankan      html  css  js  c++  java
  • 合并有序数组

    #include <stdio.h>
    typedef int object_t;
    size_t merge_sort(object_t* first, object_t* last,
        object_t* begin, object_t* end,
        object_t* result_first) {
      size_t len = last - first + (end - begin);
      object_t* result = result_first + len;
      while ((first != last) && (begin != end)) {
        if (*(last - 1) < *(end -1 )) {
          *(--result) = *(--end);
        } else {
          *(--result) = *(--last);
        }
      }
      while (first != last) {
        *(--result) = *(--last);
      }
      while (begin != end) {
        *(--result) = *(--end);
      }
      return len;
    }
    void array_print(object_t* first, object_t* last) {
      size_t len = last - first;
      if (len <= 0) {
        return;
      }
      while (first != last) {
        printf("%d	", *first++);
      }
      printf("
    ");
    }
    int main(int argc, char *argv[]) {
      object_t memory_array[20]={
        1,5, 6, 16, 17
      };
      object_t station[] ={
        1, 3, 5, 6, 7
      };
      object_t* last1 = memory_array + 5;
      object_t* last2 = station + sizeof(station)/sizeof(object_t);
      array_print(memory_array, last1);
      array_print(station, last2);
      size_t len = merge_sort(memory_array, last1 , station, last2, memory_array);
      array_print(memory_array , memory_array + len);
      return 0;
    }    
    View Code
  • 相关阅读:
    Python之二维数组(list与numpy.array)
    too many values to unpack
    python 寻找可迭代(list)的目标元素的下表方法
    zip函数
    map函数
    Sokcet代码错误类型
    PL-VIO Docker测试
    如何检索国外博士论文
    EuRoc V203数据集的坑
    Tracking of Features and Edges
  • 原文地址:https://www.cnblogs.com/westfly/p/3291301.html
Copyright © 2011-2022 走看看