茂港手机网站建设公司,自己做的网站打开太慢,网站建设与网页设计制作绿皮书,u9u8网站建设递归排序是一种基于分治法的排序算法#xff0c;最典型的例子就是快速排序和归并排序。这两种算法都利用递归将问题分解成更小的子问题#xff0c;然后将子问题的解合并以得到原始问题的解。
1、快速排序#xff08;Quick Sort#xff09;
快速排序的基本思想是选择一个基…递归排序是一种基于分治法的排序算法最典型的例子就是快速排序和归并排序。这两种算法都利用递归将问题分解成更小的子问题然后将子问题的解合并以得到原始问题的解。
1、快速排序Quick Sort
快速排序的基本思想是选择一个基准元素将序列分割成两个子序列小于基准元素的放在左边大于基准元素的放在右边然后对左右子序列递归地进行快速排序。
public class QuickSort {public static void quickSort(int[] arr) {if (arr null || arr.length 0) {return;}quickSort(arr, 0, arr.length - 1);}private static void quickSort(int[] arr, int left, int right) {if (left right) {return;}int pivot partition(arr, left, right); // 分区操作返回基准元素的位置quickSort(arr, left, pivot - 1); // 递归排序左子数组quickSort(arr, pivot 1, right); // 递归排序右子数组}private static int partition(int[] arr, int left, int right) {int pivot arr[right]; // 选择最右边的元素作为基准int i left - 1; // i指向小于基准元素的最后一个元素for (int j left; j right; j) {if (arr[j] pivot) {i;swap(arr, i, j); // 将小于基准的元素交换到左边}}swap(arr, i 1, right); // 将基准元素交换到正确的位置return i 1; // 返回基准元素的位置}private static void swap(int[] arr, int i, int j) {int temp arr[i];arr[i] arr[j];arr[j] temp;}public static void main(String[] args) {int[] arr { 5, 2, 9, 3, 6, 1, 8, 7, 4 };quickSort(arr);System.out.println(array: Arrays.toString(arr));}
}2、归并排序Merge Sort
归并排序采用分治法将序列分成两个子序列分别对它们进行排序然后将已排序的子序列合并成一个有序序列。
public class MergeSort {public static void mergeSort(int[] arr) {if (arr null || arr.length 2) {return;}int[] temp new int[arr.length];mergeSort(arr, 0, arr.length - 1, temp);}private static void mergeSort(int[] arr, int left, int right, int[] temp) {if (left right) {int mid (left right) / 2;mergeSort(arr, left, mid, temp); // 对左半部分进行归并排序mergeSort(arr, mid 1, right, temp); // 对右半部分进行归并排序merge(arr, left, mid, right, temp); // 合并左右两部分}}private static void merge(int[] arr, int left, int mid, int right, int[] temp) {int i left; // 左序列指针int j mid 1; // 右序列指针int t 0; // 临时数组指针while (i mid j right) {if (arr[i] arr[j]) {temp[t] arr[i];} else {temp[t] arr[j];}}while (i mid) {temp[t] arr[i]; // 将左边剩余元素填充进temp中}while (j right) {temp[t] arr[j]; // 将右边剩余元素填充进temp中}t 0;// 将temp中的元素全部拷贝到原数组中while (left right) {arr[left] temp[t];}}public static void main(String[] args) {int[] arr { 5, 2, 9, 3, 6, 1, 8, 7, 4 };mergeSort(arr);System.out.println(array: Arrays.toString(arr));}
} 文章转载自: http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.jykzy.cn.gov.cn.jykzy.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.bzsqr.cn.gov.cn.bzsqr.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.swdnr.cn.gov.cn.swdnr.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.bszmy.cn.gov.cn.bszmy.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.jtybl.cn.gov.cn.jtybl.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.mxnfh.cn.gov.cn.mxnfh.cn http://www.morning.pxlpt.cn.gov.cn.pxlpt.cn http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.jbhhj.cn.gov.cn.jbhhj.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.hxxzp.cn.gov.cn.hxxzp.cn http://www.morning.rfbq.cn.gov.cn.rfbq.cn http://www.morning.fkwp.cn.gov.cn.fkwp.cn http://www.morning.tldfp.cn.gov.cn.tldfp.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.mbfj.cn.gov.cn.mbfj.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.gdgylp.com.gov.cn.gdgylp.com http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.rnytd.cn.gov.cn.rnytd.cn http://www.morning.rsjng.cn.gov.cn.rsjng.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.stflb.cn.gov.cn.stflb.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.dthyq.cn.gov.cn.dthyq.cn http://www.morning.lfpzs.cn.gov.cn.lfpzs.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.huxinzuche.cn.gov.cn.huxinzuche.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.lnbyk.cn.gov.cn.lnbyk.cn http://www.morning.hyxwh.cn.gov.cn.hyxwh.cn http://www.morning.wkmrl.cn.gov.cn.wkmrl.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.zknjy.cn.gov.cn.zknjy.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.zqybs.cn.gov.cn.zqybs.cn http://www.morning.ktmnq.cn.gov.cn.ktmnq.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn