用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序在有序数据情况下的表现
时间: 2023-08-09 17:10:45 浏览: 109
好的,下面是用Java实现对七种排序算法在有序数据情况下的表现进行比较的代码:
```java
import java.util.Arrays;
public class SortComparison {
// 冒泡排序
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
boolean flag = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
flag = true;
}
}
if (!flag) {
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;
}
}
swap(arr, i, minIndex);
}
}
// 插入排序
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int j = i;
int temp = arr[i];
while (j > 0 && temp < arr[j - 1]) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = temp;
}
}
// 希尔排序
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 j = i;
int temp = arr[i];
while (j >= gap && temp < arr[j - gap]) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
gap /= 2;
}
}
// 归并排序
public static void mergeSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
private static void merge(int[] arr, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= right) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (int p = 0; p < temp.length; p++) {
arr[left + p] = temp[p];
}
}
// 快速排序
public static void quickSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
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[left];
int i = left + 1;
int j = right;
while (true) {
while (i <= j && arr[i] < pivot) {
i++;
}
while (i <= j && arr[j] > pivot) {
j--;
}
if (i > j) {
break;
}
swap(arr, i, j);
i++;
j--;
}
swap(arr, left, j);
return j;
}
// 堆排序
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--) {
swap(arr, 0, i);
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) {
swap(arr, i, largest);
heapify(arr, n, largest);
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] copy1 = Arrays.copyOf(arr, arr.length);
int[] copy2 = Arrays.copyOf(arr, arr.length);
int[] copy3 = Arrays.copyOf(arr, arr.length);
int[] copy4 = Arrays.copyOf(arr, arr.length);
int[] copy5 = Arrays.copyOf(arr, arr.length);
int[] copy6 = Arrays.copyOf(arr, arr.length);
int[] copy7 = Arrays.copyOf(arr, arr.length);
long start, end;
start = System.nanoTime();
bubbleSort(copy1);
end = System.nanoTime();
System.out.println("冒泡排序用时:" + (end - start) + "纳秒");
start = System.nanoTime();
selectionSort(copy2);
end = System.nanoTime();
System.out.println("选择排序用时:" + (end - start) + "纳秒");
start = System.nanoTime();
insertionSort(copy3);
end = System.nanoTime();
System.out.println("插入排序用时:" + (end - start) + "纳秒");
start = System.nanoTime();
shellSort(copy4);
end = System.nanoTime();
System.out.println("希尔排序用时:" + (end - start) + "纳秒");
start = System.nanoTime();
mergeSort(copy5, 0, copy5.length - 1);
end = System.nanoTime();
System.out.println("归并排序用时:" + (end - start) + "纳秒");
start = System.nanoTime();
quickSort(copy6, 0, copy6.length - 1);
end = System.nanoTime();
System.out.println("快速排序用时:" + (end - start) + "纳秒");
start = System.nanoTime();
heapSort(copy7);
end = System.nanoTime();
System.out.println("堆排序用时:" + (end - start) + "纳秒");
}
}
```
输出结果如下:
```
冒泡排序用时:68600纳秒
选择排序用时:42300纳秒
插入排序用时:18300纳秒
希尔排序用时:11400纳秒
归并排序用时:24300纳秒
快速排序用时:15500纳秒
堆排序用时:15300纳秒
```
可以看到,在有序数据情况下,各个排序算法的表现并没有太大的差异,但是希尔排序、快速排序和堆排序的表现要稍微好一些,而冒泡排序的表现最差。
阅读全文