怎样实现java数组的稳定排序

avatar
作者
猴君
阅读量:0

在 Java 中,稳定排序是指相等的元素在排序后保持原有的相对顺序。Java 提供了稳定的排序算法,如冒泡排序、插入排序和归并排序。以下是这些排序算法的实现:

  1. 冒泡排序(Bubble Sort):
public class BubbleSort {     public static void main(String[] args) {         int[] arr = {64, 34, 25, 12, 22, 11, 90};         bubbleSort(arr);         System.out.println("Sorted array is:");         printArray(arr);     }      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+1] 和 arr[j]                     int temp = arr[j];                     arr[j] = arr[j + 1];                     arr[j + 1] = temp;                 }             }         }     }      public static void printArray(int[] arr) {         for (int value : arr) {             System.out.print(value + " ");         }     } } 
  1. 插入排序(Insertion Sort):
public class InsertionSort {     public static void main(String[] args) {         int[] arr = {64, 34, 25, 12, 22, 11, 90};         insertionSort(arr);         System.out.println("Sorted array is:");         printArray(arr);     }      public static void insertionSort(int[] arr) {         int n = arr.length;         for (int i = 1; i < n; ++i) {             int key = arr[i];             int j = i - 1;              // Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position             while (j >= 0 && arr[j] > key) {                 arr[j + 1] = arr[j];                 j = j - 1;             }             arr[j + 1] = key;         }     }      public static void printArray(int[] arr) {         for (int value : arr) {             System.out.print(value + " ");         }     } } 
  1. 归并排序(Merge Sort):
public class MergeSort {     public static void main(String[] args) {         int[] arr = {64, 34, 25, 12, 22, 11, 90};         mergeSort(arr, 0, arr.length - 1);         System.out.println("Sorted array is:");         printArray(arr);     }      public static void mergeSort(int[] arr, int left, int right) {         if (left < right) {             int middle = (left + right) / 2;              // Sort first and second halves             mergeSort(arr, left, middle);             mergeSort(arr, middle + 1, right);              merge(arr, left, middle, right);         }     }      public static void merge(int[] arr, int left, int middle, int right) {         int n1 = middle - left + 1;         int n2 = right - middle;          // Create temp arrays         int[] leftArray = new int[n1];         int[] rightArray = new int[n2];          // Copy data to temp arrays         for (int i = 0; i < n1; ++i)             leftArray[i] = arr[left + i];         for (int j = 0; j < n2; ++j)             rightArray[j] = arr[middle + 1 + j];          // Merge the temp arrays         int i = 0, j = 0;         int k = left;         while (i < n1 && j < n2) {             if (leftArray[i] <= rightArray[j]) {                 arr[k] = leftArray[i];                 i++;             } else {                 arr[k] = rightArray[j];                 j++;             }             k++;         }          // Copy the remaining elements of leftArray         while (i < n1) {             arr[k] = leftArray[i];             i++;             k++;         }          // Copy the remaining elements of rightArray         while (j < n2) {             arr[k] = rightArray[j];             j++;             k++;         }     }      public static void printArray(int[] arr) {         for (int value : arr) {             System.out.print(value + " ");         }     } } 

这些排序算法都是稳定的,你可以根据需要选择合适的算法。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!