Java生成希尔排序代码
时间: 2023-04-02 08:04:59 浏览: 134
以下是 Java 生成希尔排序代码的示例:
```
public class ShellSort {
public static void sort(int[] arr) {
int n = arr.length;
for (int gap = n / 2; gap > ; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
}
}
```
这段代码可以对一个整数数组进行希尔排序,时间复杂度为 O(nlogn)。
相关问题
用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序在随机数据情况下的表现
好的,以下是用 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 的随机数组,并对其进行冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序,输出各个算法的耗时。你可以根据实际情况修改数组长度,或者增加其他排序算法的实现。
用java写一段代码,比较冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序在有序数据情况下的表现
好的,下面是一段Java代码,实现了对以上七种排序算法在有序数据情况下的性能比较:
```java
import java.util.Arrays;
public class SortingComparison {
public static void main(String[] args) {
// 生成有序数据
int[] arr = new int[10000];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
// 冒泡排序
int[] arr1 = Arrays.copyOf(arr, arr.length);
long start1 = System.nanoTime();
bubbleSort(arr1);
long end1 = System.nanoTime();
System.out.println("冒泡排序耗时:" + (end1 - start1) + "纳秒");
// 选择排序
int[] arr2 = Arrays.copyOf(arr, arr.length);
long start2 = System.nanoTime();
selectionSort(arr2);
long end2 = System.nanoTime();
System.out.println("选择排序耗时:" + (end2 - start2) + "纳秒");
// 插入排序
int[] arr3 = Arrays.copyOf(arr, arr.length);
long start3 = System.nanoTime();
insertionSort(arr3);
long end3 = System.nanoTime();
System.out.println("插入排序耗时:" + (end3 - start3) + "纳秒");
// 希尔排序
int[] arr4 = Arrays.copyOf(arr, arr.length);
long start4 = System.nanoTime();
shellSort(arr4);
long end4 = System.nanoTime();
System.out.println("希尔排序耗时:" + (end4 - start4) + "纳秒");
// 归并排序
int[] arr5 = Arrays.copyOf(arr, arr.length);
long start5 = System.nanoTime();
mergeSort(arr5);
long end5 = System.nanoTime();
System.out.println("归并排序耗时:" + (end5 - start5) + "纳秒");
// 快速排序
int[] arr6 = Arrays.copyOf(arr, arr.length);
long start6 = System.nanoTime();
quickSort(arr6, 0, arr6.length - 1);
long end6 = System.nanoTime();
System.out.println("快速排序耗时:" + (end6 - start6) + "纳秒");
// 堆排序
int[] arr7 = Arrays.copyOf(arr, arr.length);
long start7 = System.nanoTime();
heapSort(arr7);
long end7 = System.nanoTime();
System.out.println("堆排序耗时:" + (end7 - start7) + "纳秒");
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
boolean swapped = false;
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
swapped = true;
}
}
if (!swapped) {
break;
}
}
}
// 选择排序
public 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) {
swap(arr, i, minIndex);
}
}
}
// 插入排序
public static void insertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int temp = arr[i];
int j;
for (j = i - 1; j >= 0 && arr[j] > temp; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
// 希尔排序
public 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;
for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
gap /= 2;
}
}
// 归并排序
public static void mergeSort(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
private static void mergeSort(int[] arr, int start, int end) {
if (start >= end) {
return;
}
int mid = (start + end) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
private static void merge(int[] arr, int start, int mid, int end) {
int[] temp = new int[end - start + 1];
int i = start, j = mid + 1, k = 0;
while (i <= mid && j <= end) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= end) {
temp[k++] = arr[j++];
}
for (int p = 0; p < temp.length; p++) {
arr[start + p] = temp[p];
}
}
// 快速排序
public static void quickSort(int[] arr, int start, int end) {
if (start >= end) {
return;
}
int pivotIndex = partition(arr, start, end);
quickSort(arr, start, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, end);
}
private static int partition(int[] arr, int start, int end) {
int pivot = arr[start];
int left = start + 1;
int right = end;
while (left <= right) {
while (left <= right && arr[left] <= pivot) {
left++;
}
while (left <= right && arr[right] >= pivot) {
right--;
}
if (left <= right) {
swap(arr, left, right);
}
}
swap(arr, start, right);
return right;
}
// 堆排序
public 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--) {
swap(arr, 0, i);
heapify(arr, 0, i);
}
}
private static void heapify(int[] arr, int i, int n) {
int left = i * 2 + 1;
int right = i * 2 + 2;
int largest = i;
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, largest, n);
}
}
// 交换数组中的两个元素
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
```
这段代码会生成一个长度为10000的有序数组,并对该数组进行冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序,分别计算它们的耗时,并输出结果。由于数组是有序的,所有排序算法的性能都应该比较好,但不同算法的耗时也会有所差异。
阅读全文