用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序等算法在随机数据的情况下的比较
时间: 2023-08-12 13:08:00 浏览: 99
排序算法比较(冒泡,选择,快排,堆排,归并,插入)
5星 · 资源好评率100%
好的,我来给您写一段Java代码,比较几种排序算法在随机数据情况下的性能:
```java
import java.util.*;
public class SortComparison {
private static final int ARRAY_SIZE = 10000;
private static final int MAX_RANDOM_NUM = 100000;
private static final int[] RANDOM_ARRAY = new int[ARRAY_SIZE];
public static void main(String[] args) {
// 生成随机数组
Random rand = new Random();
for (int i = 0; i < ARRAY_SIZE; i++) {
RANDOM_ARRAY[i] = rand.nextInt(MAX_RANDOM_NUM);
}
// 冒泡排序
int[] bubbleSortedArray = Arrays.copyOf(RANDOM_ARRAY, RANDOM_ARRAY.length);
long bubbleSortStartTime = System.nanoTime();
bubbleSort(bubbleSortedArray);
long bubbleSortEndTime = System.nanoTime();
System.out.println("冒泡排序用时:" + (bubbleSortEndTime - bubbleSortStartTime) + " 纳秒");
// 选择排序
int[] selectionSortedArray = Arrays.copyOf(RANDOM_ARRAY, RANDOM_ARRAY.length);
long selectionSortStartTime = System.nanoTime();
selectionSort(selectionSortedArray);
long selectionSortEndTime = System.nanoTime();
System.out.println("选择排序用时:" + (selectionSortEndTime - selectionSortStartTime) + " 纳秒");
// 插入排序
int[] insertionSortedArray = Arrays.copyOf(RANDOM_ARRAY, RANDOM_ARRAY.length);
long insertionSortStartTime = System.nanoTime();
insertionSort(insertionSortedArray);
long insertionSortEndTime = System.nanoTime();
System.out.println("插入排序用时:" + (insertionSortEndTime - insertionSortStartTime) + " 纳秒");
// 希尔排序
int[] shellSortedArray = Arrays.copyOf(RANDOM_ARRAY, RANDOM_ARRAY.length);
long shellSortStartTime = System.nanoTime();
shellSort(shellSortedArray);
long shellSortEndTime = System.nanoTime();
System.out.println("希尔排序用时:" + (shellSortEndTime - shellSortStartTime) + " 纳秒");
// 归并排序
int[] mergeSortedArray = Arrays.copyOf(RANDOM_ARRAY, RANDOM_ARRAY.length);
long mergeSortStartTime = System.nanoTime();
mergeSort(mergeSortedArray, 0, mergeSortedArray.length - 1);
long mergeSortEndTime = System.nanoTime();
System.out.println("归并排序用时:" + (mergeSortEndTime - mergeSortStartTime) + " 纳秒");
// 快速排序
int[] quickSortedArray = Arrays.copyOf(RANDOM_ARRAY, RANDOM_ARRAY.length);
long quickSortStartTime = System.nanoTime();
quickSort(quickSortedArray, 0, quickSortedArray.length - 1);
long quickSortEndTime = System.nanoTime();
System.out.println("快速排序用时:" + (quickSortEndTime - quickSortStartTime) + " 纳秒");
// 堆排序
int[] heapSortedArray = Arrays.copyOf(RANDOM_ARRAY, RANDOM_ARRAY.length);
long heapSortStartTime = System.nanoTime();
heapSort(heapSortedArray);
long heapSortEndTime = System.nanoTime();
System.out.println("堆排序用时:" + (heapSortEndTime - heapSortStartTime) + " 纳秒");
}
// 冒泡排序
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;
}
}
if (minIndex != i) {
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 temp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
// 希尔排序
private 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 = i - gap;
while (j >= 0 && arr[j] > temp) {
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = temp;
}
gap /= 2;
}
}
// 归并排序
private static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
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];
}
}
// 快速排序
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;
for (int j = left; j < right; j++) {
if (arr[j] < pivot) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}
int temp = arr[i];
arr[i] = arr[right];
arr[right] = temp;
return i;
}
// 堆排序
private 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--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, 0, i);
}
}
private static void heapify(int[] arr, int i, int length) {
int temp = arr[i];
for (int j = 2 * i + 1; j < length; j = 2 * j + 1) {
if (j + 1 < length && arr[j + 1] > arr[j]) {
j++;
}
if (arr[j] > temp) {
arr[i] = arr[j];
i = j;
} else {
break;
}
}
arr[i] = temp;
}
}
```
这段代码会生成一个大小为10000的随机数组,然后分别对这个数组进行冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序,并统计每种排序算法的执行时间。您可以在自己的本地环境中运行这段代码,观察不同排序算法的性能表现。
阅读全文