欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
算法設計與分析 2.7 歸并排序

import java.util.Arrays;

public class MergeSort {
 public static void mergeSort(int[] a, int start, int end) {
  if (start < end) {
   int mid = (start + end) / 2;
   mergeSort(a, start, mid);
   mergeSort(a, mid + 1, end);

   merge(a, start, mid, end);
  }
 }

 private static void merge(int[] a, int start, int mid, int end) {
  int[] temp = new int[a.length];
  int i = start, j = mid + 1, k = start;
  while (i <= mid && j <= end) {
   if (a[i] < a[j]) {
    temp[k] = a[i];
    k++;
    i++;
   } else {
    temp[k] = a[j];
    k++;
    j++;
   }
  }
  if (i > mid) {
   while (j <= end) {
    temp[k] = a[j];
    k++;
    j++;
   }
  } else {
   while (i <= mid) {
    temp[k] = a[i];
    k++;
    i++;
   }
  }
  for (k = start; k <= end; k++) {
   a[k] = temp[k];
  }
 }

 /**
  * 非遞歸版本
  * mergeSort sort the int array a
  * @param a
  *  the array to be sorted
  **/
 public static void mergeSort(int[] a) {
  int[] b = new int[a.length];
  int s = 1;
  while (s < a.length) {
   mergePass(a, b, s); //合并到數組b
   s += s;
   mergePass(b, a, s); //合并到數組a
   s += s;
  }
 }

 /**
  * mergePass用于合并排好序的相鄰數組段,具體的合并算法由merge來(lái)實(shí)現
  *   @param x
  *  the src
  * @param y
  *  the des
  * @param s
  *  the size of merge array
  **/
 public static void mergePass(int[] x, int[] y, int s) {
  int i = 0;
  while (i < x.length - 2 * s) {
   //合并大小為s的相鄰2段子數組
   merge(x, y, i, i + s - 1, i + 2 * s - 1);
   i += 2 * s;
  }
  //剩下的元素少于2 * s
  if (i + s < x.length) {//剩下的元素小于 2 * s 但大于 s
   merge(x, y, i, i + s - 1, x.length - 1);
  } else {
   //剩下的元素小于 s ,復制到y
   for (int j = i; j < x.length; j++) {
    y[j] = x[j];
   }
  }
 }

 /**
  * 合并c[l:m]和c[m+1:r]到d[l:r]
  *
  * @param c
  *  the src array
  * @param d
  *  the des array
  * @param l
  *  the start
  * @param m
  *  the mid
  * @param r
  *  the end
  *
  **/
 public static void merge(int[] c, int[] d, int l, int m, int r) {
  int i = l;
  int j = m + 1;
  int k = l;
  while ((i <= m) && (j <= r)) {
   if (c[i] <= c[j]) {
    d[k++] = c[i++];
   } else {
    d[k++] = c[j++];
   }
  }
  if (i > m) {
   for (int q = j; q <= r; q++) {
    d[k++] = c[q];
   }
  } else {
   for (int q = i; q <= m; q++) {
    d[k++] = c[q];
   }
  }
 }

 public static void main(String[] args) {
  int[] a = { 3, 4, 1, 5, 9, 3, 2, 8, 10 };
  int[] b = { 3, 4, 1, 5, 9, 3, 2, 8, 10 };
  mergeSort(a, 0, a.length - 1);
  mergeSort(b);
  System.out.println(Arrays.toString(a));
  System.out.println(Arrays.toString(b));
 }
}

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
八大排序算法實(shí)戰:思想與實(shí)現(下)
歸并排序
面試必備:冒泡,選擇,插入,希爾,歸并,快速排序大合集
排序算法 --- 歸并排序
Java實(shí)現歸并排序(Merge-Sort)算法
歸并排序三種實(shí)現方法(遞歸、非遞歸和自然合并排序)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久