用java写一段代码,实现内部排序算法在随机数据、有序数据、逆序数据等不同情况下的表现
时间: 2024-03-23 11:39:57 浏览: 43
好的,以下是使用Java实现常见内部排序算法在不同数据情况下的表现:
```java
import java.util.Arrays;
import java.util.Random;
public class SortingPerformanceTest {
public static void main(String[] args) {
int[] arr = generateRandomArray(10000);
int[] sortedArr = generateSortedArray(10000);
int[] reversedArr = generateReversedArray(10000);
long start, end;
// 测试冒泡排序
start = System.currentTimeMillis();
bubbleSort(arr);
end = System.currentTimeMillis();
System.out.println("Bubble Sort on Random Data: " + (end - start) + " ms");
start = System.currentTimeMillis();
bubbleSort(sortedArr);
end = System.currentTimeMillis();
System.out.println("Bubble Sort on Sorted Data: " + (end - start) + " ms");
start = System.currentTimeMillis();
bubbleSort(reversedArr);
end = System.currentTimeMillis();
System.out.println("Bubble Sort on Reversed Data: " + (end - start) + " ms");
// 测试插入排序
arr = generateRandomArray(10000);
sortedArr = generateSortedArray(10000);
reversedArr = generateReversedArray(10000);
start = System.currentTimeMillis();
insertionSort(arr);
end = System.currentTimeMillis();
System.out.println("Insertion Sort on Random Data: " + (end - start) + " ms");
start = System.currentTimeMillis();
insertionSort(sortedArr);
end = System.currentTimeMillis();
System.out.println("Insertion Sort on Sorted Data: " + (end - start) + " ms");
start = System.currentTimeMillis();
insertionSort(reversedArr);
end = System.currentTimeMillis();
System.out.println("Insertion Sort on Reversed Data: " + (end - start) + " ms");
// 测试快速排序
arr = generateRandomArray(10000);
sortedArr = generateSortedArray(10000);
reversedArr = generateReversedArray(10000);
start = System.currentTimeMillis();
quickSort(arr, 0, arr.length - 1);
end = System.currentTimeMillis();
System.out.println("Quick Sort on Random Data: " + (end - start) + " ms");
start = System.currentTimeMillis();
quickSort(sortedArr, 0, sortedArr.length - 1);
end = System.currentTimeMillis();
System.out.println("Quick Sort on Sorted Data: " + (end - start) + " ms");
start = System.currentTimeMillis();
quickSort(reversedArr, 0, reversedArr.length - 1);
end = System.currentTimeMillis();
System.out.println("Quick Sort on Reversed Data: " + (end - start) + " ms");
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
// 插入排序
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int tmp = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > tmp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = tmp;
}
}
// 快速排序
public static void quickSort(int[] arr, int begin, int end) {
if (begin < end) {
int pivot = partition(arr, begin, end);
quickSort(arr, begin, pivot - 1);
quickSort(arr, pivot + 1, end);
}
}
private static int partition(int[] arr, int begin, int end) {
int pivot = arr[end];
int i = begin - 1;
for (int j = begin; j < end; j++) {
if (arr[j] < pivot) {
i++;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
int tmp = arr[i + 1];
arr[i + 1] = arr[end];
arr[end] = tmp;
return i + 1;
}
// 生成随机数组
public static int[] generateRandomArray(int size) {
int[] arr = new int[size];
Random rand = new Random();
for (int i = 0; i < size; i++) {
arr[i] = rand.nextInt(size);
}
return arr;
}
// 生成有序数组
public static int[] generateSortedArray(int size) {
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = i;
}
return arr;
}
// 生成逆序数组
public static int[] generateReversedArray(int size) {
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = size - i;
}
return arr;
}
}
```
上述代码中,我们使用了冒泡排序、插入排序和快速排序三种内部排序算法,并分别测试了它们在随机数据、有序数据和逆序数据情况下的表现。在实际应用中,我们也可以使用其他内部排序算法,如归并排序、堆排序等。
阅读全文