package sort;
import java.security.SecureRandom;
/**
* @author: xyz
* @create: 2022/8/15
* @Description:
* @FileName: Exercise23_13
* @History:
* @自定义内容:
*/
public class Exercise23_13 {
public static void main(String[] args) {
int[][] list = new int[6][1];
System.out.println("数组大小\t\t选择排序\t\t\t冒泡排序\t\t归并排序\t\t快速排序\t\t堆排序\t\t基数排序");
for (int i = 50_000; i < 300_000; i += 50_000) {
System.out.print(i + "\t\t");
list(i, list);
displayResult(list);
}
}
public static void list(int size, int[][] list) {
int[] array = new int[size];
for (int i = 0; i < array.length; i++)
array[i] = new SecureRandom().nextInt(size);
for (int i = 0; i < list.length; i++)
list[i] = array.clone();
}
public static void displayResult(int[][] list) {
for (int i = 0; i < 6; i++) {
long startTime = System.currentTimeMillis();
switch (i) {
case 0: InsertSort.selectionSort(list[i]); break;
case 1: InsertSort.bubbleSort(list[i]); break;
case 2: InsertSort.mergeSort(list[i]); break;
case 3: InsertSort.quickSort(list[i]); break;
case 4: InsertSort.heapSort(list[i]); break;
case 5: InsertSort.radixSort(list[i]); break;
default:
}
long endTime = System.currentTimeMillis();
System.out.print((endTime - startTime) + "\t\t\t");
}
System.out.println();
}
}
InsertSort类:
package sort;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Stack;
/**
* @author: xyz
* @create: 2022/8/13
* @Description:
* @FileName: InsertSort
* @History:
* @自定义内容:
*/
public class InsertSort {
public static void main(String[] args) {
int[] list = new int[10];
for (int i = 0; i < list.length; i++)
list[i] = new SecureRandom().nextInt(20);
quickSort(list);
System.out.println(Arrays.toString(list));
//
// heapSort(list);
// System.out.println(Arrays.toString(list));
//
//
// String[] list2 = {"China", "Babylon", "Paris", "america", "India", "Japan"};
// System.out.println(Arrays.toString(list2));
// heapSort(list2);
// System.out.println(Arrays.toString(list2));
}
/** 插入排序 */
public static void insertionSort(int[] list) {
for (int i = 1; i < list.length; i++) {
int currentElement = list[i];
int k; //从起点向后遍历,用于直接把后面的值赋给前一个值
for (k = i - 1; k >= 0 && list[k] > currentElement; k--)
list[k + 1] = list[k];
list[k + 1] = currentElement;
}
}
/** 冒泡排序 */
public static void bubbleSort(int[] list) {
boolean needNextPass = true;
for (int i = 1; i < list.length && needNextPass; i++) {
needNextPass = false; //数组可能已排好序
for (int j = 0; j < list.length - i; j++) {
if (list[j] > list[j + 1]) {
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
needNextPass = true;
}
}
}
}
/** 归并排序 */
public static void mergeSort(int[] list) {
if (list.length > 1) {
int[] firstHalf = new int[list.length / 2];
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf);
int secondHalfLength = list.length - list.length / 2;
int[] secondHalf = new int[secondHalfLength];
System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
mergeSort(secondHalf);
merge(firstHalf, secondHalf, list);
}
}
/** 归并数组 */
public static void merge(int[] list1, int[] list2, int[] temp) {
int current1 = 0;
int current2 = 0;
int current3 = 0;
while (current1 < list1.length && current2 < list2.length)
temp[current3++] = list1[current1] < list2[current2] ? list1[current1++] : list2[current2++];
while (current1 < list1.length)
temp[current3++] = list1[current1++];
while (current2 < list2.length)
temp[current3++] = list2[current2++];
}
/** 快速排序 */
public static void quickSort(int[] list) {
quickSort(list, 0, list.length - 1);
}
/** 快速排序辅助方法 */
private static void quickSort(int[] list, int low, int high) {
if (low > high) return; //子数组无元素时,结束方法
int temp = list[low]; //基数
int i = low;
int j = high;
while (i != j) { //遍历数组
while (i < j && list[j] >= temp) j--; //在范围内查找小于temp的数
while (i < j && list[i] <= temp) i++; //在范围内查找大于temp的数
if (i < j) { //查找到数
int t = list[i];
list[i] = list[j];
list[j] = t;
}
}
//交换基数与i == j的数的值
list[low] = list[i];
list[i] = temp;
quickSort(list, low, i - 1); //对前一部分排序
quickSort(list, i + 1, high); //对后一部分排序
}
/** 堆排序 */
public static <E extends Comparable<E>> void heapSort(E[] list) {
Heap<E> heap = new Heap<>(list);
for (int i = list.length - 1; i >= 0; i--)
list[i] = heap.remove();
}
/** 堆排序 */
public static void heapSort(int[] list) {
Heap<Integer> heap = new Heap<>();
for (int i = 0; i < list.length; i++)
heap.add(list[i]);
for (int i = list.length - 1; i >= 0; i--)
list[i] = heap.remove();
}
/** 选择排序 */
public static void selectionSort(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
int min = list[i];
int index = i;
for (int j = i + 1; j < list.length; j++) {
if (min > list[j]) {
min = list[j];
index = j;
}
}
if (index != i) {
list[index] = list[i];
list[i] = min;
}
}
}
/** 基数排序 */
public static void radixSort(int[] list) {
ArrayList[] bucket = new ArrayList[10]; //桶(存储基数0~9)
for (int j = 0; j < list.length; j++) { //将元素按末尾基数添加到桶
int key = list[j] % 10;
if (bucket[key] == null)
bucket[key] = new ArrayList();
bucket[key].add(list[j]);
}
int max = list[0];
for (int i = 1; i < list.length; i++) //查找元素最大值
if (list[i] > max)
max = list[i];
int k = 1, length = String.valueOf(max).length();
for (int n = 0; n < length; n++) { //根据基数对桶内元素重新放置
for (int i = 0; i < bucket.length; i++)
if (bucket[i] != null) {
for (int j = 0; j < bucket[i].size(); j++) {
int key = (int)bucket[i].get(j);
for (int l = 0; l < k; l++) //获取基数
key /= 10;
key %= 10;
bucket[key].add(bucket[i].get(j));
bucket[i].remove(j);
}
}
k++;
}
for (int i = 0, j = 0; i < bucket.length; i++) //对桶内元素排序并添加回数组
if (bucket[i] != null) {
Collections.sort(bucket[i]);
for (int l = 0; l < bucket[i].size(); l++)
list[j++] = (Integer) bucket[i].get(l);
}
}
}
Heap类:
package sort;
import java.util.ArrayList;
import java.util.Comparator;
/**
* @author: xyz
* @create: 2022/8/14
* @Description:
* @FileName: Heap
* @History:
* @自定义内容:
*/
public class Heap<E extends Comparable<E>> implements Cloneable {
private ArrayList<E> list = new ArrayList<>();
private Comparator<? super E> comparator;
public Heap() {
}
public Heap(E[] object) {
for (E obj: object)
add(obj);
}
public Heap(Comparator<? super E> comparator) {
this.comparator = comparator;
}
public void add(E obj) {
list.add(obj);
int currentIndex = list.size() - 1;
while (currentIndex > 0) { //大于两个元素
int parentIndex = (currentIndex - 1) / 2;
//当前元素大于父元素时交换值,否则退出循环
if (comparator != null) {
if (comparator.compare(list.get(currentIndex), list.get(parentIndex)) > 0) {
E temp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, temp);
} else break;
} else {
if (list.get(currentIndex).compareTo(list.get(parentIndex)) > 0) {
E temp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, temp);
} else break;
}
currentIndex = parentIndex; //向上移动下标
}
}
public E remove() {
if (list.size() == 0) return null; //堆为空
E remove = list.get(0);
list.set(0, list.get(list.size() - 1)); //最后一个元素设置为根元素
list.remove(list.size() - 1); //移除最后一个元素
int currentIndex = 0; //根元素下标
while (currentIndex < list.size()) {
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
if (leftChildIndex >= list.size()) break;
int maxIndex = leftChildIndex;
if (comparator != null) {
if (rightChildIndex < list.size()) //当左右子节点存在,找出之间最大值
if (comparator.compare(list.get(maxIndex), list.get(rightChildIndex)) < 0)
maxIndex = rightChildIndex;
//最大值与当前节点比较
if (comparator.compare(list.get(currentIndex), list.get(maxIndex)) < 0) {
E temp = list.get(maxIndex);
list.set(maxIndex, list.get(currentIndex));
list.set(currentIndex, temp);
currentIndex = maxIndex;
} else break;
} else {
if (rightChildIndex < list.size()) //当左右子节点存在,找出之间最大值
if (list.get(maxIndex).compareTo(list.get(rightChildIndex)) < 0)
maxIndex = rightChildIndex;
//最大值与当前节点比较
if (list.get(currentIndex).compareTo(list.get(maxIndex)) < 0) {
E temp = list.get(maxIndex);
list.set(maxIndex, list.get(currentIndex));
list.set(currentIndex, temp);
currentIndex = maxIndex;
} else break;
}
}
return remove;
}
public E get(int index) {
if (index < 0 || index >= list.size())
throw new IllegalArgumentException("Illegal index: " + index);
return list.get(index);
}
@Override
protected Object clone() throws CloneNotSupportedException {
try {
Heap<E> heap = (Heap<E>) super.clone();
ArrayList<E> list = (ArrayList<E>) this.list.clone();
return heap;
} catch (CloneNotSupportedException ex) {
return null;
}
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Heap)
return this.list.equals(((Heap<E>) obj).getList());
return false;
}
public int size() {
return list.size();
}
public ArrayList<E> getList() {
return list;
}
@Override
public String toString() {
return "Heap{" +
"list=" + list +
'}';
}
}