用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序在随机数据情况下的表现
时间: 2023-08-11 18:06:59 浏览: 106
好的,以下是用 Java 实现的比较排序算法在随机数据情况下的表现的代码:
```java
import java.util.Arrays;
import java.util.Random;
public class SortingComparison {
public static void main(String[] args) {
int[] arr = generateRandomArray(10000); // 生成随机数组
int[] arrCopy = Arrays.copyOf(arr, arr.length); // 复制数组,以便各个算法对同一数据进行排序
long start, end;
// 冒泡排序
start = System.currentTimeMillis();
bubbleSort(arrCopy);
end = System.currentTimeMillis();
System.out.println("冒泡排序所用时间:" + (end - start) + "ms");
// 选择排序
arrCopy = Arrays.copyOf(arr, arr.length); // 复制数组
start = System.currentTimeMillis();
selectionSort(arrCopy);
end = System.currentTimeMillis();
System.out.println("选择排序所用时间:" + (end - start) + "ms");
// 插入排序
arrCopy = Arrays.copyOf(arr, arr.length); // 复制数组
start = System.currentTimeMillis();
insertionSort(arrCopy);
end = System.currentTimeMillis();
System.out.println("插入排序所用时间:" + (end - start) + "ms");
// 希尔排序
arrCopy = Arrays.copyOf(arr, arr.length); // 复制数组
start = System.currentTimeMillis();
shellSort(arrCopy);
end = System.currentTimeMillis();
System.out.println("希尔排序所用时间:" + (end - start) + "ms");
// 归并排序
arrCopy = Arrays.copyOf(arr, arr.length); // 复制数组
start = System.currentTimeMillis();
mergeSort(arrCopy, 0, arrCopy.length - 1);
end = System.currentTimeMillis();
System.out.println("归并排序所用时间:" + (end - start) + "ms");
// 快速排序
arrCopy = Arrays.copyOf(arr, arr.length); // 复制数组
start = System.currentTimeMillis();
quickSort(arrCopy, 0, arrCopy.length - 1);
end = System.currentTimeMillis();
System.out.println("快速排序所用时间:" + (end - start) + "ms");
// 堆排序
arrCopy = Arrays.copyOf(arr, arr.length); // 复制数组
start = System.currentTimeMillis();
heapSort(arrCopy);
end = System.currentTimeMillis();
System.out.println("堆排序所用时间:" + (end - start) + "ms");
}
// 生成随机数组
private static int[] generateRandomArray(int length) {
int[] arr = new int[length];
Random random = new Random();
for (int i = 0; i < length; i++) {
arr[i] = random.nextInt(10000);
}
return arr;
}
// 冒泡排序
private static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
// 选择排序
private 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);
}
}
// 插入排序
private static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int current = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > current) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = current;
}
}
// 希尔排序
private static void shellSort(int[] arr) {
int n = arr.length;
int gap = n / 2;
while (gap > 0) {
for (int i = gap; i < n; i++) {
int current = arr[i];
int j = i - gap;
while (j >= 0 && arr[j] > current) {
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = current;
}
gap /= 2;
}
}
// 归并排序
private 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, j = mid + 1, 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 (i = 0; i < temp.length; i++) {
arr[left + i] = temp[i];
}
}
// 快速排序
private static void quickSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int pivot = partition(arr, left, right);
quickSort(arr, left, pivot - 1);
quickSort(arr, pivot + 1, right);
}
private static int partition(int[] arr, int left, int right) {
int pivot = arr[right];
int i = left;
for (int j = left; j < right; j++) {
if (arr[j] < pivot) {
swap(arr, i, j);
i++;
}
}
swap(arr, i, right);
return i;
}
// 堆排序
private 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;
}
}
```
这段代码将会生成一个长度为 10000 的随机数组,并对其进行冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序,输出各个算法的耗时。你可以根据实际情况修改数组长度,或者增加其他排序算法的实现。
阅读全文