特效视频网站,做最好的色书网站,丁香花影视大全,东莞免费自助建站模板常见排序算法实现
冒泡排序、选择排序、计数排序、插入排序、快速排序、堆排序、归并排序JAVA实现 文章目录 常见排序算法实现冒泡排序选择排序计数排序插入排序快速排序堆排序归并排序 冒泡排序
冒泡排序算法#xff0c;对给定的整数数组进行升序排序。冒泡排序是一种简单…常见排序算法实现
冒泡排序、选择排序、计数排序、插入排序、快速排序、堆排序、归并排序JAVA实现 文章目录 常见排序算法实现冒泡排序选择排序计数排序插入排序快速排序堆排序归并排序 冒泡排序
冒泡排序算法对给定的整数数组进行升序排序。冒泡排序是一种简单的排序算法通过多次遍历数组并相邻元素比较与交换来排列数组。代码最后将排序后的数组打印到控制台上输出结果为1 2 3 5 8 9。
public class BubbleSort {public static void main(String[] args) {int[] arr {5, 2, 8, 3, 9, 1};bubbleSort(arr);for (int i 0; i arr.length; i) {System.out.print(arr[i] );}}public static void bubbleSort(int[] arr) {int n arr.length;for (int i 0; i n - 1; i) {for (int j 0; j n - i - 1; j) {if (arr[j] arr[j 1]) {// swap arr[j] and arr[j1]int temp arr[j];arr[j] arr[j 1];arr[j 1] temp;}}}}
}选择排序
选择排序算法其主要功能是对一个整数数组进行升序排序。选择排序的基本思想是每次从未排序部分中选择最小元素将其放在已排好序的部分的末尾。该算法的时间复杂度为 O(n²)在数据量较小的情况下性能较为优秀。最终排序后的数组会被打印输出。
public class SelectionSort {public static void main(String[] args) {int[] arr {5, 2, 8, 3, 9, 1};selectionSort(arr); // sorting the array in ascending orderfor (int i 0; i arr.length; i) {System.out.print(arr[i] );}}public static void selectionSort(int[] arr) {for (int i 0; i arr.length - 1; i) {int minIndex i;for (int j i 1; j arr.length; j) {if (arr[j] arr[minIndex]) {minIndex j;}}if (minIndex! i) {int temp arr[i]; // swapping the elementsarr[i] arr[minIndex];arr[minIndex] temp;}}}
}计数排序
计数排序是一种非比较排序算法主要用于对范围较小的整数集合进行排序。其主要功能是对给定的整数数组 arr 进行从小到大的排序。该算法的时间复杂度为 O(n k)其中 n 是数组元素的个数k 是最大元素的值适合用于处理大量重复值的数据集。
public class CountingSort {public static void main(String[] args) {int[] arr {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};int max 9;int[] count new int[max 1];int[] output new int[arr.length];// Step 1: Count the frequency of each elementfor (int i 0; i arr.length; i) {count[arr[i]];}// Step 2: Calculate the cumulative sum of the frequencyfor (int i 1; i max; i) {count[i] count[i - 1];}// Step 3: Place each element in its correct position in the output arrayfor (int i arr.length - 1; i 0; i--) {output[count[arr[i]] - 1] arr[i];count[arr[i]]--;}// Step 4: Copy the output array to the original arrayfor (int i 0; i arr.length; i) {arr[i] output[i];}// Print the sorted arrayfor (int i 0; i arr.length; i) {System.out.print(arr[i] );}}
}插入排序
插入排序算法其主要功能是对一个随机生成的整数数组进行排序。插入排序是一种简单直观的排序算法适合于小规模的数组时间复杂度为 O(n^2)。通过不断将未排序的元素插入到已排序部分的合适位置最终得到一个升序排列的数组。代码中的 main 方法演示了如何使用这个方法并输出排序结果。
public class InsertionSort {public static void main(String[] args) {int[] arr {5, 2, 4, 6, 1, 3};insertionSort(arr);for (int i 0; i arr.length; i) {System.out.print(arr[i] );}}public static void insertionSort(int[] arr) {for (int i 1; i arr.length; i) {int key arr[i];int j i - 1;while (j 0 arr[j] key) {arr[j 1] arr[j];j--;}arr[j 1] key;}}
}快速排序
快速排序算法其主要功能是对一个整数数组进行排序。快速排序是一种高效的排序算法其平均时间复杂度为 O(n log n)。该代码通过选择支点通常是数组的最后一个元素然后将数组分为两个子数组递归地对这两个子数组进行排序最终得到一个有序的数组。打印输出展示了排序结果。
public class QuickSort {public static void main(String[] args) {int[] arr {5, 2, 8, 3, 9, 1, 7, 4, 6};quickSort(arr, 0, arr.length - 1);for (int i : arr) { System.out.print(i ); }}public static void quickSort(int[] arr, int left, int right) {if (left right) {int pivotIndex partition(arr, left, right);quickSort(arr, left, pivotIndex - 1);quickSort(arr, pivotIndex 1, right);}}public static int partition(int[] arr, int left, int right) {int pivot arr[right];int i left - 1;for (int j left; j right; j) {if (arr[j] pivot) {i;int temp arr[i];arr[i] arr[j];arr[j] temp;}}int temp arr[i 1];arr[i 1] arr[right];arr[right] temp;return i 1;}
}堆排序
堆排序的主要功能将一个整数数组排序。堆排序的过程包括建立最大堆并逐步将最大元素移动到数组的末尾最终得到升序排列的数组。整个算法的时间复杂度为 O(n log n)空间复杂度为 O(1)。堆排序是一种不稳定的排序算法。
public class HeapSort {public static void sort(int[] arr) {int n arr.length;for (int i n / 2 - 1; i 0; i--)heapify(arr, n, i);for (int i n - 1; i 0; i--) {int temp arr[0];arr[0] arr[i];arr[i] temp;heapify(arr, i, 0);}}private static void heapify(int[] arr, int n, int i) {int largest i;int l 2 * i 1;int r 2 * i 2;if (l n arr[l] arr[largest])largest l;if (r n arr[r] arr[largest])largest r;if (largest! i) {int swap arr[i];arr[i] arr[largest];arr[largest] swap;heapify(arr, n, largest);}}
}归并排序
归并排序是一种有效的排序算法采用分治法的思想将待排序的数组递归地分成两半直至每个子数组只有一个元素然后再将这些子数组合并为一个有序的整体。最终该程序能够将输入的数组 {5, 2, 8, 3, 9, 1, 7, 4, 6} 排序并打印输出。归并排序的时间复杂度为O(nlogn) 使其在处理大型数据集时十分高效。
public class MergeSort {public static void main(String[] args) {int[] arr {5, 2, 8, 3, 9, 1, 7, 4, 6};mergeSort(arr, 0, arr.length - 1);for (int i 0; i arr.length; i) {System.out.print(arr[i] );}}public static void mergeSort(int[] arr, int left, int right) {if (left right) {int mid (left right) / 2;mergeSort(arr, left, mid);mergeSort(arr, mid 1, right);merge(arr, left, mid, right);}}public static void merge(int[] arr, int left, int mid, int right) {int[] temp new int[right - left 1];int i left;int j mid 1;int k 0;while (i mid j right) {if (arr[i] arr[j]) {temp[k] arr[i];} else {temp[k] arr[j];}}while (i mid) {temp[k] arr[i];}while (j right) {temp[k] arr[j];}for (i left; i right; i) {arr[i] temp[i - left];}}
}
文章转载自: http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.fxqjz.cn.gov.cn.fxqjz.cn http://www.morning.gbfck.cn.gov.cn.gbfck.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.wyctq.cn.gov.cn.wyctq.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.mrpqg.cn.gov.cn.mrpqg.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn http://www.morning.jtqxs.cn.gov.cn.jtqxs.cn http://www.morning.dxqfh.cn.gov.cn.dxqfh.cn http://www.morning.gpryk.cn.gov.cn.gpryk.cn http://www.morning.mqwnp.cn.gov.cn.mqwnp.cn http://www.morning.sgbss.cn.gov.cn.sgbss.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.jqrhz.cn.gov.cn.jqrhz.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.rkrl.cn.gov.cn.rkrl.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.ztnmc.cn.gov.cn.ztnmc.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.djmdk.cn.gov.cn.djmdk.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.lndongguan.com.gov.cn.lndongguan.com http://www.morning.rwtlj.cn.gov.cn.rwtlj.cn http://www.morning.sjgsh.cn.gov.cn.sjgsh.cn http://www.morning.kdnrp.cn.gov.cn.kdnrp.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.wmglg.cn.gov.cn.wmglg.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.cnprt.cn.gov.cn.cnprt.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.slfmp.cn.gov.cn.slfmp.cn http://www.morning.tntgc.cn.gov.cn.tntgc.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.nfbnl.cn.gov.cn.nfbnl.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.xshkh.cn.gov.cn.xshkh.cn http://www.morning.nrtpb.cn.gov.cn.nrtpb.cn