- package sort;
-
- import java.security.SecureRandom;
- import java.util.Arrays;
- import java.util.Comparator;
-
- /**
- * @author: xyz
- * @create: 2022/8/14
- * @Description:
- * @FileName: Exercise23_03
- * @History:
- * @自定义内容:
- */
- public class Exercise23_03 {
- public static void main(String[] args) {
- Integer[] list1 = new Integer[10];
- for (int i = 0; i < list1.length; i++)
- list1[i] = new SecureRandom().nextInt(20);
-
- System.out.println(Arrays.toString(list1));
- quickSort(list1, new SortComparator<>());
- System.out.println(Arrays.toString(list1));
-
- String[] list2 = {"China", "Babylon", "Paris", "america", "India", "Japan"};
- System.out.println(Arrays.toString(list2));
- quickSort(list2, new SortComparator<>());
- System.out.println(Arrays.toString(list2));
-
- Double[] list3 = new Double[10];
- for (int i = 0; i < list3.length; i++)
- list3[i] = Math.random() * 20;
-
- System.out.println(Arrays.toString(list3));
- quickSort(list3, new SortComparator<>());
- System.out.println(Arrays.toString(list3));
- }
-
- /** 使用Comparable接口的泛型快速排序 */
- public static
extends Comparable> void quickSort(E[] list) { - quickSort(list, 0, list.length - 1);
- }
-
- /** 使用Comparable接口的泛型快速排序辅助方法 */
- private static
extends Comparable> void quickSort(E[] list, int low, int high) { - if (low > high) return;
-
- E temp = list[low];
- int i = low;
- int j = high;
-
- while (i != j) {
- while (i < j && (list[j].compareTo(temp) > 0 || list[j].compareTo(temp) == 0)) j--;
- while (i < j && (list[i].compareTo(temp) < 0 || list[i].compareTo(temp) == 0)) i++;
-
- if (i < j) {
- E t = list[i];
- list[i] = list[j];
- list[j] = t;
- }
- }
- list[low] = list[i];
- list[i] = temp;
-
- quickSort(list, i + 1, high);
- quickSort(list, low, i - 1);
- }
-
- /** 使用Comparator接口的泛型快速排序 */
- public static
void quickSort(E[] list, Comparator super E> comparator) { - quickSort(list, 0, list.length - 1, comparator);
- }
-
- /** 使用Comparator接口的泛型快速排序辅助方法 */
- private static
void quickSort(E[] list, int low, int high, Comparator super E> comparator) { - if (low > high) return;
-
- E temp = list[low];
- int i = low;
- int j = high;
-
- while (i != j) {
- while (i < j && (comparator.compare(list[j], temp) > 0 || comparator.compare(list[j], temp) == 0)) j--;
- while (i < j && (comparator.compare(list[i], temp) < 0 || comparator.compare(list[i], temp) == 0)) i++;
-
- if (i < j) {
- E t = list[i];
- list[i] = list[j];
- list[j] = t;
- }
- }
- list[low] = list[i];
- list[i] = temp;
-
- quickSort(list, i + 1, high, comparator);
- quickSort(list, low, i - 1, comparator);
- }
-
- /** 静态内部类-泛型比较器类 */
- static class SortComparator
implements Comparator { - @Override
- public int compare(E o1, E o2) {
- if (o1 instanceof Comparable) {
- if (((Comparable) o1).compareTo(o2) > 0) return 1;
- else if (((Comparable) o1).compareTo(o2) < 0) return -1;
- else return 0;
- }
-
- //按哈希码排序
- if (o1.hashCode() > o2.hashCode())
- return 1;
- else if (o1.hashCode() < o2.hashCode())
- return -1;
- else
- return 0;
- }
- }
- }