提示信息

Java实现八种常见排序算法

痴人梦 已关注

在Java中实现八种常见的排序算法可以帮助你更好地理解这些算法的工作原理。以下是八种常见排序算法的Java实现:

  1. 冒泡排序(Bubble Sort)
  1. public void bubbleSort(int[] array) {
  2. int n = array.length;
  3. for (int i = 0; i < n - 1; i++) {
  4. for (int j = 0; j < n - i - 1; j++) {
  5. if (array[j] > array[j + 1]) {
  6. int temp = array[j];
  7. array[j] = array[j + 1];
  8. array[j + 1] = temp;
  9. }
  10. }
  11. }
  12. }
  1. 选择排序(Selection Sort):
  1. public void selectionSort(int[] array) {
  2. int n = array.length;
  3. for (int i = 0; i < n - 1; i++) {
  4. int minIndex = i;
  5. for (int j = i + 1; j < n; j++) {
  6. if (array[j] < array[minIndex]) {
  7. minIndex = j;
  8. }
  9. }
  10. int temp = array[minIndex];
  11. array[minIndex] = array[i];
  12. array[i] = temp;
  13. }
  14. }
  1. 插入排序(Insertion Sort):
  1. public void insertionSort(int[] array) {
  2. int n = array.length;
  3. for (int i = 1; i < n; i++) {
  4. int key = array[i];
  5. int j = i - 1;
  6. while (j >= 0 && array[j] > key) {
  7. array[j + 1] = array[j];
  8. j--;
  9. }
  10. array[j + 1] = key;
  11. }
  12. }
  1. 希尔排序(Shell Sort):
  1. public void shellSort(int[] array) {
  2. int n = array.length;
  3. for (int gap = n / 2; gap > 0; gap /= 2) {
  4. for (int i = gap; i < n; i++) {
  5. int key = array[i];
  6. int j = i;
  7. while (j >= gap && array[j - gap] > key) {
  8. array[j] = array[j - gap];
  9. j -= gap;
  10. }
  11. array[j] = key;
  12. }
  13. }
  14. }
  1. 归并排序(Merge Sort):
  1. public void mergeSort(int[] array, int left, int right) {
  2. if (left < right) {
  3. int mid = (left + right) / 2;
  4. mergeSort(array, left, mid);
  5. mergeSort(array, mid + 1, right);
  6. merge(array, left, mid, right);
  7. }
  8. }
  9. private void merge(int[] array, int left, int mid, int right) {
  10. int n1 = mid - left + 1;
  11. int n2 = right - mid;
  12. int[] L = new int[n1];
  13. int[] R = new int[n2];
  14. for (int i = 0; i < n1; ++i)
  15. L[i] = array[left + i];
  16. for (int j = 0; j < n2; ++j)
  17. R[j] = array[mid + 1 + j];
  18. int i = 0, j = 0;
  19. int k = left;
  20. while (i < n1 && j < n2) {
  21. if (L[i] <= R[j]) {
  22. array[k] = L[i];
  23. i++;
  24. } else {
  25. array[k] = R[j];
  26. j++;
  27. }
  28. k++;
  29. }
  30. while (i < n1) {
  31. array[k] = L[i];
  32. i++;
  33. k++;
  34. }
  35. while (j < n2) {
  36. array[k] = R[j];
  37. j++;
  38. k++;
  39. }
  40. }
  1. 快速排序(Quick Sort):
  1. public void quickSort(int[] array, int low, int high) {
  2. if (low < high) {
  3. int pi = partition(array, low, high);
  4. quickSort(array, low, pi - 1);
  5. quickSort(array, pi + 1, high);
  6. }
  7. }
  8. private int partition(int[] array, int low, int high) {
  9. int pivot = array[high];
  10. int i = (low - 1);
  11. for (int j = low; j < high; j++) {
  12. if (array[j] <= pivot) {
  13. i++;
  14. int temp = array[i];
  15. array[i] = array[j];
  16. array[j] = temp;
  17. }
  18. }
  19. int temp = array[i + 1];
  20. array[i + 1] = array[high];
  21. array[high] = temp;
  22. return i + 1;
  23. }
  1. 堆排序(Heap Sort):
  1. public void heapSort(int[] array) {
  2. int n = array.length;
  3. for (int i = n / 2 - 1; i >= 0; i--) {
  4. heapify(array, n, i);
  5. }
  6. for (int i = n - 1; i >= 0; i--) {
  7. int temp = array[0];
  8. array[0] = array[i];
  9. array[i] = temp;
  10. heapify(array, i, 0);
  11. }
  12. }
  13. private void heapify(int[] array, int n, int i) {
  14. int largest = i;
  15. int left = 2 * i + 1;
  16. int right = 2 * i + 2;
  17. if (left < n && array[left] > array[largest]) {
  18. largest = left;
  19. }
  20. if (right < n && array[right] > array[largest]) {
  21. largest = right;
  22. }
  23. if (largest != i) {
  24. int swap = array[i];
  25. array[i] = array[largest];
  26. array[largest] = swap;
  27. heapify(array, n, largest);
  28. }
  29. }
  1. 计数排序(Counting Sort):
  1. public void countingSort(int[] array) {
  2. int max = Arrays.stream(array).max().getAsInt();
  3. int min = Arrays.stream(array).min().getAsInt();
  4. int range = max - min + 1;
  5. int[] count = new int[range];
  6. int[] output = new int[array.length];
  7. for (int i = 0; i < array.length; i++) {
  8. count[array[i] - min]++;
  9. }
  10. for (int i = 1; i < count.length; i++) {
  11. count[i] += count[i - 1];
  12. }
  13. for (int i = array.length - 1; i >= 0; i--) {
  14. output[count[array[i] - min] - 1] = array[i];
  15. count[array[i] - min]--;
  16. }
  17. for (int i = 0; i < array.length; i++) {
  18. array[i] = output[i];
  19. }
  20. }

这些算法各有优缺点,选择使用哪种算法取决于你的数据集的特点和应用的要求。

    遇到难题? "AI大模型GPT4.0、GPT" 是你的私人解答专家! 点击按钮去提问......
痴人梦 关注 已关注

最近一次登录:2024-11-21 10:02:01   

暂时还没有签名,请关注我或评论我的文章
×
免费图表工具,画流程图、架构图