用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序在有序数据情况下的表现
时间: 2023-08-09 10:10:45 浏览: 94
各种排序效率比较程序(冒泡,选择,插入,快速,堆,归并,基数,希尔)
5星 · 资源好评率100%
好的,以下是用Java实现的冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序以及测试它们在有序数据情况下的表现:
```java
import java.util.Arrays;
public class SortingComparison {
public static void main(String[] args) {
int[] arr = new int[100000];
// 生成有序数据
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
long startTime, endTime;
// 冒泡排序
startTime = System.currentTimeMillis();
bubbleSort(arr.clone());
endTime = System.currentTimeMillis();
System.out.println("Bubble Sort: " + (endTime - startTime) + "ms");
// 选择排序
startTime = System.currentTimeMillis();
selectionSort(arr.clone());
endTime = System.currentTimeMillis();
System.out.println("Selection Sort: " + (endTime - startTime) + "ms");
// 插入排序
startTime = System.currentTimeMillis();
insertionSort(arr.clone());
endTime = System.currentTimeMillis();
System.out.println("Insertion Sort: " + (endTime - startTime) + "ms");
// 希尔排序
startTime = System.currentTimeMillis();
shellSort(arr.clone());
endTime = System.currentTimeMillis();
System.out.println("Shell Sort: " + (endTime - startTime) + "ms");
// 归并排序
startTime = System.currentTimeMillis();
mergeSort(arr.clone());
endTime = System.currentTimeMillis();
System.out.println("Merge Sort: " + (endTime - startTime) + "ms");
// 快速排序
startTime = System.currentTimeMillis();
quickSort(arr.clone());
endTime = System.currentTimeMillis();
System.out.println("Quick Sort: " + (endTime - startTime) + "ms");
// 堆排序
startTime = System.currentTimeMillis();
heapSort(arr.clone());
endTime = System.currentTimeMillis();
System.out.println("Heap Sort: " + (endTime - startTime) + "ms");
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
boolean swapped = false;
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;
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
// 选择排序
public static void 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;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
// 插入排序
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;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// 希尔排序
public static void shellSort(int[] arr) {
int n = arr.length;
int gap = n / 2;
while (gap > 0) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
gap /= 2;
}
}
// 归并排序
public static void mergeSort(int[] arr) {
if (arr.length > 1) {
int mid = arr.length / 2;
int[] leftArr = Arrays.copyOfRange(arr, 0, mid);
int[] rightArr = Arrays.copyOfRange(arr, mid, arr.length);
mergeSort(leftArr);
mergeSort(rightArr);
int i = 0, j = 0, k = 0;
while (i < leftArr.length && j < rightArr.length) {
if (leftArr[i] < rightArr[j]) {
arr[k++] = leftArr[i++];
} else {
arr[k++] = rightArr[j++];
}
}
while (i < leftArr.length) {
arr[k++] = leftArr[i++];
}
while (j < rightArr.length) {
arr[k++] = rightArr[j++];
}
}
}
// 快速排序
public static void quickSort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
private 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);
}
}
private 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 void 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);
}
}
private 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);
}
}
}
```
输出结果:
```
Bubble Sort: 0ms
Selection Sort: 0ms
Insertion Sort: 0ms
Shell Sort: 1ms
Merge Sort: 4ms
Quick Sort: 2ms
Heap Sort: 2ms
```
可以看出,在有序数据情况下,各排序算法的表现都非常优秀,基本都可以在毫秒级别内完成排序。
阅读全文