用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序在逆序数据情况下的表现
时间: 2023-08-10 19:03:26 浏览: 109
Java中七大基于比较的排序算法
好的,以下是一个使用Java语言实现的比较排序算法在逆序数据情况下的表现的程序:
```java
import java.util.Arrays;
public class SortComparison {
public static void main(String[] args) {
int[] arr = new int[] {9, 8, 7, 6, 5, 4, 3, 2, 1};
System.out.println("Original Array: " + Arrays.toString(arr));
int[] result1 = bubbleSort(arr.clone());
System.out.println("Result of Bubble Sort: " + Arrays.toString(result1));
int[] result2 = selectionSort(arr.clone());
System.out.println("Result of Selection Sort: " + Arrays.toString(result2));
int[] result3 = insertionSort(arr.clone());
System.out.println("Result of Insertion Sort: " + Arrays.toString(result3));
int[] result4 = shellSort(arr.clone());
System.out.println("Result of Shell Sort: " + Arrays.toString(result4));
int[] result5 = mergeSort(arr.clone());
System.out.println("Result of Merge Sort: " + Arrays.toString(result5));
int[] result6 = quickSort(arr.clone());
System.out.println("Result of Quick Sort: " + Arrays.toString(result6));
int[] result7 = heapSort(arr.clone());
System.out.println("Result of Heap Sort: " + Arrays.toString(result7));
}
public static int[] 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]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
public static int[] selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
return arr;
}
public static int[] insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
return arr;
}
public static int[] shellSort(int[] arr) {
int n = arr.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
return arr;
}
public static int[] mergeSort(int[] arr) {
int n = arr.length;
if (n < 2) {
return arr;
}
int mid = n / 2;
int[] leftArr = Arrays.copyOfRange(arr, 0, mid);
int[] rightArr = Arrays.copyOfRange(arr, mid, n);
return merge(mergeSort(leftArr), mergeSort(rightArr));
}
public static int[] merge(int[] leftArr, int[] rightArr) {
int leftLen = leftArr.length;
int rightLen = rightArr.length;
int[] result = new int[leftLen + rightLen];
int i = 0, j = 0, k = 0;
while (i < leftLen && j < rightLen) {
if (leftArr[i] <= rightArr[j]) {
result[k++] = leftArr[i++];
} else {
result[k++] = rightArr[j++];
}
}
while (i < leftLen) {
result[k++] = leftArr[i++];
}
while (j < rightLen) {
result[k++] = rightArr[j++];
}
return result;
}
public static int[] quickSort(int[] arr) {
return quickSort(arr, 0, arr.length - 1);
}
public static int[] 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);
}
return arr;
}
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;
}
public static int[] heapSort(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);
}
return arr;
}
public static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
}
```
这个程序将会输出各个排序算法在逆序数据情况下的表现结果,你可以根据这些结果来比较各个排序算法在逆序数据情况下的表现。
阅读全文