广州微网站建设哪家好,建设 政务数据共享网站,哪里建网站便宜,什么事三合一网站常见排序算法实现
冒泡排序、选择排序、计数排序、插入排序、快速排序、堆排序、归并排序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];}}
}