用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序在有序数据情况下的表现
时间: 2023-08-11 20:07:00 浏览: 80
Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
5星 · 资源好评率100%
好的,下面是一段Java代码,实现了对以上七种排序算法在有序数据情况下的性能比较:
```java
import java.util.Arrays;
public class SortingComparison {
public static void main(String[] args) {
// 生成有序数据
int[] arr = new int[10000];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
// 冒泡排序
int[] arr1 = Arrays.copyOf(arr, arr.length);
long start1 = System.nanoTime();
bubbleSort(arr1);
long end1 = System.nanoTime();
System.out.println("冒泡排序耗时:" + (end1 - start1) + "纳秒");
// 选择排序
int[] arr2 = Arrays.copyOf(arr, arr.length);
long start2 = System.nanoTime();
selectionSort(arr2);
long end2 = System.nanoTime();
System.out.println("选择排序耗时:" + (end2 - start2) + "纳秒");
// 插入排序
int[] arr3 = Arrays.copyOf(arr, arr.length);
long start3 = System.nanoTime();
insertionSort(arr3);
long end3 = System.nanoTime();
System.out.println("插入排序耗时:" + (end3 - start3) + "纳秒");
// 希尔排序
int[] arr4 = Arrays.copyOf(arr, arr.length);
long start4 = System.nanoTime();
shellSort(arr4);
long end4 = System.nanoTime();
System.out.println("希尔排序耗时:" + (end4 - start4) + "纳秒");
// 归并排序
int[] arr5 = Arrays.copyOf(arr, arr.length);
long start5 = System.nanoTime();
mergeSort(arr5);
long end5 = System.nanoTime();
System.out.println("归并排序耗时:" + (end5 - start5) + "纳秒");
// 快速排序
int[] arr6 = Arrays.copyOf(arr, arr.length);
long start6 = System.nanoTime();
quickSort(arr6, 0, arr6.length - 1);
long end6 = System.nanoTime();
System.out.println("快速排序耗时:" + (end6 - start6) + "纳秒");
// 堆排序
int[] arr7 = Arrays.copyOf(arr, arr.length);
long start7 = System.nanoTime();
heapSort(arr7);
long end7 = System.nanoTime();
System.out.println("堆排序耗时:" + (end7 - start7) + "纳秒");
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
boolean swapped = false;
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
// 选择排序
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) {
swap(arr, i, minIndex);
}
}
}
// 插入排序
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int temp = arr[i];
int j;
for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
// 希尔排序
public static void shellSort(int[] arr) {
int gap = arr.length / 2;
while (gap > 0) {
for (int i = gap; i < arr.length; i++) {
int temp = arr[i];
int j;
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
gap /= 2;
}
}
// 归并排序
public static void mergeSort(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
private static void mergeSort(int[] arr, int start, int end) {
if (start >= end) {
return;
}
int mid = (start + end) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
private static void merge(int[] arr, int start, int mid, int end) {
int[] temp = new int[end - start + 1];
int i = start, j = mid + 1, k = 0;
while (i <= mid && j <= end) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= end) {
temp[k++] = arr[j++];
}
for (int p = 0; p < temp.length; p++) {
arr[start + p] = temp[p];
}
}
// 快速排序
public static void quickSort(int[] arr, int start, int end) {
if (start >= end) {
return;
}
int pivotIndex = partition(arr, start, end);
quickSort(arr, start, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, end);
}
private static int partition(int[] arr, int start, int end) {
int pivot = arr[start];
int left = start + 1;
int right = end;
while (left <= right) {
while (left <= right && arr[left] <= pivot) {
left++;
}
while (left <= right && arr[right] >= pivot) {
right--;
}
if (left <= right) {
swap(arr, left, right);
}
}
swap(arr, start, right);
return right;
}
// 堆排序
public static void heapSort(int[] arr) {
for (int i = arr.length / 2 - 1; i >= 0; i--) {
heapify(arr, i, arr.length);
}
for (int i = arr.length - 1; i >= 0; i--) {
swap(arr, 0, i);
heapify(arr, 0, i);
}
}
private static void heapify(int[] arr, int i, int n) {
int left = i * 2 + 1;
int right = i * 2 + 2;
int largest = i;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
swap(arr, i, largest);
heapify(arr, largest, n);
}
}
// 交换数组中的两个元素
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
```
这段代码会生成一个长度为10000的有序数组,并对该数组进行冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序,分别计算它们的耗时,并输出结果。由于数组是有序的,所有排序算法的性能都应该比较好,但不同算法的耗时也会有所差异。
阅读全文