 +//}
 +    }
 +
-+    qsort(arr, arr_idx, sizeof(arr[0]), qnameid_cmpfunc);
-+
-+    int j;
-+    for(j=0; j<arr_idx; j++) {
-+      if (j+1 < arr_idx &&
-+          bamptr_get_qnameid(arr[j]) == bamptr_get_qnameid(arr[j+1])) {
-+        bamptr_set_mate(arr[j], arr[j+1]);
-+        bamptr_set_mate(arr[j+1], arr[j]);
-+        j++;
-+      } else {
-+        short const flag = bamptr_get_flag(arr[j]);
-+        if ((flag & BAM_FPAIRED) && !(flag & BAM_FMUNMAP)) {
-+          char qname[128];
-+          bamptr_get_qname(arr[j], qname, sizeof(qname));
-+          fprintf(stderr, "(W) %s is paired (shown in FLAG) but has no pair read in inputs (j %d arr_idx %ld flag %d)\n", qname, j, arr_idx, flag);
++    const int use_qsort = 32; // TODO should be tuned
++    if (arr_idx >= use_qsort) {
++      qsort(arr, arr_idx, sizeof(arr[0]), qnameid_cmpfunc);
++      int j;
++      for(j=0; j<arr_idx; j++) {
++        if (j+1 < arr_idx &&
++            bamptr_get_qnameid(arr[j]) == bamptr_get_qnameid(arr[j+1])) {
++          bamptr_set_mate(arr[j], arr[j+1]);
++          bamptr_set_mate(arr[j+1], arr[j]);
++          j++;
++        } else {
++          short const flag = bamptr_get_flag(arr[j]);
++          if ((flag & BAM_FPAIRED) && !(flag & BAM_FMUNMAP)) {
++            char qname[128];
++            bamptr_get_qname(arr[j], qname, sizeof(qname));
++            fprintf(stderr, "(W) %s (qnameid %ld) is paired (shown in FLAG) but has no pair read in inputs (j %d arr_idx %ld flag %d)\n", qname, bamptr_get_qnameid(arr[j]), j, arr_idx, flag);
++          }
 +        }
-+      }
-+    } // for j
++      } // for j
++    } else { // qsort
++      int j;
++      for(j=0; j<arr_idx; j++) {
++        if(NULL == bamptr_get_mate(arr[j])) {
++          int k;
++          const uint64_t qnameid_j = bamptr_get_qnameid(arr[j]);
++          for (k=j+1; k<arr_idx; k++) {
++            if(qnameid_j == bamptr_get_qnameid(arr[k])) {
++              bamptr_set_mate(arr[j], arr[k]);
++              bamptr_set_mate(arr[k], arr[j]);
++              break;
++            }
++          } // for k
++          if(NULL == bamptr_get_mate(arr[j])) {
++            short const flag = bamptr_get_flag(arr[j]);
++            if ((flag & BAM_FPAIRED) && !(flag & BAM_FMUNMAP)) {
++              char qname[128];
++              bamptr_get_qname(arr[j], qname, sizeof(qname));
++              fprintf(stderr, "(W) %s (qnameid %ld) is paired (shown in FLAG) but has no pair read in inputs (j %d arr_idx %ld flag %d)\n", qname, qnameid_j, j, arr_idx, flag);
++            }
++          } // if mate == NULL
++        } // if mate == NULL
++      } // for j
++    }
 +  } // for i
 +  md_free(arr, arr_sz*sizeof(arr[0]), mem_mtMateFinder__);
 +}

+    qname_hash_bucket = md_malloc(sizeof(*qname_hash_bucket)*QNAME_HASH_SIZE, mem_bamGetQnameId1, 1);
+}
+PROFILE_MEM_INIT();
+fprintf(stderr, "(I) sam-to-bam $Rev: 80 $\n");
+#if !defined(DEBUG_POS)
+fprintf(stderr, "(I) **** built with no DEBUG_POS feature\n");
+#endif

# DEALINGS IN THE SOFTWARE.
diff -uprN -x '*_Jul11' -x 'ibm_*' -x 'Numer*' -x a.out -x '#*#' -x version.h -x q -x '*~' -x '*.c.*' -x '*.a' -x .git -x .svn -x '*.MIT' 00_svn_r2/samtools-2015-08-19/samtools/bam_sam2bam.c ../files/samtools/bam_sam2bam.c
--- 00_svn_r2/samtools-2015-08-19/samtools/bam_sam2bam.c	1970-01-01 09:00:00.000000000 +0900
+++ ../files/samtools/bam_sam2bam.c	2016-10-24 17:27:16.277479651 +0900
@@ -0,0 +1,7797 @@
+#define _BAM_SAM2BAM_C
+/*  bam_sam2bam.c
+
