免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
算法設(shè)計(jì)與分析 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); //合并到數(shù)組b
   s += s;
   mergePass(b, a, s); //合并到數(shù)組a
   s += s;
  }
 }

 /**
  * mergePass用于合并排好序的相鄰數(shù)組段,具體的合并算法由merge來實(shí)現(xiàn)
  *   @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段子數(shù)組
   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 ,復(fù)制到y(tǒng)
   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ù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
八大排序算法實(shí)戰(zhàn):思想與實(shí)現(xiàn)(下)
歸并排序
面試必備:冒泡,選擇,插入,希爾,歸并,快速排序大合集
排序算法 --- 歸并排序
Java實(shí)現(xiàn)歸并排序(Merge-Sort)算法
歸并排序三種實(shí)現(xiàn)方法(遞歸、非遞歸和自然合并排序)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服