用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序等算法在不同数据量的情况下的比较
时间: 2023-08-12 20:07:59 浏览: 97
好的,这里是一个使用Java编写的比较算法在不同数据量下表现的程序:
```java
import java.util.Arrays;
import java.util.Random;
public class SortingAlgorithmComparison {
public static void main(String[] args) {
int[] sizes = {10, 100, 1000, 10000, 100000};
for (int size : sizes) {
int[] arr1 = generateArray(size);
int[] arr2 = Arrays.copyOf(arr1, arr1.length);
int[] arr3 = Arrays.copyOf(arr1, arr1.length);
int[] arr4 = Arrays.copyOf(arr1, arr1.length);
int[] arr5 = Arrays.copyOf(arr1, arr1.length);
int[] arr6 = Arrays.copyOf(arr1, arr1.length);
int[] arr7 = Arrays.copyOf(arr1, arr1.length);
System.out.println("For size " + size + ":");
long time1 = timeAlgorithm(arr1, "Bubble Sort");
long time2 = timeAlgorithm(arr2, "Selection Sort");
long time3 = timeAlgorithm(arr3, "Insertion Sort");
long time4 = timeAlgorithm(arr4, "Shell Sort");
long time5 = timeAlgorithm(arr5, "Merge Sort");
long time6 = timeAlgorithm(arr6, "Quick Sort");
long time7 = timeAlgorithm(arr7, "Heap Sort");
System.out.println("----------------------------------------");
System.out.println("Bubble Sort: " + time1 + "ms");
System.out.println("Selection Sort: " + time2 + "ms");
System.out.println("Insertion Sort: " + time3 + "ms");
System.out.println("Shell Sort: " + time4 + "ms");
System.out.println("Merge Sort: " + time5 + "ms");
System.out.println("Quick Sort: " + time6 + "ms");
System.out.println("Heap Sort: " + time7 + "ms");
System.out.println("----------------------------------------");
}
}
private static int[] generateArray(int size) {
int[] arr = new int[size];
Random random = new Random();
for (int i = 0; i < size; i++) {
arr[i] = random.nextInt(size);
}
return arr;
}
private static long timeAlgorithm(int[] arr, String algorithmName) {
long startTime = System.currentTimeMillis();
switch (algorithmName) {
case "Bubble Sort":
bubbleSort(arr);
break;
case "Selection Sort":
selectionSort(arr);
break;
case "Insertion Sort":
insertionSort(arr);
break;
case "Shell Sort":
shellSort(arr);
break;
case "Merge Sort":
mergeSort(arr);
break;
case "Quick Sort":
quickSort(arr);
break;
case "Heap Sort":
heapSort(arr);
break;
}
long endTime = System.currentTimeMillis();
return endTime - startTime;
}
private static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
private 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;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
private static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; 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;
}
}
private static void 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;
}
}
}
private static void mergeSort(int[] arr) {
if (arr.length <= 1) {
return;
}
int mid = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, mid);
int[] right = Arrays.copyOfRange(arr, mid, arr.length);
mergeSort(left);
mergeSort(right);
merge(arr, left, right);
}
private static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < left.length) {
arr[k++] = left[i++];
}
while (j < right.length) {
arr[k++] = right[j++];
}
}
private static void quickSort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
private static void quickSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int pivot = arr[left];
int i = left, j = right;
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
arr[i] = arr[j];
while (i < j && arr[i] <= pivot) {
i++;
}
arr[j] = arr[i];
}
arr[i] = pivot;
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
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--) {
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 l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest]) {
largest = l;
}
if (r < n && arr[r] > arr[largest]) {
largest = r;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
}
```
该程序首先生成了不同大小的随机数组,然后分别对每个算法进行计时,并输出结果。在这个程序中,我们测试了冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序和堆排序。
请注意,这里的计时方法是简单地获取当前时间并在算法执行结束后再次获取当前时间,然后计算两者之差。这并不是一个非常精确的计时方法,但对于我们的目的来说已经足够了。
你可以将上述程序复制到Java开发环境中,并运行它来看看不同算法在不同数据量下的表现。当然,由于不同算法的实现方式和优化程度不同,它们的表现可能会有所不同。
阅读全文